user679530
user679530

Reputation: 917

Column Names in DataGrid

I have a datagrid say Grid1 and I have a datatable called Dt in the codebehind where the column names and data will be changing always. I am giving the itemssource as shown below

Grid1.ItemsSource=Dt.DefaultView;

In this case if I dont have any rows in the datatable but it just has column names but still I need to show up the column names in the datagrid.

Upvotes: 2

Views: 933

Answers (1)

Rick Sladkey
Rick Sladkey

Reputation: 34240

The way the DataGrid works is to infer the automatic columns from the row data itself. If there are no rows, it doesn't generate any columns!

You can work around this problem by simply adding an empty row when the table does not have any rows:

if (Dt.Rows.Count == 0)
    Dt.Rows.Add(Dt.NewRow());
Grid1.ItemsSource = Dt.DefaultView;

If you don't want to modify the original table you can create a copy first with DataTable.Copy.

Upvotes: 1

Related Questions