title: How to fill DataGridView Programmatically
body: <p>I need to fill a DataGridView programmatically. The columnus of the database are fixed and the Rows depends on the size of list. Actually i have <code>List&lt;MyCustomClass&gt;</code> and i need to fill data with that list.</p>
  
  <p>Currently, I am doing this: </p>
  
  <pre><code>public Constructor()
  {
      InitializeComponent();
      dataGridViewFiles.AutoGenerateColumns = false;
      dataGridViewFiles.ColumnCount = 3;
      dataGridViewFiles.Columns[0].Name = "File Name";
      dataGridViewFiles.Columns[1].Name = "Total Documents";
      dataGridViewFiles.Columns[2].Name = "Total Pages";
  }
  
  Public LoadDGV()
  {
      for (int i = 0; i &lt; nTotalInputFiles; i++)
      {//add code here for adding rows to dataGridviewFiles
          DataGridViewRow tempRow = new DataGridViewRow();
  
          DataGridViewCell cellFileName = new DataGridViewTextBoxCell();
          cellFileName.Value = selectedProject.InputFiles[i].FileName;
          tempRow.Cells.Add(cellFileName);
  
          DataGridViewCell cellDocCount = new DataGridViewTextBoxCell();
          cellDocCount.Value = selectedProject.InputFiles[i].DocCount.ToString();
          tempRow.Cells.Add(cellDocCount);
  
          DataGridViewCell cellPageCount = new DataGridViewTextBoxCell();
          cellPageCount.Value = selectedProject.InputFiles[i].PageCount.ToString();
          tempRow.Cells.Add(cellPageCount);
  
          tempRow.Tag = selectedProject.InputFiles[i].Id;
          dataGridViewFiles.Rows.Add(tempRow);
      }
  </code></pre>
  
  <p>But the above code some time doesn't perfect. So is there any other way? Or any suggestion to improve above one?</p>