DataTable & RowChanged Event

G

Guest

I'm sure this question has been beat to death however, I have been unable to
find a reasonable reply.

My problem is this...

While working with the DataGrid control I noticed the DataTable RowChanged
event is being fired whenever I add a new row in the DataGrid however, the
new row I just added to the DataGrid has not been committed to the DataTable
when the RowChanged event is fired. I can see the Row and it's state is
"Added" but it isn't in the DataTable.

The reason I need it to be is I want to perfrom an update to the DataAdapter
as soon as a new record is added to the DataGrid and I am unable to do this
since the DataTable doesn't contain the newly added row. How can I get the
row to be in the DataTable at the time RowChanged event has been called? Is
there a way to force the commit?
 
Y

yonggangwang

Have you used commandbulider to create insert command for dataadapter?
test is your dataadapter
dim CmdBuld as system.data.sqlclient.sqlcommandbulider
cmdbuld = new system.data.sqlcilent.sqlcommandbulider( test)
test.update(your dataset)
 
G

Guest

Is anyone from microsoft able to enlighten me as to why this isn't classified
as a bug and hasn't been fixed by now? The definition for the RowChanged even
says it's called after the Datable has a new row added. If this is not the
case then add a property to the DataTable to force the add prior to calling
Row Changed. Or even put another call out after it has been committed to the
datatable.

I have spent 2 days earching the web for some hope of an answer and so far
no one has one. I have read how you can use the BindingManager.listChanged
event but quess what, it's called simply clicking on the very last row in
your Datagrid and the state is ItemAdded. That is no help.

I read where one guy used a timer on his form and performed the update after
soo many ticks. He put the timer start in the RowChanged event and did the
update in the timers tick event. This would work to a degree but is it
pratical for commercial applications?

Another person performed an insert by using another data object during the
RowChanged event but then he still had to refresh the DataGrid or else the
state of the newly added row was still "Added". If he didn't he would end up
with duplicate records or even errors. This was a little more practical but
you still had the problem of changing the rowSate to "Unchanged" so not to
raise the even again when you updated the "primary key" of the row.

I can only see one solution for this problem and that is to simply add the
row prior to the RowChanged event is called.

If anyone has even a remote chance of providing a working solution I would
be very interested in it.

This one problem with the DataTable & DataGrid will have a major impact in
the useability of our application pending the outcome.
 
G

Guest

Thank you Yong for your reply but I believe you have missed the problem. The
problem is when you are using a Datable that is bound to a Datagrid and you
add a new DataRow using the datagrid. There is an event called RowChanged
that the DataTable calls once a change has been made to the DataTable. If you
look at the DataTable when this event is called the new datarow doesn't
exsist in the DataTable even though the event has been fired and the Row
state is "Added". The problem if not clear is the new DataRow should be
already added to the DataTable at the time the RowChanged event is called.
why it isn't there when it called is beyound me or why it hasn't been fixed I
dont know. I do know this is a big problem in terms of how many are posting
question related to the issuse.

If only Microsoft would address this issuse or someone provide a link to
where they have.
 
A

alex

Chris

I have been faced with the same problem now for many weeks, and
believe me I have searched everywhere and tried everything, but to no
avail, until this morning that is....

This is still a hack, and Microsoft should really do something about
it.... so here is my resolution which works every time me.

' set focus to another control on form
btnOther.Focus()
' then do events
Application.DoEvents

My problem was that the user would make changes to row in datagrid,
not leave that row / cell and press the save button - no changes were
being written back to the table no matter what combination of
..Endedits I used.

But the above code forces the focus away from the datagrid before the
save code gets to work with inspecting the datagrid's datasource and
updating the database.

Hope this helps.

Alex
 
J

jacobya

Try this:

DataGrid1.EndEdit(null, DataGrid1.CurrentRowIndex, false);
DataGrid1.BindingContext[DataGrid1.DataSource,
DataGrid1.DataMember].EndCurrentEdit();
 

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