F
F
freshlemon2020-04-29 09:33:03
C++ / C#
freshlemon, 2020-04-29 09:33:03

How to fix that 2 elements with the same data source are dependent on each other?

There are 2 DataGridViews that were created from the same database table. When I delete some rows in the second table, they are deleted in the first one, how to avoid this? Found a solution with creating a new BindingSource. I created, configured, specified it in the data source for the second DataGridView, but it did not work (it could not have been configured correctly ..). At what the ComboBox had the same problem (2 ComboBox with the same data source, one in the area with adding to the database, the other to change the selected one in the database), but this was solved just by creating a new BindingSource.
There are 2 methods for filling and other manipulations with these DataGridView
For the first one:

Connection();
SqlCommand command = con.CreateCommand();
command.CommandText = "SELECT hot_ID, hot_name FROM t_hotel";
SqlDataReader itog = command.ExecuteReader();
for (int i = 0; i < t_serviceDataGridView.Rows.Count; i++)
{
   while (itog.Read())
      {
         if (t_serviceDataGridView.Rows[i].Cells[6].Value.ToString() == itog.GetValue(0).ToString())
            {
               t_serviceDataGridView.Rows[i].Cells[1].Value = itog.GetValue(1);
               break;
            }
         }
      itog.Close();
      itog = command.ExecuteReader();
      t_serviceDataGridView.Rows[i].Cells[4].Value =               
         ((Convert.ToInt32(t_serviceDataGridView.Rows[i].Cells[5].Value) == 1) ? "Да" : "Нет");
}
con.Close();

And for the second:
t_serviceTableAdapter.Fill(database1DataSet.t_service);
t_serviceDataGridView1.Update();
for (int i = t_serviceDataGridView1.Rows.Count - 1; i >= 0; i--)
{
   if (t_serviceDataGridView1.Rows[i].Cells[6].Value.ToString() != 
      t_permitsDataGridView1.CurrentRow.Cells[8].Value.ToString())
   {
      t_serviceDataGridView1.Rows.RemoveAt(i);
   }
}
Connection();
SqlCommand command = con.CreateCommand();
command.CommandText = "SELECT hot_ID, hot_name FROM t_hotel";
SqlDataReader itog = command.ExecuteReader();
for (int i = 0; i < t_serviceDataGridView1.Rows.Count; i++)
{
   while (itog.Read())
{
   if (t_serviceDataGridView1.Rows[i].Cells[6].Value.ToString() == itog.GetValue(0).ToString())
{
   t_serviceDataGridView1.Rows[i].Cells[1].Value = itog.GetValue(1);
   break;
}
}
itog.Close();
itog = command.ExecuteReader();
t_serviceDataGridView1.Rows[i].Cells[4].Value =
   ((Convert.ToInt32(t_serviceDataGridView1.Rows[i].Cells[5].Value) == 1) ? "Да" : "Нет");
}
con.Close();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-04-29
@freshlemon

no way. absolutely appropriate behaviour. This is exactly what binding is for.
to disable synchronization of changes, you just need to break the connection between the view and the source
- do not use binding at all, or only to one of the tables
- one or both of the tables, fill in by copying the data, and not providing them as a source
- as an option, you can create clone of the source, and to bind the second table to it, the
choice of strategy depends on the goals set for such an interface

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question