当我使用Developer Express 的GridControl控件时,使用下面代码,一切都很好用,但是当我像使用
SqldataAdapter.Update(dataSetName,tableName)更新数据库时 却没有反映;而相同的代码放到微软的DataGrid控件中时却没有毛病。情高手指点啊!谢谢! private void InitData()
{
string sqlString ="SELECT MB, Description AS 描述 FROM MB";
SqlConnection sqlConnection = new SqlConnection(Global.connectonString);
sqlDataAdapter = new SqlDataAdapter(sqlString,sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlConnection.Open();
dataSet_MB = new DataSet();
sqlDataAdapter.Fill(dataSet_MB,"MB");
gridControl1.DataSource = dataSet_MB.Tables["MB"];
sqlConnection.Close();
}
SqldataAdapter.Update(dataSetName,tableName)更新数据库时 却没有反映;而相同的代码放到微软的DataGrid控件中时却没有毛病。情高手指点啊!谢谢! private void InitData()
{
string sqlString ="SELECT MB, Description AS 描述 FROM MB";
SqlConnection sqlConnection = new SqlConnection(Global.connectonString);
sqlDataAdapter = new SqlDataAdapter(sqlString,sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlConnection.Open();
dataSet_MB = new DataSet();
sqlDataAdapter.Fill(dataSet_MB,"MB");
gridControl1.DataSource = dataSet_MB.Tables["MB"];
sqlConnection.Close();
}
XtraGridPosting Data to a Connected Database
[CSHARP]
Language
○ C#
○ Visual Basic
○ Show All
The .Net data model implies that a control bound to a database using DbDataAdapter and DataTable objects does not immediately post changes after they have been made. Instead, changes are accumulated in the DataTable and you have to manually call a specific method to update the database. The XtraGrid behaves in a similar way: changes you made while editing data (adding, deleting or modifying records) are saved in a corresponding DataTable. To post these changes to the database, you should call the data adapter's System.Data.Common.DbDataAdapter.Update method.
Before calling this method, make sure that the grid control has saved all the changes made to the currently focused row (the end-user could enter new data but forget to update the row). For this purpose, you should call the BaseView.CloseEditor and ColumnView.UpdateCurrentRow methods.
The following code lists the UpdateDatasource method which posts the changes stored in the custom Suppliers and Products DataTables to a database. It is assumed that the data adapters (oleDbDataAdapter1 and oleDbDataAdapter2) contain appropriate Update SQL statements to modify the corresponding tables in the database. By default, these statements are created by the adapter's Wizards.
The dataSet11 object represents a DataSet instance and this provides access to our tables.
我用了事务处理,目的是为了能在出错时回滚,想简单使用sqlCommandBuilder生成命令
结果出错,后来我用SqlDataAdapter向导生成就行了(当然得有主键保证各命令成功生成),
不想用向导生成数据集,最后只好自己手动写各命令的Command,CommandText你可以先用向导生成,然后复制一下就行了,我有回答对你一定有
#region update
public void UpdateDatasource(GridControl grid)
{
//Save the latest changes to the bound DataTable
ColumnView view = (ColumnView)grid.DefaultView;
view.CloseEditor();
if(!view.UpdateCurrentRow()) return; //Update the database's Suppliers table to which oleDBDataAdapter1 is connected
DoUpdate(oleDbDataAdapter1, dataSet_Item1.Tables["Item"]);
} public void DoUpdate(DbDataAdapter dataAdapter, System.Data.DataTable dataTable)
{
try
{
dataAdapter.Update(dataTable);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
我有一次也出了问题,那是因为自己不小心,那Dataset不在同一个类中
如果你用向导生成SqlDataAdapter和DataSet一定没问题