简体   繁体   English

C#-Datagridview无法绑定数据表中的数据

[英]C# - Datagridview cannot bind data from datatable

I have datagridview binding data from datatable . 我有datagridviewdatatable绑定数据。 When I checked the number of columns of datatable , it returned 10. However, datagridview got an error when showing more than 8 columns. 当我检查datatable的列数时,它返回10。但是,当显示8列以上时, datagridview出现错误。 The error is Index was out of range. Must be non-negative and less than the size of the collection 错误是Index was out of range. Must be non-negative and less than the size of the collection 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. Index was out of range. Must be non-negative and less than the size of the collection以下是我的代码,也是我遇到的错误。 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: 这是我的数据库: 在此处输入图片说明

And my query is: SELECT * FROM patientdatabase ORDER BY ID DESC LIMIT 0,15 我的查询是: SELECT * FROM patientdatabase ORDER BY ID DESC LIMIT 0,15

It looks like columns are added in the designer and your DataGridView's AutoGenerateColumns is set to false. 好像在设计器中添加了列,并且DataGridView的AutoGenerateColumns设置为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 或者,将dataGridView1.AutoGenerateColumns设置为True

See reference here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns(v=vs.110).aspx 请参见此处的参考: https : //msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.autogeneratecolumns(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM