title: How to cache a datagridview in windows forms application
body: <p>I am working on an application in which data is retrieved from four <code>SQL</code> tables using <code>joins</code> and the retrieved data is populated to a <code>datagridview</code> on a windows form.
  I have two <code>Radio Buttons</code> <code>ALL</code> and <code>DrawDate</code> , by default <code>ALL</code> Radio Button is selected and once the application is opened it populates all the data on to the <code>datagridview</code> and when I select <code>DrawDate Radio Button</code>, only the data related to that draw date is populated on the <code>datagridview</code>. Everything looks fine until then but after selecting <code>DrawDate Radio Button</code> and if a user want to get all the data again by selecting <code>ALL</code> <code>Radio Button</code>, it again loads all data from the database server which is not preferred. 
  Is there any better way I can cache the data populated once the application is opened and populate it when a user selects <code>ALL</code> <code>Radio Button</code> later when he needs it?</p>
  
  <p><strong>C# Code</strong></p>
  
  <pre><code>sqlcon = GetConnectionString();
              try
              {
                  sqlcon.Open();
  
                  //var sw = Stopwatch.StartNew();
  
                  for (int i = 0; i &lt; dgvPaymentsReceived_Collections.RowCount; i++)
                  {
                      int trademonth = Convert.ToInt32(dgvPaymentsReceived_Collections.Rows[i].HeaderCell.Value);
  
  
                      for (int j = 0; j &lt; dgvPaymentsReceived_Collections.ColumnCount; j++)
                      {
                          int paymentmonth = Convert.ToInt32(dgvPaymentsReceived_Collections.Columns[j].HeaderCell.Value);
  
                          //var sw = Stopwatch.StartNew();
  
                          SqlCommand cmd_PaymentsReceived = new SqlCommand();
  
                          cmd_PaymentsReceived.Connection = sqlcon;
                          cmd_PaymentsReceived.CommandType = CommandType.StoredProcedure;
                          cmd_PaymentsReceived.CommandText = sp_PaymentsReceved_Collections;
  
                          cmd_PaymentsReceived.Parameters.Add(new SqlParameter("@trademonth", trademonth));
                          cmd_PaymentsReceived.Parameters.Add(new SqlParameter("@paymentmonth", paymentmonth));
  
  
                          SqlDataAdapter da_PaymentsReceived_Collections = new SqlDataAdapter();
                          DataTable dt_PaymentsReceived_Colletions = new DataTable();
  
                          da_PaymentsReceived_Collections.SelectCommand = cmd_PaymentsReceived;
                          da_PaymentsReceived_Collections.Fill(dt_PaymentsReceived_Colletions);
  
                          //sw.Stop();
                          //MessageBox.Show(sw.ElapsedMilliseconds.ToString());
  
                          dgvPaymentsReceived_Collections.Rows[i].Cells[j].Value = dt_PaymentsReceived_Colletions.Rows[0][0].ToString();
  
                      }
                  }
  
                  sqlcon.Close();
              }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/SNKj2.png" alt="enter image description here"></p>