I want to delete from database when i select a row in datagridview. I tried 2 different methods. With this method i can delete only a few rows.
cmd.CommandText = "delete from dbo.Scenarist where idScenarist=@idScenarist";
cmd.Parameters.Add(new SqlParameter("@idScenarist", dataGridScenarist.SelectedIndex));
I tried also with SelectedRows, but i have this error:
'System.Windows.Controls.DataGrid' does not contain a definition for 'SelectedRow' and no extension method 'SelectedRow' accepting a first argument of type 'System.Windows.Controls.DataGrid' could be found (are you missing a using directive or an assembly reference?)
Is a WPF App and i can't use DataGridView.Only DataGrid. Thank you!
You would probably want to use DataGridView
here. It provides access to SelectedRows
which is a collection of DataGridViewRow
s. Loop through that collection and update your @idScenarist
parameter with the value from the column containing the corresponding id, and execute the delete command object in each loop.
Here is a basic example:
cmd.CommandText = "delete from dbo.Scenarist where idScenarist=@idScenarist";
SqlParameter parm = new SqlParameter("@idScenarist", SqlDbType.Int);
cmd.Parameters.Add(parm);
foreach (DataGridViewRow row in dataGridScenarist.SelectedRows)
{
parm.Value = row.Cells["idScenarist"].Value;
cmd.ExecuteNonQuery();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.