I am working with a simple application with c# What I wan't to do is that when I retrieve data from SQL Database into Datagrid, i want some of rows to deleting by selecting them and then clicking a button.
The code i used to retrieve data is shown below:
SqlConnection conn = new SqlConnection("Server=MEO-PC;Database= autoser; Integrated Security = true");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * From evidenc", conn);
DataTable dt = new DataTable("dtList");
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dtg.ItemsSource = dt.DefaultView;
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
conn.Close();
Also the code i tried for deleting the specific row is:
if (dtg.SelectedIndex >= 0)
{
dtg.Items.RemoveAt(dtg.SelectedIndex);
}
The erro i get is :Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
I don't know where is the problem cause I'am new to programming. Thanks to everyone