Datagrid Refresh Problem

C

copyco

I'm having trouble with a datagrid. The problem is that after updating
my table with the command object, the changes to the table are not being
reflected in the datagrid. I have determined that before I can refill
the dataset, the changes to the table haven't actually taken place yet,
so the old data goes back into the datagrid. The only work-around to
this problem is to put the application to sleep long enough for the
command object to work, then after the sleep period, the dataset can be
filled with the new and changed data. Is there a more tidy, less sloppy
way of dealing with this problem? BTW this is a windows form datagrid,
not a webpage datagrid. The code is below.


cmNumbers.CommandText = "insert into numbers " & _
"([number], comName, callCount, lastCall) values " & _
"('" & comNum & "', '" & comName & "', 1, '" & vNow & "')"
cmNumbers.ExecuteNonQuery()

'** without the following line, my datagrid doesn't refresh!!
System.Threading.Thread.Sleep(500)

dsNumbers.Clear()
daNumbers.Fill(dsNumbers, "Numbers")
 
S

srinivas moorthy

Dont do a Clear on DataSet. Directly fill that with the
SqlDataAdapter.
That should work

thanks
srinivas Moorthy
 
P

Pete Wright

Just a wild stab in the dark, but don't forget that anytime you post changes
to the database from your dataset, two things need to happen

1. You need to call AcceptChanges after hitting the database, if you don't
refresh the dataset(which you do)
2. Second, you need to , you must, you have no choice, but to rebind the
grid. Just put a couple of lines in to rebind the dataset to the grid after
the execute and you should find it all works.

Good luck

--
Peter Wright
Author of ADO.NET Novice To Pro, from Apress Inc.


_____________________________
 
P

Pete Wright

My pleasure. Glad you got it working.

--
Peter Wright
Author of ADO.NET Novice To Pro, from Apress Inc.


_____________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top