当 DataTable 为空时,DataGrid 显示空行
我有一个绑定到 DataTable
(DataSet.Tables) 的 DataGrid
(dg1)。 代码运行良好,DataGrid
正确显示 DataTable
中的数据。
但是,如果我 Clear()
DataTable
,DataGrid
也是清晰的,但留下一个空行,我不知道这一点如何摆脱。我已经清除了数据表。这个空行是从哪里来的?
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();
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.
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该行通常是 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
tofalse
.显示的空行可能是由于默认情况下在数据网格中,空行位于最后,以便用户可以添加新行。
尝试做
dataGridView1.AllowUserToAddRows = false;
The empty row that is shown is probably due to the reason that by default in datagrid, empty row is there in the last so that the user can add new row.
try doing
dataGridView1.AllowUserToAddRows = false;
@Abdul Muqtadir:正确的解释。
下面的代码将执行您正在寻找的操作:
对于 Datagrid,您应该将 DataGrid.CanUserAddRows 设置为 false;
@Abdul Muqtadir : Correct explanation.
The below code will do what you are looking for:
for Datagrid you should set DataGrid.CanUserAddRows to false;