DataGridView checkbox column - value and functiona

2019-01-06 18:22发布

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 serviced, and you select which of them you wish to be serviced this time around.

Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:

1) How do I make it so that the whole column is "checked" by default? 2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?

Here's the code to get the column inserted:

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "0";
            doWork.TrueValue = "1";
            dataGridView1.Columns.Insert(0, doWork);

So what next? Any help would be greatly appreciated!

9条回答
走好不送
2楼-- · 2019-01-06 18:53

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
    }
}
查看更多
等我变得足够好
3楼-- · 2019-01-06 18:56

if u make this column in sql database (bit) as a data type u should edit this code

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "0";
            doWork.TrueValue = "1";
            dataGridView1.Columns.Insert(0, doWork);

with this

DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
            doWork.HeaderText = "Include Dog";
            doWork.FalseValue = "False";
            doWork.TrueValue = "True";
            dataGridView1.Columns.Insert(0, doWork);
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-06 18:58
  1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        row.Cells[CheckBoxColumn1.Name].Value = true;
    }
    
  2. The Click event might look something like this:

    private void button1_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
            {
                rows_with_checked_column.Add(row);
            }
        }
        // Do what you want with the check rows
    }
    
查看更多
登录 后发表回答