Checking the value of a CheckBoxCell in a DataGridVIew C#

58 views Asked by At

I have some records from a database displayed in a datagridview for the use to view and select data. I needed a way for users to select individual records for exporting the data into a formatted PDF document so I set up a CheckBoxCollumn. My Database and subsequent datagridviews are set up in tandem with one containing basic client information and the other the accounts associated with them. As I'm using the gridviews in tandem when a client is selected on the associate records show in the gridview. This means that if the user wants more than one clients records selected than they're unable to. To solve this problem I want to log what records have been checked and then when the PDF is generated it pulls the data from the records. My problem is with identifying which records are checked.

private void Accounts_DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridViewRow row in accounts_DataGridView.Rows)
    {
        DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;

            if (cell.Value == cell.TrueValue)
            {
                MessageBox.Show("its checked");
            }
        }
    }
}

This is the code that I currently have. The event handling functions correctly but the value always returns null.

My DataGridView for reference

2

There are 2 answers

2
Kostas L On

I have faced the same issue in the past and I never got it working with the TrueValue property.

Instead, I used the cell.Value property like below and I was able to continue my project without diving into the events of DataGridView that may be happening in the background.

if (cell.Value != null)
{
    string s = cell.Value.ToString();
    if (s == "True")
    {
       MessageBox.Show("its checked");   
    }
}
0
Karen Payne On

If the DataGridView.DataSource is a DataTable (and you have the primary key for each row), after populating the DataTable, Add a column for permitting checking rows.

Here table is the DataTable

table.Columns.Add(new DataColumn()
{
    ColumnName = "Process",
    DataType = typeof(bool),
    // important else default value is null
    DefaultValue = false 
});

In the DataGridView designer, set the column for the above DataPropertyName to Process. Otherwise you need to set the position for the Process column.

table.Columns["Process"]!.SetOrdinal(0);

Declare a BindingSource in the form

private BindingSource _bindingSource = new BindingSource();

Assign the DataTable to the BindingSource DataSource then assign the BindingSource to the DataSource for the DataGridView.

Code to get checked rows

EnumerableRowCollection<DataRow> dataRows = ((DataTable)_bindingSource.DataSource).AsEnumerable()
    .Where(row => row.Field<bool>("Process"));

Get the primary key, change ProductId to your primary key.

if (dataRows.Any())
{
    var keys = dataRows.Select(x => x.Field<int>("ProductId")).ToList();
}

The idea here is to work against the data rather than having to touch the DataGridView to get at any data including checked rows.