新行添加到数据表 [英] Add new Row to DataTable
问题描述
我有一个的DataGrid
绑定到一个表和一个列(FooTable和FooName)的数据库。
用我下面的代码,我可以绑定的DataGrid
到数据表
并显示数据库数据。但是,当我每次按 DataSet_Add_Click添加一个新行()
,没有被添加到的DataGrid
。不过,我觉得我已经绑定数据表
到的DataGrid
到的ItemsSource
,但加入新行DataTable不添加行的DataGrid。为什么?
公共部分类主窗口:窗口
{
的SqlCeConnection康恩=新的SqlCeConnection();
/ *定义连接字符串* /
串CONNSTRING;
DataGrid中DG1 =新的DataGrid();
DataTable的DT =新的DataTable();
公共主窗口()
{
的InitializeComponent();
CONNSTRING = @......;
的SqlCeConnection康恩=新的SqlCeConnection(CONNSTRING);
conn.Open();
SqlCeDataAdapter DA =新SqlCeDataAdapter();
串sqlStr = @SELECT * FROM FooTable
da.SelectCommand =新SqlCeCommand(sqlStr,康涅狄格州);
的DataSet DS =新的DataSet();
da.Fill(DSFooTable);
DT = ds.Tables [FooTable];
dg1.ItemsSource = ds.Tables;
的DataRow NEWROW = dt.NewRow();
NEWROW [FooName] =玛丽;
dt.Rows.Add(NEWROW);
CreateDataGrid();
}
公共结构DataItem1
{
公共字符串FooName {搞定;组; }
}
私人无效CreateDataGrid()
{
DataGridTextColumn山坳=新DataGridTextColumn();
列=新DataGridTextColumn();
col.Binding =新的绑定(FooName);
col.Header =FooName;
dg1.Columns.Add(COL);
/ * DataGrid1中的XAML存在,是在DataGrid * /
dataGrid1.Children.Add(DG1)的母体;
}
私人无效DataSet_Add_Click(对象发件人,RoutedEventArgs E)
{
的DataRow newRow2 = dt.NewRow();
newRow2 [FooName] =玛丽;
dt.Rows.Add(newRow2);
}
}
PLS张贴在您绑定的ItemsSource的XAML或CS代码。
贵DataSet_Add_Click添加一行到您的数据表?如果是的话,好像是刚刚刷新/绑定你的DataGrid的问题。
当我用数据表和DataGrid我总是用下面的
$ B $工作b //构造函数或init
DT = ds.Tables [FooTable];
//你有初始化的数据表
this.MyView =(BindingListCollectionView)CollectionViewSource.GetDefaultView(this.dt)后编写下一行;
XAML
<数据网格的ItemsSource ={结合MyView的}/>
刷新
this.MyView.Refresh();
编辑:
MyView的是一个属性。
公共BindingListCollectionView MyView的
{
{返回this._myview;}
集合{this._myview =价值; OnPropertyChanged(MyView的);
}
你可以在代码中绑定。
绑定myBinding =新的绑定(MyView的);
myBinding.Source =这一点; //将与MyView的财产
mydatagridctrl.SetBinding实例(ItemsControl.ItemsSourceProperty,myBinding);
I have a DataGrid
bind to a database with one Table and one Column (FooTable and FooName).
With my following code, I can bind DataGrid
to DataTable
and display database data. But when each time I add a new row by DataSet_Add_Click()
, nothing gets added to the DataGrid
. I though I have bind DataTable
to DataGrid
through ItemsSource
, but adding new row to DataTable doesn't add row to DataGrid. Why?
public partial class MainWindow : Window
{
SqlCeConnection conn = new SqlCeConnection();
/* Define the Connection String */
string connString;
DataGrid dg1 = new DataGrid();
DataTable dt = new DataTable();
public MainWindow()
{
InitializeComponent();
connString = @"...";
SqlCeConnection conn = new SqlCeConnection(connString);
conn.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter();
string sqlStr = @"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(sqlStr, conn);
DataSet ds = new DataSet();
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];
dg1.ItemsSource = ds.Tables;
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Mary";
dt.Rows.Add(newRow);
CreateDataGrid();
}
public struct DataItem1
{
public string FooName { get; set; }
}
private void CreateDataGrid()
{
DataGridTextColumn col = new DataGridTextColumn();
col = new DataGridTextColumn();
col.Binding = new Binding("FooName");
col.Header = "FooName";
dg1.Columns.Add(col);
/* dataGrid1 exist in XAML and is a parent of the DataGrid */
dataGrid1.Children.Add(dg1);
}
private void DataSet_Add_Click(object sender, RoutedEventArgs e)
{
DataRow newRow2 = dt.NewRow();
newRow2["FooName"] = "Mary";
dt.Rows.Add(newRow2);
}
}
pls post your xaml or cs code where you bind the Itemssource.
does your DataSet_Add_Click add the row to your datatable? if yes then it seems is just a refresh/binding problem with your datagrid.
when i work with datatables and datagrid i always use the following
//ctor or init
dt = ds.Tables["FooTable"];
//the next line you have to write after you initialize your datatable
this.MyView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.dt);
XAML
<DataGrid ItemsSource="{Binding MyView }"/>
Refresh
this.MyView.Refresh();
EDIT: MyView is a property
public BindingListCollectionView MyView
{
get {return this._myview;}
set {this._myview = value; OnPropertyChanged("MyView");
}
you can do binding in code.
Binding myBinding = new Binding("MyView");
myBinding.Source = this;//the instance with the MyView property
mydatagridctrl.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
这篇关于新行添加到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!