How can I prevent the Grid from showing the "+" sign next to the parent rows that have no children?
To hide the expansion indicator when no children are present in the parent row of a hierarchical grid, you can handle the CellDrawn in the grid.
In the CellDrawn event handler, you check whether the row has a child list, and if it does, you check if the child list has members. The tricky part is that you can only getthe proper child count if the node is expanded. So, anytime there is an unexpanded node, the code does a 'silent' expand.
C#
private void gridDataBoundGrid1_CellDrawn(object sender, Syncfusion.Windows.Forms.Grid.GridDrawCellEventArgs e)
{
if(e.ColIndex == 1)
{
int count = -1;
if(!this.gridDataBoundGrid1.CurrentCell.IsEditing)
{
GridBoundRecordState state = this.gridDataBoundGrid1.Binder.GetRecordStateAtRowIndex(e.RowIndex);
count = state.ChildCount;
//to get the valid child count, the row has to be expanded so the childlist is populated
//here we do a 'silent' expand
if(!this.gridDataBoundGrid1.IsExpandedAtRowIndex(e.RowIndex))
{
this.gridDataBoundGrid1.BeginUpdate();
this.gridDataBoundGrid1.Model.SuspendChangeEvents();
this.gridDataBoundGrid1.ExpandAtRowIndex(e.RowIndex);
count = state.ChildCount;
this.gridDataBoundGrid1.CollapseAtRowIndex(e.RowIndex);
this.gridDataBoundGrid1.Model.ResumeChangeEvents();
this.gridDataBoundGrid1.EndUpdate();
}
}
if( count == 0
|| e.RowIndex == this.gridDataBoundGrid1.Model.RowCount
|| this.gridDataBoundGrid1.Binder.IsAddNew )
{
Brush brush = new SolidBrush(e.Style.BackColor);
try
{
e.Graphics.FillRectangle(brush,e.Bounds);
e.Cancel = true;
}
finally
{
brush.Dispose();
}
}
}
}
VB
Private Sub gridDataBoundGrid1_CellDrawn(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridDrawCellEventArgs)
If e.ColIndex = 1 Then
Dim count As Integer = -1
If Not Me.gridDataBoundGrid1.CurrentCell.IsEditing Then
Dim state As GridBoundRecordState = Me.gridDataBoundGrid1.Binder.GetRecordStateAtRowIndex(e.RowIndex)
count = state.ChildCount
'to get the valid child count, the row has to be expanded so the childlist is populated
'here we do a 'silent' expand
If Not Me.gridDataBoundGrid1.IsExpandedAtRowIndex(e.RowIndex) Then
Me.gridDataBoundGrid1.BeginUpdate()
Me.gridDataBoundGrid1.Model.SuspendChangeEvents()
Me.gridDataBoundGrid1.ExpandAtRowIndex(e.RowIndex)
count = state.ChildCount
Me.gridDataBoundGrid1.CollapseAtRowIndex(e.RowIndex)
Me.gridDataBoundGrid1.Model.ResumeChangeEvents()
Me.gridDataBoundGrid1.EndUpdate()
End If
End If
If if(count = 0 Then
Dim brush As Brush = New SolidBrush(e.Style.BackColor)
Try
e.Graphics.FillRectangle(brush,e.Bounds)
e.Cancel = True
Finally
brush.Dispose()
End Try
End If
End If
End Sub
Here is a sample that illustrates this:
http://websamples.syncfusion.com/samples/KB/Grid.Windows/PreventPlusSign/main.htm