开发者

Column Names in DataGrid

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜