Имеется 2 DataGridView, которые были созданы из одной и той же таблицы базы данных. Когда во второй таблице я удаляю некоторые строки, они удаляются и в первой, как этого избежать? Нашел решение с созданием нового BindingSource. Создал, настроил, указал его в источнике данных для второго DataGridView, но это не сработало (мог и настроить не правильно..). При чем та же проблема была и у ComboBox (2 ComboBox с тем же источником данных, один в области с добавлением в БД, другой для изменения выбранного в БД), но это решилось как раз созданием нового BindingSource.
Имеется 2 метода для заполнения и других манипуляций с этими DataGridView
Для первого:
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();
И для второго:
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();