Reputation:
I have a DataGridView in my form and it gets its source from a DataTable. Would there be a way to change the column type to a DataGridViewCheckBoxColumn after setting the source or would I have to insert each row and its cell separately and perform the change then
Previous code:
Select("uspSelectChildByClientID",
new string[] { "clientID" }, new string[] { nodeTag.ToString() });
dataGridViewC.DataSource = dataTable;
Intended code (if there's really no way to change it after getting the source):
Select("uspSelectChildByClientID", new string[] { "clientID" },
new string[] { nodeTag.ToString() });
for (int x = 0; x < dataTable.Columns.Count; x++)
dataGridViewC.Columns.Add("hello", dataTable.Columns[x].ColumnName.ToString());
DataGridViewCheckBoxCell column = new DataGridViewCheckBoxCell();
*add checkbox column and make another loop*
Upvotes: 1
Views: 4986
Reputation:
Usually, relying on DataSource
and Columns.Insert()
at the same time in DataGridView
is not precisely recommendable. Although when dealing with special column types (like DataGridViewCheckBoxColumn
), this approach does deliver a quite good answer. Further modifications at the cell level might even not be required. Sample code:
DataTable dt = new DataTable();
dt.Columns.Add("Col 1", typeof(int));
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
dataGridView1.DataSource = dt;
DataGridViewCheckBoxColumn curCol = new DataGridViewCheckBoxColumn();
curCol.HeaderText = "Col 2";
dataGridView1.Columns.Insert(1, curCol);
Upvotes: 1