0

im working on a Windows Forms app that allows the user to update the Database programmaticaly (update number of cells values once) and manualy (update one by one). the process works fine in the maual update, but when its trying to update programmaticaly, only some of the changes are saved.

When I look at the datagridview after the programmaticaly change, I can see that the values have been changed, but when I close the program and open it again, only some of the changes remain.

What is strange is that when I click the column header to sort the values before I close it (the values are dates), some of the dates are not in order. those same dates are the ones which are not save to the database.

How do I fix that? How do i make all the changes to be saved?

Here is my related code:

private void save_btn_Click(object sender, EventArgs e)
{
     dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
     string t = "";
     Label l;
     int space;
     for (int c = 1; c < tableLayoutPanel1.ColumnCount; c++)
     {
         for (int r = 1; r < tableLayoutPanel1.RowCount; r++)
         {
             l = (Label)tableLayoutPanel1.GetControlFromPosition(c, r);
             if (l != null)
             {
                 t = l.Text;
                 for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                 {
                    if (dataGridView1[2, i].Value.ToString().Equals(t))
                    {
                        dataGridView1[4, i].Value = GetDate();
                    }
                 }
             }            
         }           
     }
     bindingSource1.EndEdit();
     dataAdapter.Update((DataTable)bindingSource1.DataSource);
     dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
}

Edit: manual updating code:

  private void saveDB_Btn_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.EndEdit();
                dataAdapter.Update((DataTable)bindingSource1.DataSource);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

Binding Datagridview to Database code:

 void GetData(string selectCommand)
        {
            try
            {
                dataAdapter = new SqlCeDataAdapter(selectCommand, conn);
                commandBuilder = new SqlCeCommandBuilder(dataAdapter);
                table = new DataTable();
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
someGuy
  • 43
  • 1
  • 1
  • 6
  • You are using a BindingSource, perhaps the unsaved values are not binded to it? – Omer Dec 03 '14 at 19:01
  • Thank you for the comment. What reason could cause those values not to be bound? and why sometimes it works and sometimes it doesn't ? – someGuy Dec 03 '14 at 19:06
  • If you ask why with the 'manual' update it works, and why it doesn't work when you try to enumerate all cells and save the data 'automatically', the answer is that it's not the same code. You only posted the 'automatic' saving code, so it's pretty hard to guess. – Omer Dec 03 '14 at 19:18
  • The manual part is actually the last 3 lines of code, without the EditMode part. – someGuy Dec 03 '14 at 19:40
  • Right. It is still pretty difficult to help you with only part of the code in hand. My suggestion to you is either to post the full form code in pastebin and add the link to your question, or try to reduce the problem, and minimize your code by removing everything unrelated (like the things that are being saved) so you could post the smallest possible reproducible example that demonstrates your issue. This will make it much easier to help you (and you might even figure it out yourself on the way!) – Omer Dec 03 '14 at 19:44
  • 1
    Please check my new edit of the question. I added some code so you could take a look. @Omer – someGuy Dec 04 '14 at 14:15

1 Answers1

0

Despite the additional code you added after the edit, it is hard to tell exactly what is going on, because some relevant code is still missing, such as the GetDate() method, the code for the Sorting, and the TableLayoutPanel's code in the *.Designer.cs file.

It would be pretty easy to find the root cause during a debug session; first, check if you miss values which are not stored in a Label control, if there are any. This could be done by adding an empty else clause to the first if statement and putting a breakpoint there, see if it hits.

A breakpoint inside the first if statement could tell you if you see there all values that you see displayed on the UI. Just put a breakpoint inside the if and examine the Label.Text.

Last but not least, you could examine the second if statement. Since you are comparing the TableLayoutPanel's label text with the DGV text, there might be a mismatch that cause some values not to update. This could be done by stepping through the code in the most inner clause, and making sure that all the labels that should be matched indeed matched.

Hope this helps.

Omer
  • 684
  • 4
  • 14