I have a datagrid say Grid1
and I have a datatable called Dt
in the codebehind where the column name开发者_Python百科s 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.
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
.
精彩评论