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);
}
}