I've create a ContexMenu, and the option allow the user to clear the all item in the DataGrid. All working so good, but I want to optimize the algorithm with one method, in particular I want do this:
User go in one of three DataGrid and press right mouse button, delete the option in this table
A method starting the method, this method should do this:
Check which table has fired the events, get the DataGrid name and delete all rows of the DataGrid that have fired the events. This is my code XAML CODE:
ContextMenu x:Key="Squadre_ContextMenu"> <MenuItem Header="Pulisci Tabella" Click="ClearTable_Click"> </MenuItem> </ContextMenu>
C# CODE method calling:
private void ClearTable_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine(((DataGrid)sender).Name.ToString()); //exception returned...
//datagrid.ClearValue();
}
and in a class I've the property function:
public static void removeRows(DataGrid name passed by parameter)
{
var grid = parameter passed;
var mygrid = parameter passed;
if (grid.SelectedIndex >= 0)
{
for (int i = grid.SelectedItems.Count - 1; i >= 0; i--)
{
mygrid.Items.Remove(grid.SelectedItems[i]);
};
}
grid = mygrid;
}
NB: datagrid variable is an instance of the class DataGrid datagrid = new DataGrid(); this class contains the function for delete all the rows.