Refresh DataGridView's Datasource

KriZa

Member
Joined
Jun 22, 2007
Messages
21
Programming Experience
3-5
hello,

got a serious problem. following code:
VB.NET:
dt = New DataTable
da = New MySqlDataAdapter("SELECT ID, corr_KundenNr, ... FROM table1 WHERE corr_KundenNr = '" & Kundennummer & "'", conn)
da.Fill(dt)
cb = New MySqlCommandBuilder(da)
dt.Columns("corr_KundenNr").DefaultValue = Kundennummer
DGV.DataSource = dt

now I change values in the DG an doing on every RowValidated event:
VB.NET:
DGV.EndEdit()
Dim changes As DataTable = dt.GetChanges()
If Not changes Is Nothing Then
    da.Update(changes)
    dt.AcceptChanges()        
End If
Me.Refresh()

works fine. BUT if I insert a new row directly in my DB, it won't be shown in my DGV. So the commandbuilder only does a UPDATE..., and there is no SELECT... again to get updated DB-values. How to do that? I tried da.Fill(dt) but that adds the queryresult whitout clearing the existing. So I tried DGV.Rows.clear() but I get an errormessage, that I can#t use it. Is there an other solution?

thx
 
:D:D:D

AAARGH!!!

recognized my mistake!

reconstructed it step by step...

do not try to clear the DGV - makes no sense at all, cause of the datasource binding. clearing the dataource will succeed.
solution:
VB.NET:
dt.clear()
da.fill(dt)

abrakadabra - your query 'requeries'
 
Youre using .NEt 2.0. You should be doing data access the new way as described in the DW2 link in ym signature. Never form SQL queries like you have there, read the PQ link in my signature for why...
 
MySQL does support parametersed queries, but I really not sure what the format is..

I do have a vauge memory that it would look like this:

SELECT * FROM myTable WHERE name = ?name and age = ?age


But i dont know where or why I know this.. Neither do i know if ou add parameters like:
Params.AddWithValue("name", "Fred")

or like:
Params.AddWithValue("?name", "Fred")


Try both?!
 
Back
Top