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);
}
}
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 emptyelse
clause to the firstif
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 theif
and examine theLabel.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.