How to conditionally set style of a row in WinForms GridGrouping?
Formatting of a row can be applied through either Conditional Formatting or by using QueryCellStyleInfo event in the WinForms GridGroupingControl.
Conditional Formatting
To conditionally set the style of a row in the GridGroupingControl, you can apply the row styles by using the Conditional Formatting function. You can refer to the following code example to apply Conditional Formatting to the records that have the cell value of Row1.
C#
//Creates FormatDescriptor Syncfusion.Windows.Forms.Grid.Grouping.GridConditionalFormatDescriptor gridConditionalFormatDescriptor3 = new Syncfusion.Windows.Forms.Grid.Grouping.GridConditionalFormatDescriptor(); //Conditional Formatting applied through designer gridConditionalFormatDescriptor3.Appearance.AnyRecordFieldCell.Interior = new Syncfusion.Drawing.BrushInfo(System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(191)))), ((int)(((byte)(52)))))); gridConditionalFormatDescriptor3.Appearance.AnyRecordFieldCell.TextColor = System.Drawing.Color.White; gridConditionalFormatDescriptor3.Appearance.AnyRecordFieldCell.Enabled = false; // Applies format by checking the value ‘Row1’ gridConditionalFormatDescriptor3.Expression = "[Name] LIKE \'Row1\'"; gridConditionalFormatDescriptor3.Name = "ConditionalFormat 1"; //Adds conditional format descriptor to the GridGroupingControl TableDescriptor. this.gridGroupingControl1.TableDescriptor.ConditionalFormats.Add(gridConditionalFormatDescriptor3);
VB
'Creates FormatDescriptor Dim gridConditionalFormatDescriptor3 As New Syncfusion.Windows.Forms.Grid.Grouping.GridConditionalFormatDescriptor() 'Conditional Formatting applied through designer gridConditionalFormatDescriptor3.Appearance.AnyRecordFieldCell.Interior = New Syncfusion.Drawing.BrushInfo(System.Drawing.Color.FromArgb((CInt(Fix((CByte(255))))), (CInt(Fix((CByte(191))))), (CInt(Fix((CByte(52))))))) gridConditionalFormatDescriptor3.Appearance.AnyRecordFieldCell.TextColor = System.Drawing.Color.White gridConditionalFormatDescriptor3.Appearance.AnyRecordFieldCell.Enabled = False ' Applies format by checking the value ‘Row1’ gridConditionalFormatDescriptor3.Expression = "[Name] LIKE 'Row1'" gridConditionalFormatDescriptor3.Name = "ConditionalFormat 1" 'Adds conditional format descriptor to the GridGroupingControl TableDescriptor. Me.gridGroupingControl1.TableDescriptor.ConditionalFormats.Add(gridConditionalFormatDescriptor3)
The following screenshot displays the Conditional Formatting applied to the GridGroupingControl.
QueryCellStyleInfo event
To dynamically set the style properties on a cell, the QueryCellStyleInfo event has to be handled. You can access the row of the cell and check whether it is a RecordRow or not. Assuming the datasource as a DataTable, the GridRecordRow.Record.GetData method returns the data as a DataRowView object. Then by using the DataRowView, you can access the data in the row and after checking the conditions, change the style of the row. Refer to the following code example.
C#
// Event triggering from designer this.gridGroupingControl1.QueryCellStyleInfo += new Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventHandler(this.gridGroupingControl1_QueryCellStyleInfo); private void gridGroupingControl1_QueryCellStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs e) { //Disables the row when the value of Name in the same row is Row1. if(e.TableCellIdentity.RowIndex != -1) { GridRecordRow rec = this.gridGroupingControl1.Table.DisplayElements[e.TableCellIdentity.RowIndex] as GridRecordRow; if(rec != null) { // Applies format by checking the value ‘Row1’ DataRowView dr = rec.GetData() as DataRowView; if(dr != null && dr["Name"].Equals("Row1")) { e.Style.Enabled = false; e.Style.BackColor = Color.Azure; } } } }
VB
' Event triggering from designer AddHandler gridGroupingControl1.QueryCellStyleInfo, AddressOf gridGroupingControl1_QueryCellStyleInfo Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.Grouping.GridTableCellStyleInfoEventArgs) 'Disables the row when the value of Name in the same row is Row1. If e.TableCellIdentity.RowIndex <> -1 Then Dim rec As GridRecordRow = TryCast(Me.gridGroupingControl1.Table.DisplayElements(e.TableCellIdentity.RowIndex), GridRecordRow) If rec IsNot Nothing Then ' Applies format by checking the value Row1 Dim dr As DataRowView = TryCast(rec.GetData(), DataRowView) If dr IsNot Nothing AndAlso dr("Name").Equals("Row1") Then e.Style.Enabled = False e.Style.BackColor = Color.Azure End If End If End If End Sub
Samples:
Conclusion
I hope you enjoyed learning about how to conditionally set style of a row in WinForms GridGrouping.
You can refer to our WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!