Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
DataTable.GetChanges returns null sometimes even though changes have been made.

1.Edit a cell in first row (C,1), change is applied and saves successfully to DB. Cursor moves to cell (C,2) on return.
This is more odd than I thought as DataTable changeTable = DataTableTopLevel.GetChanges(); returns null, but it still saves
2.Edit a cell in second row (C,2), value changes back on return key. Also cursor does not move to (C,3) BUT moves to (A,3).
DataTable changeTable = DataTableTopLevel.GetChanges(); returns non null, but is not saved.
3.Edit a cell in third row (C,3), change is applied and saves successfully to DB. Cursor moves to cell (C,4) on return.

C++
public partial class MeServerMainForm : Form
 {

     string connServer;
     string qryServerTopLevel;
     string qryServerBottomLevel;
     SqlDataAdapter SqlAdapterTopLevel;
     SqlDataAdapter SqlAdapterBottomLevel;
     SqlCommandBuilder SqlCommandBuilderTopLevel;
     SqlCommandBuilder SqlCommandBuilderBottomLevel;
     DataTable DataTableTopLevel;
     DataTable DataTableBottomLevel;
     BindingSource BindingSourceTopLevel;
     BindingSource BindingSourceBottomLevel;

     bool TopLevelCellValueChanged;

     public MeServerMainForm()
     {
         InitializeComponent();
         InitData();
         TopLevelCellValueChanged = false;
     }

     public void InitData()
     {
         //create the connection string
         connServer = "Data Source=192.168.0.11;Initial Catalog=Phil;User Id=Philip;Password=Philip;";

         // TOP LEVEL
         //create the database query
         qryServerTopLevel = @"SELECT
                                             MeID
                                             ,MeTypeID
                                             ,MeTitle
                                             ,CreatedDateTime
                                             ,ReadOnly
                                             ,ParentMeID
                                             ,Question
                                             ,Answer
                                             ,MeScore
                                             ,MeLastTestDate
                                         FROM MyMees
                                         WHERE ParentMeID = 0 ";

         //create an OleDbDataAdapter to execute the query
         SqlAdapterTopLevel = new SqlDataAdapter(qryServerTopLevel, connServer);
         //create a command builder
         SqlCommandBuilderTopLevel = new SqlCommandBuilder(SqlAdapterTopLevel);
         //create a DataTable to hold the query results
         DataTableTopLevel = new DataTable();
         //fill the DataTable
         SqlAdapterTopLevel.Fill(DataTableTopLevel);
         //BindingSource to sync DataTable and DataGridView
         BindingSourceTopLevel = new BindingSource();
         //set the BindingSource DataSource
         BindingSourceTopLevel.DataSource = DataTableTopLevel;
         //set the DataGridView DataSource
         DataGridViewTopLevel.DataSource = BindingSourceTopLevel;

         // Bottom LEVEL
         //create the database query
         qryServerBottomLevel = @"SELECT
                                             MeID
                                             ,MeTypeID
                                             ,MeTitle
                                             ,CreatedDateTime
                                             ,ReadOnly
                                             ,ParentMeID
                                             ,Question
                                             ,Answer
                                             ,MeScore
                                             ,MeLastTestDate
                                         FROM MyMees
                                         WHERE ParentMeID = 2 ";

         //create an OleDbDataAdapter to execute the query
         SqlAdapterBottomLevel = new SqlDataAdapter(qryServerBottomLevel, connServer);
         //create a command builder
         SqlCommandBuilderBottomLevel = new SqlCommandBuilder(SqlAdapterBottomLevel);
         //create a DataTable to hold the query results
         DataTableBottomLevel = new DataTable();
         //fill the DataTable
         SqlAdapterBottomLevel.Fill(DataTableBottomLevel);
         //BindingSource to sync DataTable and DataGridView
         BindingSourceBottomLevel = new BindingSource();
         //set the BindingSource DataSource
         BindingSourceBottomLevel.DataSource = DataTableBottomLevel;
         //set the DataGridView DataSource
         DataGridViewBottomLevel.DataSource = BindingSourceBottomLevel;

         DataTableTopLevel.AcceptChanges();
     }

     private void loadMeTreeToolStripMenuItem_Click(object sender, EventArgs e)
     {

     }

     private void DataGridViewTopLevel_RowLeave(object sender, DataGridViewCellEventArgs e)
     {

         DataTable changeTable = DataTableTopLevel.GetChanges();
         if (changeTable != null)
         {
             SqlAdapterTopLevel.Update(DataTableTopLevel);
             DataTableTopLevel.AcceptChanges();
             DataTableTopLevel.Clear();
             SqlAdapterTopLevel.Fill(DataTableTopLevel);
             TopLevelCellValueChanged = false;

         }




     }



Any ideas?
Thanks
Phil
Posted
Updated 9-Nov-10 2:42am
v3

Why not use the DataGridViewCellEventArgs?

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcelleventargs.aspx[^]

Have a look at the remarks (that will explain I think):
http://msdn.microsoft.com/en-us/library/5dxfaha8.aspx[^]

Good luck!
 
Share this answer
 
v2
I was using DataGridView.RowLeave, I should have used DataGridView.RowValidated. I believe this is because the row leave event can be fired before RowValidated has occurred. When this happens, DataGridView.GetChanged() returns null.

example

C#
private void DataGridViewTopLevel_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            DataTable changeTable = DataTableTopLevel.GetChanges();
            if (changeTable != null)
            {
                SqlAdapterTopLevel.Update(DataTableTopLevel);
            }
        }


Thanks for you help E.F. Nijboer, I am now using the DataGridViewCellEventArgs for other uses.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900