C#中DataGridView控件可以通过以下几种方式进行数据绑定:
dataGridView1.DataSource = myDataTable;
foreach (DataRow row in myDataTable.Rows)
{
dataGridView1.Rows.Add(row.ItemArray);
}
foreach (DataColumn column in myDataTable.Columns)
{
dataGridView1.Columns.Add(column.ColumnName, column.ColumnName);
}
for (int i = 0; i < myDataTable.Rows.Count; i++)
{
dataGridView1.Rows.Add(myDataTable.Rows[i].ItemArray);
}
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = myDataTable;
dataGridView1.DataSource = bindingSource1;
以上是常见的DataGridView数据绑定的方式,可以根据具体情况选择适合的方式。