Articles in this section
Category / Section

How to style rows and cells conditionally based on data in GridTreeControl

1 min read

You can apply the style for the rows and cells based on some conditions by using the QueryCellInfo event like below,

Style for Rows:

C# 

void treeGrid_ModelLoaded(object sender, EventArgs e)
{
    treeGrid.Model.QueryCellInfo += Model_QueryCellInfo;
}
 
void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
    int nodeindex = treeGrid.InternalGrid.ResolveIndexToColumnIndex(e.Style.ColumnIndex);
 
    if (nodeindex > -1)
    {
            //Get the node
            var node = treeGrid.InternalGrid.GetNodeAtRowIndex(e.Style.RowIndex);
 
            if (node != null)
            {
                var item = node.Item as EmployeeInfo;
 
                if (item != null)
 
                    if (item.EmpID == 1002)
                        e.Style.Background = Brushes.Red;
 
                    else if(item.EmpID==1004)
                        e.Style.Background = Brushes.Blue;
 
                    else if (item.EmpID == 1012)
                        e.Style.Background = Brushes.Yellow;
 
                    else if (item.EmpID == 1005)
                        e.Style.Background = Brushes.Orange;
            }
 
        }
    } 

 

Rows in Grid Tree control

Figure 1: Conditional formatting for rows in GridTreeControl

Style for Cells:

C#

void treeGrid_ModelLoaded(object sender, EventArgs e)
{
    treeGrid.Model.QueryCellInfo += Model_QueryCellInfo;
}
 
void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
    int nodeindex = treeGrid.InternalGrid.ResolveIndexToColumnIndex(e.Style.ColumnIndex);
 
    if (nodeindex > -1)
    {
        if (treeGrid.Columns[nodeindex].MappingName != "EmpID")
            return;
 
        else
        {
            //Get the node
            var node = treeGrid.InternalGrid.GetNodeAtRowIndex(e.Style.RowIndex);
              if (node != null)
                {
                    var item = node.Item as EmployeeInfo;
 
                    if (item != null)
 
                        if (item.EmpID == 1002)
                            e.Style.Background = Brushes.Red;
 
                        else if (item.EmpID == 1004)
                            e.Style.Background = Brushes.Blue;
 
                        else if (item.EmpID == 1012)
                            e.Style.Background = Brushes.Yellow;
 
                        else if (item.EmpID == 1005)
                            e.Style.Background = Brushes.Orange;
                }
          }
 
      }
    }

 

Graphical user interface, table

Figure 2: Conditional formatting for cells in GridTreeControl

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