I have a solution with multiple datagridviews (DGV), and now multiple bindingsources(BSs) and OleDbDataAdapters (ODA).
This stems from the fact that I have multiple DGV in a tab control, and I want to show different tables from a database in each DGV, as well as be able to CRUD back from the DGV.
Using one BS and ODA, I was able to show all the correct tables in the correct DGV by switching BS.DataMember
when I entered each tab. This worked because I only had one DGV per tab.
Where I ran into problems was going back into the database. I've found that most examples I've found involve using a BS and ODA for each table. This seems inelegant, so if there was a way to bind the DataSource
of a DGV to a particular BS.Datamember
I would love to know it.
Having acquiesced to multiple BS and ODA, I can pull the DGV, table name, and BS needed to update any dirty rows on any of the DGV from one Sub. What I haven't figured out is if there is a way to get the ODA that was used to fill the table originally. In Code:
Private Sub UniversalDGV_Handler_LeaveRow(sender As Object, e As DataGridViewCellEventArgs) _
Handles DGV1.RowLeave, DGV2.RowLeave, '...' DGVn.RowLeave
Dim DGV As DataGridView = CType(sender, DataGridView)
Dim bsPass As BindingSource = DGV.DataSource
Dim CurrentDBTableName As String = bsPass.DataMember
Dim da as OleDbDataAdapter = 'something I haven't figured out yet
UniversalDGV_RowLeave(sender, CurrentDBTableName, e, bsPass, da)
End Sub
Now in reality, the largest number of DGV I have is 7, so I've just made a matched list and pulled the ODA out that way. But again that seems very crude.
Is there a way to retrieve which DataAdapter
was used from the table it filled, DataSet
it was used on, [the BS, or DGV]?
Info from the BS or DGV seems kind of far fetched because of they don't have direct interaction with the ODA from my understanding. I was also thinking of possibly storing the information in a dataset as I used the adapters, which would at least be a more elegant list/table.
I found the answer by carefully going through properties of the DataTable Class .
By using DataTable.ExtendedProperties()
you can store the DataAdapter used when filling the table. Code sample:
Dim ds as DataSet
Dim BS1 as New BindingSource
Dim da1, da2, da3 as New OleDbDataAdapter
Private Sub DoSomething(da as OleDbDataAdapter)
ds.Tables.Add(New DataTable(DBTableName))
ds.Tables(DBTableName).ExtendedProperties("DataAdapter", da)
End Sub
Private Sub DoesSomethingElse() handles DGV1.Rowleave, DGV2.RowLeave, '...' DGVn.RowLeave
Dim DGV As DataGridView = CType(sender, DataGridView)
Dim bsPass As BindingSource = DGV.DataSource
Dim CurrentDBTableName As String = bsPass.DataMember
Dim RecalledDA as OleDbDataAdapter = ds.Tables(CurrentDBTableName).ExtendedProperties("DataAdapter")
'Do something with it. In my case I have a seperate sub I run it all into'
UniversalDGV_RowLeave(sender, CurrentDBTableName, e, da)
End Sub
Since I have no formal training maybe everyone else knew that, but I hope I can save someone all the hours to figure that out.
With this you can use one BS for all your DGV, assuming you are only showing one at a time. I think you would still need one BS per DGV if you were showing multiple DGV that required BS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.