Articles in this section
Category / Section

How to hide the rows based on condition in Silverlight DataGrid?

1 min read

In SfDataGrid, you can hide the rows based on some condition by using QueryRowHeight event.

 

The following code example explains how to hide the rows based on condition by setting e.Height to zero.

 

C#

//Hook the QueryRowHeight event
this.sfdatagrid.QueryRowHeight += sfdatagrid_QueryRowHeight;
//QueryRowHeight event for hide the rows
void sfdatagrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
{
    var grid = sender as SfDataGrid;
    var rowData = grid.GetRecordAtRowIndex(e.RowIndex);
    var provider = grid.View.GetPropertyAccessProvider();
    var celltext = provider.GetValue(rowData, "ProductName");
    if (celltext != null && celltext.Equals("HardWare"))
    {
        e.Height = 0;
        e.Handled = true;
    }
}

 

When you need to hide/unhide a row at the run time, you can trigger QueryRowHeight event only for particular row by calling SfDataGrid.InvalidateRowHeight and VisualContainer.InvalidateMeasureInfo methods.

 

RowValidating event is triggered while editing a particular row. So that you can change the RowHeight based on edited values by calling SfDataGrid.InvalidateRowHeight and VisualContainer.InvalidateMeasureInfo methods.

 

C#

//Hook the RowValidating event
this.sfdatagrid.RowValidating += sfdatagrid_RowValidating;
// RowValidating event 
void sfdatagrid_RowValidating(object sender, RowValidatingEventArgs args)
{
    sfdatagrid.InvalidateRowHeight(args.RowIndex);
    this.sfdatagrid.GetVisualContainer().InvalidateMeasureInfo();
}

You can bring the items which are hidden into View when property value changes by invalidating the row height by handling RecordPropertyChanged event.

 

C#

//Hook the ItemsSourceChanged event
this.sfdatagrid.ItemsSourceChanged += Sfdatagrid_ItemsSourceChanged;
//ItemsSourceChanged event for unhide the hidden rows
void Sfdatagrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
        {
            if(this.sfdatagrid.View!= null)
                this.sfdatagrid.View.RecordPropertyChanged += View_RecordPropertyChanged;
        }
 
void View_RecordPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var datarowbase = this.sfdatagrid.GetRowGenerator().Items.FirstOrDefault(row => row.RowData == sender);
            if (datarowbase != null && datarowbase.RowIndex > 0)
            {
                sfdatagrid.InvalidateRowHeight(datarowbase.RowIndex);
                this.sfdatagrid.GetVisualContainer().InvalidateMeasureInfo();
            }
        }

 

 

Sample Links:

 

WPF

WRT

SilverLight

UWP

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