DataGrid and DataTable probs

  • Thread starter Thread starter Edge
  • Start date Start date
E

Edge

Hey all,

I have a simple datatable consisting of a few columns of the type Boolean
and some rows. Using the defaultView of the table, I connect it to a
dataGrid.
The datagrid automatically displays the table as a grid of checkboxes. You
can set and unset these boxes and the table is updated accordingly.
All works fine accept for two things:
1. I have to select the cell in which the checkbox I want to set is. In
other words, I _always_ need to click twice for a box to set. Is there any
setting to change that?
2. The checkboxes all have 3 possible states: checked, unchecked and checked
with grey background (as if there's some underlying settings you partially
checked). Is it possible to somehow tell the checkboxes they can only be
checked ot unchecked?

Thnx in advance,

Ben
 
Hi,

There are 2 options. Either add a datagridstyle to your checkbox column (actually u will have to add the styles to all the columns) and in that you add the foll. line ((DataGridBoolColumn)boolCol).AllowNull = false in the DataGridBoolColumn object.
Other option is to intercept the datagrid mouseup event and change the checkbox state to what u desire.

For more details refer the winform faq URL(http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp)
sections are
5.15 How can I put a checkbox in a column of my DataGrid?
5.80 How can I get a CheckBox column in a DataGrid to react to the first click?
 
I'm having similar issues - it's most likely my lack of knowledge for
the DataGrid, DataTable, DataSet framework... Let me run this by
you. I have an XML file that I get into a DataSet. I create a
DataTable with the first table in my DataSet. I then bind this to a
DataGrid and the 13 columns I want from my original XML file are
indeed shown in the DataGrid.

Now - what I want to do is add in a 14th column and have it setup as a
two state checkbox that I can essentially loop through when the user
is done checking through the various line items.

My thinking was - take the DataTable, create a new DataColumn
(typeof(bool)), add that DataColumn into the DataTable. Then I tried
to create a DataGridTableStyle, create a DataGridColumnStyle, map that
ColumnStyle to my added DataColumn, add that ColumnStyle into my
DataGridTableStyle and add the TableStyle into my DataGrid.

When I do this, only the checkbox column appears - not the other 13
rows...

Here is some of my code

DataTable _resultTable = ds.Tables[0] ;

DataColumn cCurrent = new DataColumn("Arbitrated", typeof(bool)) ;
cCurrent.DefaultValue = false ;

_resultTable.Columns.Add(cCurrent) ;

DataGridTableStyle ts1 = new DataGridTableStyle() ;
ts1.MappingName = "Result" ;

DataGridColumnStyle boolCol = new DataGridBoolColumn() ;
boolCol.MappingName = "Arbitrated" ;
boolCol.HeaderText = "header text" ;
boolCol.Width = 150 ;
((DataGridBoolColumn)boolCol).AllowNull = false;

ts1.GridColumnStyles.Add(boolCol) ;

dataGrid1.TableStyles.Add(ts1) ;

this.dataGrid1.DataSource = _resultTable ;

Again - this correctly sets up a two state column and it shows in the
DataGrid - but where did my other columns go???
 
I'm having similar issues - it's most likely my lack of knowledge for
the DataGrid, DataTable, DataSet framework... Let me run this by
you. I have an XML file that I get into a DataSet. I create a
DataTable with the first table in my DataSet. I then bind this to a
DataGrid and the 13 columns I want from my original XML file are
indeed shown in the DataGrid.

Now - what I want to do is add in a 14th column and have it setup as a
two state checkbox that I can essentially loop through when the user
is done checking through the various line items.

My thinking was - take the DataTable, create a new DataColumn
(typeof(bool)), add that DataColumn into the DataTable. Then I tried
to create a DataGridTableStyle, create a DataGridColumnStyle, map that
ColumnStyle to my added DataColumn, add that ColumnStyle into my
DataGridTableStyle and add the TableStyle into my DataGrid.

When I do this, only the checkbox column appears - not the other 13
rows...

Here is some of my code

DataTable _resultTable = ds.Tables[0] ;

DataColumn cCurrent = new DataColumn("Arbitrated", typeof(bool)) ;
cCurrent.DefaultValue = false ;

_resultTable.Columns.Add(cCurrent) ;

DataGridTableStyle ts1 = new DataGridTableStyle() ;
ts1.MappingName = "Result" ;

DataGridColumnStyle boolCol = new DataGridBoolColumn() ;
boolCol.MappingName = "Arbitrated" ;
boolCol.HeaderText = "header text" ;
boolCol.Width = 150 ;
((DataGridBoolColumn)boolCol).AllowNull = false;

ts1.GridColumnStyles.Add(boolCol) ;

dataGrid1.TableStyles.Add(ts1) ;

this.dataGrid1.DataSource = _resultTable ;

Again - this correctly sets up a two state column and it shows in the
DataGrid - but where did my other columns go???

When you don't specify a TableStyle, .NET automatically generates one
based on all the columns in your datatable.

When you specify one, it then doesn't do this.
What you should do, is create a tablestyle with ALL 13+1 columns.
(Tedius, I know).

You can use the designer to help you with this, I generally got fed up
with copy/paste code - I feel fiddle with the designer is easier.

If the dataset is strongly typed - and you can add it to your form,
then it's even easier.

jliu - www.ssw.com.au - www.johnliu.net
 
Back
Top