How to check whether a column exists in XamDataGrid or not?

179 Views Asked by At

I am pretty new to Xaml. I want to hide one column "Id" from my XamDataGrid which is bound to a datatable in viewmodel. I tried the below approach:

private void xamDataGrid1_FieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)
{
    xamDataGrid1.FieldLayouts[0].Fields["ID"].Visibility = Visibility.Collapsed;
}

But when I run the code first time , I get the below Exception :

System.ArgumentException: 'key not found'

Is there any way to check if the Field["Id"] exists or not before running the above code ?

1

There are 1 best solutions below

0
Sagar Chopra On

The below one worked for me :

private void XamDataGrid_RecordsInViewChanged(object sender, Infragistics.Windows.DataPresenter.Events.RecordsInViewChangedEventArgs e)
        {
int count = 0;
          
                count = XamDataGrid.DefaultFieldLayout.Fields.Count(field => field.Name.CIeq("Id"));

                if (count > 0)
                    XamDataGrid.DefaultFieldLayout.Fields["Id"].Visibility = Visibility.Collapsed;
            
}