title: Generating Stats from Datagridview rows
body: <p>i am working on a section of a project, that parses Logs from Postgres Database Server.
  The application is developed in C sharp Framework 4.0.</p>
  
  <p>A log is produced and displayed on a DataGridView with the following columns</p>
  
  <pre><code>resultCode       Statement                  Starttime                   Duration
  XT001         select * from PizzaMade    01-02-2012 03:10:14         00:04:10
  </code></pre>
  
  <ul>
  <li>there are many loglines with the sameformat.</li>
  <li>The Datagridview is filled from another loop by parsing a Textfile.</li>
  </ul>
  
  <p>My work is to generate stats from the data available in Grid in the following format</p>
  
  <pre><code>Statement                                 Count        countpercent                    Occurs_on
  
  select * from PizzaMade                     3             1.42        01/02 at 03pm   [00:04:10], 01/02 at 04 [00:01:04]
  select id,qty,table from PizzaMade          12             5.12           ...........
  </code></pre>
  
  <p>so basically the stats reflect the following</p>
  
  <ul>
  <li>a) statement executed</li>
  <li>b) Count number of times it appears in a grid</li>
  <li>c) percentage of count which is basically the portion of totalcounts this statement occupies</li>
  <li>d) a concatenated string containing starttime,duration</li>
  </ul>
  
  <hr>
  
  <p>» The stats are generated as Datatable first, using a for loop</p>
  
  <pre><code>foreach(DataGridViewRow dr in LogGrid.Rows)
  {
  // search in the Datatable if the statement is present
  // if it is then add count , add starttime and duration to the column
  // if not then add a newrow
  
  }
  </code></pre>
  
  <p>» After populating the datatable , i use a loop to calculate the Totalcount</p>
  
  <pre><code>int totalcount = 0;
  foreach (DataRow drin StatTable.Rows)
  {
  totalcount = totalcount + Convert.ToInt32(dr["count"].ToString());
  }
  </code></pre>
  
  <p>» After calculating the count, there is a loop to calculate the percentage</p>
  
  <pre><code>foreach (DataRow dr in StatTable.Rows)
  {
      int c = Convert.ToInt32(dr["count"].ToString());
      dr["countpercent"] = (c/totalcount)*100;
  }
  </code></pre>
  
  <hr>
  
  <p>Although everything seems ok, the whole method is sluggish with large number of rows.</p>
  
  <ul>
  <li>Can you please suggest methods to improve the performance.</li>
  </ul>
  
  <p>thanks
  arvind</p>