【发布时间】:2011-08-14 13:53:42
【问题描述】:
我创建了一个 DataTable 并将它绑定到一个 DataGrid。我的数据源由一个表 (FooTable) 组成,该表由一列 (FooName) 组成。
以下代码运行良好 - 除了每次添加新行时,都会使用相同的数据填充重复的列,我不知道如何摆脱。请参见下面的图像和代码。我只有一个 FooName 列,并且出现了一个重复的列。
/* Create a DataGrid dg1 */
DataGrid dg1 = new DataGrid();
DataGridTextColumn col = new DataGridTextColumn();
col = new DataGridTextColumn();
colA.Binding = new Binding("FooName");
colA.Header = "FooName";
dg1.Columns.Add(colA);
dataGrid1.Children.Add(dg1);
/* Create a DataTable and bind it to DataGrid */
SqlCeDataAdapter da = new SqlCeDataAdapter();
string sqlStr = @"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(sqlStr, conn);
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Donkey";
dt.Rows.Add(newRow);
dg1.ItemsSource = ds.Tables[0].DefaultView;
【问题讨论】: