Databinding on DataGridBoolColumn

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;
 
M

Marc Gravell

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
 
G

Guest

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top