DataGrid Column count is 0 when loading dataview for the first time

Aug 18, 2014 at 9:37 AM
I am using mui wpf templates. I have a datagrid which is binding to a dataview object inside
OnNavigatedTo() event (present in IContent interface). My code is something looks like below

public void OnNavigatedTo(NavigationEventArgs e)
datagrid1.ItemSource = dataviewObj ;

datagrid1.Columns[0].Visibility = Visisbility.Hidden ;
datagrid1.Columns[1].Visibility = Visisbility.Hidden ;
datagrid1.Columns[2].Visibility = Visisbility.Hidden ;


My problem is when this page is loading for the very 1st time then ,
datagrid1.Columns[0].Visibility = Visisbility.Hidden ; this line gives as error as index out of bound exception.

But if I navigate to any other page and revert back to again this page after that no error is comming and these three lines executing fine. Basically I want to hide some columns at run time after binding to gridview.

I have tried a lot , How to achieve this please advice.