I am trying to change the column width of my second column in datagridview. I keep on getting - Index out of range error. If I click on datagridview, it shows that it has binded with my datatable and all the columns and data are showing. Tried everything and are stuck, please help
DataTable dt = new DataTable();
da.Fill(dt);
dt.Columns["ID"].SetOrdinal(0);
dt.Columns["Name"].SetOrdinal(1);
dt.Columns["SKU"].SetOrdinal(2);
dt.Columns["Price"].SetOrdinal(3);
dt.Columns["QTY"].SetOrdinal(4);
dt.Columns["Status"].SetOrdinal(5);
DataRow row = dt.NewRow();
bindingSource1.DataSource = dt;
dataGridView2.DataSource = dt;
dataGridView2.Location = new Point(0, 0);
dataGridView2.ClientSize = new Size(850, 500);
this.Controls.Add(dataGridView2); // Display table
dataGridView2.Columns[1].Width = 300; // ERROR here
I have also tried
DataGridViewColumn firstcolumn = dataGridView2.Columns[1];
firstcolumn.Width = 300;