Alan
Alan

Reputation: 2086

Setting ID for XAML Checkbox

I have a datagrid that is populated from a list. I added some checkboxes so that I could delete the items on the list, but then couldn't figure out how to attach an ID/Gui to the checkbox so I could delete them by the database ID#. Eventually I added a new column of ID and tried setting it's width to 0, or setting it's visibility to hidden or collapsed, but then it can't find the ID if the value isn't visible. Here is the code to populate:

public void PopulateDataGrid()
{
    items = new List<CommandGridItems>();
    var serverCommandsList = ServerDB.ExecuteDB("SELECT * FROM commands where group_id = " + Config.groupId);
    var json = JsonConvert.SerializeObject(serverCommandsList);
    dynamic dynJson = JsonConvert.DeserializeObject(json);
    foreach (var command in dynJson)
    {
        items.Insert(items.Count, new CommandGridItems() { Delete = false, Command = command.command.ToString(), Response = command.expected_response.ToString(), Results = "", Command_ID = command.id.ToString() });
    }
    CommandRows2.ItemsSource = items;
    CommandRows2.Items.Refresh();

}

XAML:

<DataGrid Name="CommandRows2" Margin="0,36,0,73" AutoGenerateColumns="False" ScrollViewer.CanContentScroll="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding Command_ID}" Width="0" x:Name="ID"/>
        <DataGridCheckBoxColumn Header="Delete" Binding="{Binding Delete}"  x:Name="Delete"/>
        <DataGridTextColumn Header="Command" Binding="{Binding Command}" Width="200"/>
        <DataGridTextColumn Header="Response" Binding="{Binding Response}" Width="*"/>
        <DataGridTextColumn Header="Test Results" Binding="{Binding Results}" Width="70"/>

    </DataGrid.Columns>
</DataGrid>

I have tried attaching properties to the DataGridCheckBoxColumn such as x:Gui, DataContext, x:XData and several more, but I can't find how to attach an ID to the checkbox so that I don't have to have an extra column that adds nothing to the view.

Here is what I am using for deleting, again, it can't find the value for rowId unless that column is visible.

private void Delete_Commands(object sender, RoutedEventArgs e)
{
    foreach (CommandGridItems item in CommandRows2.ItemsSource)
    {
        if (((CheckBox)Delete.GetCellContent(item)).IsChecked == true)
        {
            var rowId = ID.GetCellContent(item) as TextBlock;
            ServerDB.ExecuteDB("DELETE FROM commands where id = " + rowId.Text);
            PopulateDataGrid();
        }
    }
}

Upvotes: 0

Views: 936

Answers (1)

ASh
ASh

Reputation: 35723

why bother with UI elements, when there is already a reference of a data item? Properties of data items are updated via bindings after user interactions with UI (e.g. clicks on CheckBoxes)

private void Delete_Commands(object sender, RoutedEventArgs e)
{
    foreach (CommandGridItems item in CommandRows2.ItemsSource)
    {
        if (item.Delete)
        {
            // use parameter for id here !!!
            ServerDB.ExecuteDB("DELETE FROM commands where id = " + item.Command_ID);
        }
    }
    PopulateDataGrid();
}

also if ItemsSource is an ObservableCollection, then items can be removed from it (and from DataGrid thanks to INotifyCollectionChanged) without reloading all data.

Upvotes: 1

Related Questions