Search code examples
wpfvb.netwpfdatagrid

WPF + Data Grid Binding using DataContext


I want to bind a data grid view. After binding, I need to hide a column from the datagrid. My code is:

datagrid.DataContext = Dttable  

For each gdColumn as DataGridColumn in datagrid.Columns

    If gdColumn.Header = "ID" Then
        gdColumn.Visiblity = False
    end if

Next

But here datagrid.Columns is 0 even though I have 3 columns.

Please help me to do this


Solution

  • You seem to be doing hiding right after you bind the data. Handle the datagrid.Loaded event and do the rest of your code there.

    WPF control's Loaded event is triggered after DataBinding is complete.