0

I have datagridview binding data from datatable. When I checked the number of columns of datatable, it returned 10. However, datagridview got an error when showing more than 8 columns. The error is Index was out of range. Must be non-negative and less than the size of the collection .Below is my code and also an error I got. Please help me!

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.

    }

This is my database: enter image description here

And my query is: SELECT * FROM patientdatabase ORDER BY ID DESC LIMIT 0,15

6
  • Paste a screenshot of your Quick Watch for dTable. Commented Mar 5, 2018 at 3:25
  • Can you post your SQL script? Commented Mar 5, 2018 at 3:26
  • @henocsalinas below are my database data and my query Commented Mar 5, 2018 at 3:42
  • 2
    What is the count of dataGridView1.Columns after you set it's data source? Check the Data Grid View properties in Designer View and see if columns are defined? Commented Mar 5, 2018 at 3:50
  • @henocsalinas dataGridView.Columns.Count() returns 8 Commented Mar 5, 2018 at 3:57

1 Answer 1

1

It looks like columns are added in the designer and your DataGridView's AutoGenerateColumns is set to false. As your columns count doesn't match your data source, therefore you are getting this error.

To fix this, either

  1. Add columns in the designer to match the Data source
  2. or, set dataGridView1.AutoGenerateColumns to True

See reference here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help. I just set dataGridView1.AutoGenerateColumns = true and It works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.