C
C#17mo ago
ApathyErr

what event should be added for the datagrid so that when the line stops being highlighted, track it?

2 Replies
ApathyErr
ApathyErrOP17mo ago
my code to track the last highlighted line. I want to modify it so that you can delete several elements and so that the last element that was selected is not deleted if the selection is removed from it by clicking on something else, but I don't know how
private void customersGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dg = (DataGrid)sender;
DataRowView rs = dg.SelectedItem as DataRowView;
if (rs != null)
{
Customer.SelectedId = rs["ID"].ToString();
}
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete the entry?", "Notification", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
using (var connection = new SqlConnection(_connectionString))
using (var command = new SqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = "DELETE FROM Customers WHERE ID='" + Customer.SelectedId + "'";
command.ExecuteScalar();
GetCustomersFromDataBase();
}
}

}
private void customersGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dg = (DataGrid)sender;
DataRowView rs = dg.SelectedItem as DataRowView;
if (rs != null)
{
Customer.SelectedId = rs["ID"].ToString();
}
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete the entry?", "Notification", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
using (var connection = new SqlConnection(_connectionString))
using (var command = new SqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = "DELETE FROM Customers WHERE ID='" + Customer.SelectedId + "'";
command.ExecuteScalar();
GetCustomersFromDataBase();
}
}

}
Up
Accord
Accord16mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server