title: C# - Datagridview cannot bind data from datatable
body: <p>I have <code>datagridview</code> binding data from <code>datatable</code>. When I checked the number of columns of <code>datatable</code>, it returned 10. However, <code>datagridview</code> got an error when showing more than 8 columns. The error is <code>Index was out of range. Must be non-negative and less than the size of the collection</code> .Below is my code and also an error I got. Please help me!</p>
  
  <pre><code>public void SearchPatient(string query)
      {
          MySqlConnection MysqlConnection = new MySqlConnection(Properties.Settings.Default.connectionString);
          MySqlCommand MysqlCmd = new MySqlCommand(query, MysqlConnection);
          MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
          MyAdapter.SelectCommand = MysqlCmd;
          DataTable dTable = new DataTable();
          MyAdapter.Fill(dTable);
          rows = dTable.Rows.Count;
          MessageBox.Show(rows + "  " + dTable.Columns.Count); // It showed 15 and 10
          dataGridView1.DataSource = dTable;
          dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
          dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font(dataGridView1.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold);
          dataGridView1.Columns[0].HeaderText = "ID";
          dataGridView1.Columns[1].HeaderText = fullname;
          dataGridView1.Columns[2].HeaderText = birthday;
          dataGridView1.Columns[2].DefaultCellStyle.Format = "dd/MM/yyyy";
          dataGridView1.Columns[3].HeaderText = gender;
          dataGridView1.Columns[4].HeaderText = address;
          dataGridView1.Columns[5].HeaderText = phonenumber;
          dataGridView1.Columns[6].HeaderText = cmnd;
          dataGridView1.Columns[7].HeaderText = note;
         dataGridView1.Columns[8].HeaderText = "ID benh nhan"; // Error: Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
  
      }
  </code></pre>
  
  <p>This is my database:
  <a href="https://i.stack.imgur.com/sVBGV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sVBGV.png" alt="enter image description here"></a></p>
  
  <p>And my query is: <code>SELECT * FROM patientdatabase ORDER BY ID DESC LIMIT 0,15</code></p>