C# 添加行时数据表中存在重复列

C# 添加行时数据表中存在重复列,c#,wpf,datatable,C#,Wpf,Datatable,我创建一个DataTable并将其绑定到DataGrid。我的数据源由一个表(FooTable)组成,该表由一列(FooName)组成 下面的代码运行良好-除了每次添加新行时,都会有一个重复的列填充相同的数据,我不知道如何消除这些数据。请参阅下面的图片和代码。我只有一个FooName列,并且出现了一个重复的列 /* Create a DataGrid dg1 */ DataGrid dg1 = new DataGrid(); DataGridTextColumn col = new DataGr

我创建一个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;


尝试将AutoGenerateColumns设置为false

 dg1.AutoGenerateColumns = false
试一试


我应该为你做这项工作。现在datagrid会自动生成列并添加你问的列

哈哈,比我早34秒,不错的一个:DYet另一个快速绘制问题。
dg1.AutoGenerateColumns = false;