Posting Data to a Connected Database

在调用此方法之前,确保网格控件已保存对当前焦点行的所有更改(用户可能输入了新数据但忘记更新行)。为此,需要调用BaseView.PostEditor和ColumnView.UpdateCurrentRow方法。dataSet11对象代表一个DataSet实例,可以访问我们的表格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 need to call the BaseView.PostEditor 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.

C#CopyCode imageCopy Code
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using System.Data.Common;
//...
public void UpdateDatasource(GridControl grid) {
    //Save the latest changes to the bound DataTable
    ColumnView View = (ColumnView)grid.FocusedView;
    if (!(view.PostEditor() && View.UpdateCurrentRow())) return;
    
    //Update the database's Suppliers table to which oleDBDataAdapter1 is connected
    DoUpdate(oleDbDataAdapter1, dataSet11.Tables["Suppliers"]);
    //Update the database's Products table to which the oleDbDataAdapter2 is connected
    DoUpdate(oleDbDataAdapter2, dataSet11.Tables["Products"]);
}

public void DoUpdate(DbDataAdapter dataAdapter, System.Data.DataTable dataTable) {
    try {
        dataAdapter.Update(dataTable);
    } catch(Exception ex) {
        MessageBox.Show(ex.Message);
    }
}

Visual BasicCopyCode imageCopy Code
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports System.Data.Common
'...
Public Sub UpdateDatasource(ByVal grid As GridControl)
    'Save the latest changes to the bound DataTable
    Dim View As ColumnView = grid.FocusedView
    If Not (view.PostEditor() And View.UpdateCurrentRow()) Then Return

    'Update the database's Suppliers table to which oleDBDataAdapter1 is connected
    DoUpdateTable(oleDbDataAdapter1, dataSet11.Tables("Suppliers"))
    'Update the database's Products table to which the oleDbDataAdapter2 is connected
    DoUpdateTable(oleDbDataAdapter2, dataSet11.Tables("Products"))
End Sub

Public Sub DoUpdateTable(ByVal dataAdapter As DbDataAdapter, ByVal dataTable As System.Data.DataTable)
    Try
        dataAdapter.Update(dataTable)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值