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數據綁定的方式,可以根據具體情況選擇適合的方式。