Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a Datagridview column to handle nullable bools?

I am displaying data from SQL in a datagridview by using a SqlDataAdapter. (This is a C# Winforms app but this question could just as easily apply to VB.) Some of the columns are a nullable bool.

Currently the datagridview uses a standard CheckBox column, which uses an empty checkbox both for null and false, and a checked checkbox for true.

I would like to create a column that displays no checkbox at all for null values.

What's the best way to approach this problem? I am wondering if I should create a new control that inherits from the DataGridView and go from there.

like image 301
JYelton Avatar asked Oct 12 '22 04:10

JYelton


1 Answers

Here is a solution that I've employed and is currently working:

((DataGridViewCheckBoxColumn)dgvDisplayData.Columns["field_name"]).ThreeState = true;

This is done after the data binding and assuming that the sql field "field_name" is a nullable boolean. Resharper isn't especially happy with this format, providing a "Possible System.NullReferenceException" warning.

I'd still be interested in alternative/better suggestions.

Edit:

I've changed this as follows to avoid potential null value exceptions:

DataGridViewCheckBoxColumn chkCol =
    (DataGridViewCheckBoxColumn)dgvDisplayData.Columns["field_name"];
if (chkCol != null) chkCol.ThreeState = true;
like image 164
JYelton Avatar answered Oct 15 '22 11:10

JYelton