)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load unbound checkbox column in a WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: April 30, 2011 |
Last Revised Date: December 24, 2019

Unbound columns

You can add the unbound CheckBox columns to the GridGroupingControl by using the TableDescriptor.UnboundFields.Add() method. This method allows you to add the unbound fields to the GridGroupingControl. Unbound values can be provided in QueryValue event and any changes in the values can be stored back to the data store by handling the SaveValue event. Additionally, you can handle QueryCellStyleInfo event to customize unbound cells individually.

You can save the values elsewhere as the grouping grid does not maintain any data structure to store cell values. Since the values are unbound, they cannot be stored into bound data source too. In this example, a HashTable is used to save the values of unbound column. The example displays an unbound CheckBox column along with other bound columns. Refer to the following steps to have an unbound checkbox column in the GridGroupingControl.

1. Adding an unbound column

C#

//adding unbound column and setting type.
this.gridGroupingControl1.TableDescriptor.UnboundFields.Add("print");
this.gridGroupingControl1.TableDescriptor.Columns["print"].Appearance.AnyRecordFieldCell.CellType ="CheckBox";

VB

'adding unbound column and setting type.
Me.gridGroupingControl1.TableDescriptor.UnboundFields.Add("print")
Me.gridGroupingControl1.TableDescriptor.Columns("print").Appearance.AnyRecordFieldCell.CellType ="CheckBox"

2. Creating Hashtable to store unbound column values.

C#

Hashtable checkboxvalues=new Hashtable();

VB

Dim checkboxvalues As New Hashtable()

3. Declaration of the events

Then QueryValue event is used to edit the values in the checkbox. The SaveValue event is used to save the changes that you made in the checkbox.

C#

//events for saving the checkbox values entered.
this.gridGroupingControl1.TableDescriptor.QueryValue += new FieldValueEventHandler(unboundField_QueryValue);
this.gridGroupingControl1.TableDescriptor.SaveValue += new FieldValueEventHandler(unboundField_SaveValue);
//Getting the checkBox values in a hashtable.
private void unboundField_QueryValue(object sender, FieldValueEventArgs e)
{
    int RecordIndex =this.gridGroupingControl1.Table.PrimaryKeySortedRecords.IndexOf(e.Record);
    if (e.Field.Name == "print" && RecordIndex>=0)
    {
        object value = checkboxvalues[RecordIndex];
        if (value != null)
           e.Value= value;
    }
}
//Setting the checkbox values from the hashtable.
private void unboundField_SaveValue(object sender, FieldValueEventArgs e)
{
    int RecordIndex =this.gridGroupingControl1.Table.PrimaryKeySortedRecords.IndexOf(e.Record);
    if (e.Field.Name == "print" && RecordIndex>=0)
    {
       checkboxvalues[RecordIndex] = e.Value;
    }
}

VB

'events for saving the checkbox values entered.
Private Me.gridGroupingControl1.TableDescriptor.QueryValue += New FieldValueEventHandler(AddressOf unboundField_QueryValue)
Private Me.gridGroupingControl1.TableDescriptor.SaveValue += New FieldValueEventHandler(AddressOf unboundField_SaveValue)
'Getting the checkBox values in a hashtable.
Private Sub unboundField_QueryValue(ByVal sender As Object, ByVal e As FieldValueEventArgs)
    Dim RecordIndex As Integer =Me.gridGroupingControl1.Table.PrimaryKeySortedRecords.IndexOf(e.Record)
    If e.Field.Name = "print" AndAlso RecordIndex>=0 Then
       Dim value As Object = checkboxvalues(RecordIndex)
       If value IsNot Nothing Then
           e.Value= value
       End If
    End If
End Sub
'Setting the checkbox values from the hashtable.
Private Sub unboundField_SaveValue(ByVal sender As Object, ByVal e As FieldValueEventArgs)
    Dim RecordIndex As Integer =Me.gridGroupingControl1.Table.PrimaryKeySortedRecords.IndexOf(e.Record)
    If e.Field.Name = "print" AndAlso RecordIndex>=0 Then
       checkboxvalues(RecordIndex) = e.Value
    End If
End Sub

The following screenshot illustrates the output.

Adding unbound checkbox column

Figure 1: Adding unbound CheckBox column in GridGroupingControl

Samples:

C#: UnboundCheckBoxColumn-C#

VB: UnboundCheckBoxColumn-VB

Reference link: https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/managing-records-and-columns#unbound-columns

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Joakim
Jul 13, 2016
This code is broken. this.gridGroupingControl1.TableDescriptor.UnboundFields.Add("print"); this.gridGroupingControl1.TableDescriptor.Columns["print"].Appearance.AnyRecordFieldCell.CellType ="CheckBox"; Throws a null refrence exception because there is no column named "print". In other words: TableDescriptor.UnboundFields.Add does not add it to TableDescriptor.Columns. Reply
Mahendran Annamalai [Syncfusion]
Jul 14, 2016
Hi Joakim, Sorry for the inconvenience caused. The broken link has been fixed now. Please check it and let me know your concerns. Regards, Mahendran.A
Bratislav Isailovic
Aug 14, 2016

Code won't work for child tables either! I used:

ggC.GetTableDescriptor["Relation"].UnboundFields.Add("print")

next statement fails:

ggC.GetTableDescriptor["Relation"].Columns["print"].Appearance.AnyRecordFieldCell.CellType = "CheckBox";

Object reference not set to an instance of an object.


Reply
Amal Raj Umapathy Selvam [Syncfusion]
Aug 22, 2016

Hi Bratislav,

 

Thanks for using Syncfusion products.

 

Please create a new incident regarding this query for better follow up.

 

You can create the DT incident from the following link.

http://www.syncfusion.com/account/dashboard

 

Regards,

Amal Raj U.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon