Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Linq to SQL - update date within a DataGridView
 

Linq to SQL - update date within a DataGridView

Hi,

I've got an DataGridView bound to a Linq DataSource.
All I try is to submit changed Values of a DataRow of this DataGridView to the DataBase on a RowLeave.

Here's my code:

private void Form1_Load(object sender, EventArgs e)
{
  this.dataContext = new DataContext("Connectionstring");

  Table<Contact> allContacts = dataContext.GetTable<Contact>();

  var contacts =
     from contact in allContacts
     select contact;

  this.dataGridView1.DataSource = contacts;

  this.ready = true;
}

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
  if (!ready)
  return;

  dataContext.SubmitChanges();
}



To display the Data works fine.
But the update of the Data just happens on the second RowLeave.

Does anyone know why an where the mistake is hidden.

Thanks,
Jan
  • Moved byeryangMSFTMonday, August 10, 2009 8:47 AM (From:.NET Base Class Library)
  • Edited byJanBaa Tuesday, August 11, 2009 6:16 AM
  • Edited byJanBaa Tuesday, August 11, 2009 6:18 AM
  • Edited byJanBaa Tuesday, August 11, 2009 6:19 AM
  •  
JanBaa  Friday, August 07, 2009 9:14 AM
Yes,

and I figured out why.
The value in the DataGridViewCell is only changed in the surface.
If I read the value in the eventhandler just before calling SubmitChanges() the value hasn't been changed.

So it seems to bee an problem in the grid.
The RowLeave-Event accoures bevor the DirtyFlag for the cell is set.

So I just switched to the CellValueChanged-Event - e voila, it works perfectly.

Regards,
Jan
  • Marked As Answer byJanBaa Monday, August 17, 2009 11:49 AM
  •  
JanBaa  Monday, August 17, 2009 11:49 AM
Hi JanBaa,

What is the "ready" in your if statement mean? RowLeave event occurs when a row loses input focus and is no longer the current row. so dataContext.SubmitChanges(); should be run when you leave the current row. Please delete if statement.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 11, 2009 2:52 AM
Hi,

thanks for your reply.
The Ready-Flag is to prevent the first RowLeave - after the form is loaded the RowLeave Event fires without any user activity.
After that the flag is always true.

So that might be not the problem.

Regards,
Jan
JanBaa  Tuesday, August 11, 2009 6:16 AM
Hi JanBaa,

Yes, you are right. It is a clever way to put that flag. I will try to clarify the issue.

After the form load complete, when you first step to another row, the RowLeave event doesn't fired. Then the next time you step to another row, it fire. And it works properly from now on . Is it right?

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, August 13, 2009 7:24 AM
Hi Kira,

I have to disapoint you.
The RowLeave-Event fires everytime I change the selected row in the DatagridView.
That means everytime the user change the selected row the methode SubmitChanges() is called.
But for some reason changes are only written to the database after a second time changing the row.

I changed the way calling SubmitChanges(). If I call it on a ButtonClick, it works fine.

I dont have any idea ...

Regards,
Jan


JanBaa  Thursday, August 13, 2009 7:50 AM
Hi,

So you mean the SubmitChanges() method is run but the changes are not submit at first time?

Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, August 13, 2009 7:54 AM
Yes,

and I figured out why.
The value in the DataGridViewCell is only changed in the surface.
If I read the value in the eventhandler just before calling SubmitChanges() the value hasn't been changed.

So it seems to bee an problem in the grid.
The RowLeave-Event accoures bevor the DirtyFlag for the cell is set.

So I just switched to the CellValueChanged-Event - e voila, it works perfectly.

Regards,
Jan
  • Marked As Answer byJanBaa Monday, August 17, 2009 11:49 AM
  •  
JanBaa  Monday, August 17, 2009 11:49 AM
hi all i am getting the old value on cellchangevalue event also. pls let me know how to resolve it, thanks
Neeraj Bajaj  Wednesday, October 07, 2009 7:53 AM

You can use google to search for other answers

Custom Search

More Threads

• Option to cancel changes when moving to next record
• Custom DataGridViewCell Paint - Unreliable OwningRow.DataBoundItem
• Quickly lock & unlock lots of fields for editing - best way/practice
• CaptionText in DataGridView
• I had to implement additional data sources for DataGridView
• How to set DataGridViewComboBoxColumn to display nothing ?
• DataGridView: changing DataMember displays no data, though the data is 'there'
• Bind DataTable to GridView problem
• BindingSource (Filter property)
• Having problems nesting a datagridview....is it even possible.. I am complete newbiew to winforms as I have always devloped in asp.net