当DataTable为空时,DataGrid显示一个空行 [英] DataGrid show an empty row when DataTable is empty

查看:361
本文介绍了当DataTable为空时,DataGrid显示一个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid dg1 )绑定到一个 DataTable DataSet.Tables )。
代码运行正常, DataGrid 正确显示 DataTable 中的数据。

I have a DataGrid (dg1) that binds to a DataTable (DataSet.Tables). The code runs fine and DataGrid is showing the Data in DataTable correctly.

但是,如果我 Clear() DataTable DataGrid 也是清楚的,但留下了一个空行,我不知道如何摆脱。我已经清除了DataTable。这个空行来自哪里?

But, if I Clear() the DataTable, the DataGrid is also clear but left with one single empty row, which I don't know how to get rid of. I have already cleared the DataTable. Where is this empty row come from?

SqlCeDataAdapter da = new SqlCeDataAdapter();

string sqlStr = @"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(sqlStr, conn);

da.Fill(ds, "FooTable");

/* get data table reference */
dt = ds.Tables["FooTable"];

DataRow newRow = dt.NewRow();
newRow["FooName"] = "Donkey";
dt.Rows.Add(newRow);

dg1.ItemsSource = ds.Tables[0].DefaultView;
dt.Clear();

推荐答案

该行通常是NewItemPlaceholder,用于在绑定中创建新项采集。您应该尝试设置 CanUserAddRows false

That row normally is the NewItemPlaceholder which is used to create new items in the bound collection. You should try setting CanUserAddRows to false.

这篇关于当DataTable为空时,DataGrid显示一个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆