Articles in this section
Category / Section

How do I insert an unbound check box column in a GridDataBoundGrid?

4 mins read

 

There are three steps needed to have an unbound checkbox column in the GridDataBoundGrid.

1. Add the unbound column to the GridDataBoundGrid.

2. A bounded column has it own datastore to store the values entered in it. But in an unbound column there is no datastore present in it, so it has to be handled manually by adding any collection like hashtable. To make unbound column work together with sort, assign key as corresponding primary key value in the row in the Key/Value pair of the external collection.

3. Then Model.QueryCellInfo event is used to display the values in the checkbox. The Model.SaveCellInfo event is used to save the changes made by the user in the checkbox.

C#

private void Form1_Load(object sender, System.EventArgs e)

{

...

GridBoundColumnsCollection column5 = (GridBoundColumnsCollection) this.gridDataBoundGrid1.Binder.InternalColumns.Clone();

GridBoundColumn Ucolumn5 = new GridBoundColumn();

Ucolumn5.HeaderText = "CheckBox";

Ucolumn5.MappingName = "CheckBox";

column5.Add(Ucolumn5);

this.gridDataBoundGrid1.Binder.GridBoundColumns = column5;

....

}

private void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)

{

GridModel model = this.gridDataBoundGrid1.Model;

int ColIndex = this.gridDataBoundGrid1.Model.NameToColIndex("CheckBox");

if(e.RowIndex > 0 && e.ColIndex == ColIndex)

{

e.Style.CellType = "CheckBox";

e.Style.HorizontalAlignment = GridHorizontalAlignment.Center;

e.Style.VerticalAlignment = GridVerticalAlignment.Middle;

e.Style.CellValueType = typeof(bool);

e.Style.CheckBoxOptions.CheckedValue = "True";

e.Style.CheckBoxOptions.UncheckedValue = "False";

e.Style.Enabled = true;

int keyColIndex = model.NameToColIndex("Product_ID");

string key = model[e.RowIndex, keyColIndex].Text;

if (key != null)

{

object value = CheckBoxValues[key];//Display the value in a cell by retrieving it from HashTable

if (value != null)

e.Style.CellValue = value;

}

}

}

private void Model_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e)

{

GridModel model = this.gridDataBoundGrid1.Model;

int ColIndex = this.gridDataBoundGrid1.Model.NameToColIndex("CheckBox");

if(e.RowIndex > 0 && e.ColIndex == ColIndex)

{

int keyColIndex = model.NameToColIndex("Product_ID");

string key = model[e.RowIndex, keyColIndex].Text;

if (key != null)

CheckBoxValues[key] = e.Style.CellValue;//Save the value in a HashTable

}

}

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)

...

GridBoundColumnsCollection column5 = CType(Me.gridDataBoundGrid1.Binder.InternalColumns.Clone(), GridBoundColumnsCollection)

Dim Ucolumn5 As GridBoundColumn = New GridBoundColumn()

Ucolumn5.HeaderText = "CheckBox"

Ucolumn5.MappingName = "CheckBox"

column5.Add(Ucolumn5)

Me.gridDataBoundGrid1.Binder.GridBoundColumns = column5

....

End Sub

Private Sub Model_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs)

Dim model As GridModel = Me.gridDataBoundGrid1.Model

Dim ColIndex As Integer = Me.gridDataBoundGrid1.Model.NameToColIndex("CheckBox")

If e.RowIndex > 0 AndAlso e.ColIndex = ColIndex Then

e.Style.CellType = "CheckBox"

e.Style.HorizontalAlignment = GridHorizontalAlignment.Center

e.Style.VerticalAlignment = GridVerticalAlignment.Middle

e.Style.CellValueType = GetType(Boolean)

e.Style.CheckBoxOptions.CheckedValue = "True"

e.Style.CheckBoxOptions.UncheckedValue = "False"

e.Style.Enabled = True

Dim keyColIndex As Integer = model.NameToColIndex("Product_ID")

Dim key As String = model(e.RowIndex, keyColIndex).Text

If Not key Is Nothing Then

Dim value As Object = CheckBoxValues(key) 'Display the value in a cell by retrieving it from HashTable

If Not value Is Nothing Then

e.Style.CellValue = value

End If

End If

End If

End Sub

Private Sub Model_SaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs)

Dim model As GridModel = Me.gridDataBoundGrid1.Model

Dim ColIndex As Integer = Me.gridDataBoundGrid1.Model.NameToColIndex("CheckBox")

If e.RowIndex > 0 AndAlso e.ColIndex = ColIndex Then

Dim keyColIndex As Integer = model.NameToColIndex("Product_ID")

Dim key As String = model(e.RowIndex, keyColIndex).Text

If Not key Is Nothing Then

CheckBoxValues(key) = e.Style.CellValue 'Save the value in a HashTable

End If

End If

End Sub

Here is a sample that illustrates this:

http://help.syncfusion.com/support/samples/KB/Grid.Windows/UnboundCheckboxColumn/UnboundCheckBoxColumn.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied