每个datagridview的Dataadapter都不起作用

时间:2016-05-25 03:23:45

标签: vb.net winforms data-binding datagridview dataadapter

我有3个datagridviews

  

datagridview1,datagridview2,datagridview3

每个datagridview使用不同的数据适配器和不同的绑定进行绑定

  

datapter1,dataadapter2,dataadapter3,binding1 binding2和binding3

'binding dgv1 
datapter1.SelectCommand = mycmd
datapter1.Fill(Dset, "something")
binding1.DataSource = Dset.Tables("something1")
datagridview1.DataSource = binding1

'binding dgv2 
datapter2.SelectCommand = mycmd
datapter2.Fill(Dset, "something")
binding2.DataSource = Dset.Tables("something2")
datagridview2.DataSource = binding2

'binding dgv3 
datapter3.SelectCommand = mycmd
datapter3.Fill(Dset, "something")
binding3.DataSource = Dset.Tables("something3")
datagridview3.DataSource = binding3

当我想更新datagridview1时

Dim firstbuilder As New MySqlCommandBuilder(Me.dataadapter1)
Me.dataadapter1.Update(Me.binding1.DataSource)
Me.binding1.ResetBindings(False)

它提示我错误

  

在datagridview3'的DataTable'中找不到Datagridview3'中的DataCridumn'位于datagridview3'的源列'

我不明白为什么我有这个错误,因为我为每个datagridview使用了不同的绑定和不同的dataadapter。 需要一些帮助才能解决问题...

1 个答案:

答案 0 :(得分:1)

几年前我几天都在努力解决同样的问题...我最终创建了一个自定义类。基本上我为每个DataTable创建了单独的DataAdapter和-Commandbuillder。不确定这是否是最有效的方式,但它确实有效:

'1 Dataset for the full DB
Public dbDataSet As New DataSet With {.CaseSensitive = False}

'1 Datatable, Dataadapter en Commandbuilder per TABLE
Private Listof_Datatables As New List(Of DataTable)
Private Listof_Dataadapters As New List(Of OleDbDataAdapter)
Private Listof_Command As New List(Of OleDbCommand)
Private Listof_Commandbuilder As New List(Of OleDbCommandBuilder)

Public Sub Load_Table(tablename As String)
    'Load the AccessTable to the DataSet
    Dim SQLstring As String = "SELECT * FROM " & tablename.ToUpper
    Dim dbCon As New OleDbConnection(_DBconnString_full)
    dbCon.Open()
    Listof_Dataadapters.Add(New OleDbDataAdapter)
    Listof_Command.Add(New OleDbCommand(SQLstring, dbCon))
    Listof_Dataadapters(Listof_Dataadapters.Count - 1).SelectCommand = Listof_Command(Listof_Command.Count - 1)
    Listof_Commandbuilder.Add(New OleDbCommandBuilder(Listof_Dataadapters(Listof_Dataadapters.Count - 1)))
    Listof_Commandbuilder(Listof_Commandbuilder.Count - 1).QuotePrefix = "["
    Listof_Commandbuilder(Listof_Commandbuilder.Count - 1).QuoteSuffix = "]"
    Listof_Dataadapters(Listof_Dataadapters.Count - 1).Fill(dbDataSet, tablename)
    Listof_Datatables.Add(dbDataSet.Tables(tablename))

    'Get primary keys
    Dim UCollist As New List(Of String)
    Dim mySchema As DataTable = CType(dbCon, OleDbConnection).GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, New Object() {Nothing, Nothing, tablename})
    Dim columnOrdinalForName As Integer = mySchema.Columns("COLUMN_NAME").Ordinal
    For Each r As DataRow In mySchema.Rows
        UCollist.Add(r.ItemArray(columnOrdinalForName).ToString)
    Next

    'Set primary keys
    Dim keycolumn(UCollist.Count) As DataColumn
    For index = 0 To UCollist.Count - 1
        keycolumn(index) = dbDataSet.Tables(tablename).Columns(UCollist(index))
    Next
    dbDataSet.Tables(tablename).PrimaryKey = keycolumn

    dbCon.Close()
End Sub
Public Sub Update_Tables()
    Dim NrAdap As Integer = Listof_Dataadapters.Count
    For Adapterindex = 0 To NrAdap - 1
        Listof_Dataadapters(Adapterindex).Update(dbDataSet.Tables(Adapterindex))
    Next
End Sub