My DataTable has three columns fetched from a database, while I need to bind only two columns of it to a DataGridView. Can you please help me with
We can create a new DataTable with the required Columns and add rows to it from the Dataset. Then we can initialize the DataGrid with the Newly Created DataTable.
dt = new DataTable();
dt_Property.Columns.Add("Field1");
dt_Property.Columns.Add("Field2");
int i = 0;
DataRow row = null;
foreach (DataRow r in ds.Tables[0].Rows)
{
row = dt.NewRow();
row["Field1"] = ds.Tables[0].Rows[i][1];
row["Field2"] = ds.Tables[0].Rows[i][2];
dt_Property.Rows.Add(row);
i = i + 1;
}
dataGridView1.DataSource = dt;