摘要:C# - DataGridView 自動對應 DataTable 欄位
public static void AdjustDataGridViewColumn(DataTable dataTable, DataGridView dataGridView)
{
dataGridView.Columns.Clear();
for (int i = 0; i < dataTable.Columns.Count; i++)
{
DataColumn dc = dataTable.Columns[i];
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = dc.ColumnName;
col.ValueType = typeof(string);
col.DataPropertyName = dc.ColumnName;
col.HeaderText = dc.Caption;
col.Visible = true;
col.FillWeight = 150;
dataGridView.Columns.Add(col);
}
}