datagridview的数据源未获得更新

时间:2013-08-27 10:41:22

标签: c# winforms datagridview

我绑定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);

1 个答案:

答案 0 :(得分:1)

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