0

我绑定 DataGridView 的 DataSource 属性并使用 DataAdaptor 对其进行更新,并对其进行工作。现在我正在尝试将 Excel 文件中的 DataSource 与 DataGridView 的 DataSource 合并,并将这些合并 DataSource 保存在数据库中,2 个数据源正在合并,但是 DataAdaptor 无法在数据库中更新此合并数据源,即使我在更新 DataAdaptor 时没有收到任何错误,这里是合并和保存DataSource的代码

        //Code for Merging DataSources
        byte[] dataTable = GetExcelFileByteArray();
        //ExcelDataTable is of type DataTable
        ExcelDataTable = GetTableDataOfByteArrayOfExcel(dataTable);
        //dgvAllGridView is DataGridVIew
        DataTable _dgvDataTable = this.dgvAllGridView.DataSource as DataTable;

        if (!ExcelDataTable.Columns.Contains("ID"))
            ExcelDataTable.Columns.Add("ID", _dgvDataTable.Columns["ID"].GetType()).SetOrdinal(0);

        _dgvDataTable.Merge(ExcelDataTable, true, MissingSchemaAction.Ignore);
        dgvAllGridView.DataSource = _dgvDataTable;

        //Code for Updatating DataAdaptor
        //DataAdaptor is a OleDBDataAdaptor
        DataAdaptor.UpdateCommand = new OleDbCommandBuilder(DataAdaptor).GetUpdateCommand();
        DataAdaptor.InsertCommand = new OleDbCommandBuilder(DataAdaptor).GetInsertCommand();
        DataAdaptor.Update((DataTable)dgvAllGridView.DataSource);
4

1 回答 1

1

有添加dgvAllGridView.DataSource = _dgvDataTable;最后的代码吗?它将刷新您的 DGV 数据源..

于 2013-08-27T10:48:44.393 回答