DataGridView checkbox column - value and functionality

后端 未结 11 1763
一向
一向 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:35

    1) How do I make it so that the whole column is "checked" by default?

    var doWork = new DataGridViewCheckBoxColumn();
    doWork.Name = "IncludeDog" //Added so you can find the column in a row
    doWork.HeaderText = "Include Dog";
    doWork.FalseValue = "0";
    doWork.TrueValue = "1";
    
    //Make the default checked
    doWork.CellTemplate.Value = true;
    doWork.CellTemplate.Style.NullValue = true;
    
    dataGridView1.Columns.Insert(0, doWork);
    

    2) How can I make sure I'm only getting values from the "checked" rows?

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.IsNewRow) continue;//If editing is enabled, skip the new row
    
        //The Cell's Value gets it wrong with the true default, it will return         
        //false until the cell changes so use FormattedValue instead.
        if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
        {
            //Do stuff with row
        }
    }
    

提交回复
热议问题