Databinding on DataGridBoolColumn

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm newbie on winform and datagrid

I have a DataGrid with a DataGridBoolColumn.
The DataGrid is binded to a DataTable.

The DataTable is the resultset of SQL query "SELECT UserName, Enabled FROM
siteuser" and the Enabled field is a integer type that 1 means enabled while
non-1 means disabled.

I want the DataGridBoolColumn is checked if the Enabled is 1 while keep
unchecked if it's not.

How can I do that? Here is my snippet:

DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "Siteuser";

DataGridTextBoxColumn textColumn = new DataGridTextBoxColumn();
textColumn.MappingName = "UserName";
textColumn.HeaderText = "User Name";
tableStyle.GridColumnStyles.Add(textColumn);

DataGridBoolColumn boolColumn = new DataGridBoolColumn();
boolColumn.MappingName = "Enabled";
boolColumn.HeaderText = "Enabled";
boolColumn.AllowNull = false;
tableStyle.GridColumnStyles.Add(boolColumn);

this.dataGrid1.TableStyles.Add(tableStyle);

this.dataGrid1.DataSource = dataTable;
 
Well, have you tried changing the SQL to return a bit? i.e. CAST(CASE
Enabled WHEN 1 THEN 1 ELSE 0 END as bit)

?

Marc
 
Thanks Marc,

I solved my problem!

My I got another issue:

The checkbox in the DataGridBoolColumn has 3 states!
Beside from checked and unchecked, it have another state that have a tick in
a grey checkbox (look like disabled checkbox)
It changes states from "checked" -> "grey checked" -> "unchecked"

How can I disable the grey checkbox, I already set
DataGridBoolColumn.AllowNull to false
 
I am having the same headache. Where is this ".ThreeState" property? I can't
find it either in DataGridBoolColumn or DataColumn. It can be found in
CheckBox, but that is no help as there is no access to the hosted control
from DataGridBoolColumn as there is in DataGridTextBoxColumn.
 
Back
Top