C# – Bind named datagridview column with specific column in datatable

cdatagridviewdatatablewinforms

I added some columns into datagridview by using the following code:

dataGridView1.Columns.Add("name");
dataGridView1.Columns.Add("age");
dataGridView1.Columns.Add("salary");

I have datatable that contains data from stored procedure

select col1,col2,col3 from emp

I know the traditional way to bind datagridview with datatable or dataset ,but the problem that I rebuilt datagridview where I put multi Headers and I merged some headers together so I want way to bind specific datatable column with specific datagridview column like

dataGridView1.column("Name") = dt.column("col1");

Best Answer

You are looking for the DataGridViewColumn.DataPropertyName property.

dataGridView1.Columns["Name"].DataPropertyName = "col1";
Related Topic