Saturday, January 22, 2011

WPF’s DataGrid doesn’t show blank/empty row

Yesterday, I was playing with WPF’s DataGrid and DataBindings. ItemSource of my DataGrid was bound with the ObservableCollection. DataGrid was displaying all the items correctly which were present in my collection but I was facing one issue. The DataGrid was not displaying a blank empty row through which user can add a new record. I had set CanUserAddRows=”True” in my XAML. After spending sometime on the problem I found out that in order to show a blank row for adding a new record, DataGrid needs a constructor which takes no parameter. So I added a constructor of MyCustomObject which takes no parameter as a result DataGrid started displaying a blank row at the end.

4 comments:

  1. Hello Haris,
    can u tell me how i can hide column which has null value while binding to datagrid.

    i am fecthing data from database and then fill dataset and then bind it to datagrid in wpf but i want if there is any column which has null value in dataset then hide that column in Datagrid.
    i am new to wpf ..plz help me.

    ReplyDelete
  2. I don't know any direct way to achieve this i.e. to tell DataGrid that hide the column if it has any null value.

    One way to achieve what you want is by traversing the column values in your dataset and record the columns in which you find any null value. Once you have the list of all the columns which have null values, simply hide those columns from datagrid.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Just wanted to say "Thanks". I was trying to figure out why one datagrid I had was showing the blank row and while all the others were not. Turned out to be what you found: I had no default constructors in the other classes.

    ReplyDelete