DataGridView checkbox column - value and functionality

后端 未结 11 1739
一向
一向 2020-11-28 12:17

I\'ve added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be s

11条回答
  •  旧巷少年郎
    2020-11-28 12:32

    If your column has been already created due to binding with a recordset of BIT type, the type of the column will be text anyway. The solution I have found is to remove that column and replace it with a DataGridViewCheckBoxColumn having the same binding data.

    DataGridViewColumn oldCol = dgViewCategory.Columns["mycolumn"];
    int chkIdx = oldCol.Index;
    DataGridViewCheckBoxColumn chkCol = new DataGridViewCheckBoxColumn();
    chkCol.HeaderText = oldCol.HeaderText;
    chkCol.FalseValue = "False";
    chkCol.TrueValue = "True";
    chkCol.DataPropertyName = oldCol.DataPropertyName;
    chkCol.Name = oldCol.Name;
    dgViewCategory.Columns.RemoveAt(chkIdx);
    dgViewCategory.Columns.Insert(chkIdx, chkCol);
    

提交回复
热议问题