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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With