Jenssen
Jenssen

Reputation: 1871

wpf get selected row when checkbox value changes

I'm building a wpf application in c#. I've got a GridView with information. Every row has a checkbox. When a checkbox is being clicked I would like to receive the row's user name value (in column 1).

Right now it's working but the entire row has to be selected. Otherwise I receive of course a null exception.

private void CheckBox_breakfast(object sender, RoutedEventArgs e)
{
    Reservation reservation = gridReservations.SelectedItem as Reservation;
    string name = reservation.user_name;
}

How do I get this to work with only selecting the checkbox instead of the entire row?

Already searched on the web and tried a lot but nothing works.

Would help me a lot!

Upvotes: 0

Views: 2591

Answers (2)

George
George

Reputation: 693

I just ran over the same issue as yours and I found a solution.

Firstly, if you refer to WPF (and rather than the ASP web framework) then you probably mean the Datagrid control instead of GridView control.

this goes into the XAML

<DataGrid>
    <DataGrid.Columns>
       ... <!--other definitions of datagrid columns go here-->
       <DataGridCheckBoxColumn Header="Select to delete">
            <DataGridCheckBoxColumn.ElementStyle>
                <Style> <!-- used the following for design sake to center vertically and horizontally the checkboxes relative to the other content in the datagrid-->
                    <Setter Property="TextBlock.VerticalAlignment" Value="Center" />
                    <Setter Property="TextBlock.HorizontalAlignment" Value="Center" />
                </Style>
            </DataGridCheckBoxColumn.ElementStyle>
        </DataGridCheckBoxColumn>
    </DataGrid.Columns>
</DataGrid>

and this should go in the .xaml.cs code behind

private void getCodMatricol_CheckBox_Selected_in_Datagrid()
    {
        List<int> your_list_of_items_that_correspond_to_checked_checkboxes = new List<int>();
        for (int i = 0; i < datagridGrupeProductie.Items.Count; i++)
        {
            var item = datagridGrupeProductie.Items[i];
            var mycheckbox = datagridGrupeProductie.Columns[10].GetCellContent(item) as CheckBox;
            var myTextBlock = datagridGrupeProductie.Columns[0].GetCellContent(item) as TextBlock;
            if ((bool)mycheckbox.IsChecked)
            {
               your_list_of_items_that_correspond_to_checked_checkboxes.Add(int.Parse(myTextBlock.Text));
            }
        }
    }

In my example I extract the content of the first column in the datagrid which contains some codes represented by integers.

Note: index for rows and columns in the datagrid starts from [0] (Actually as most indexes in C#)

If the column in the datadgrid is defined as (frequently used)

</DataGridTextColumn> </DataGridTextColumn>

then it hosts a TextBlock element.

var myTextBlock = datagridGrupeProductie.Columns[0].GetCellContent(item) as TextBlock;

You have to address the Text property of this control to read its content and Convert it as necessary.

int.Parse(myTextBlock.Text

Next, you are filling in in the List collection with the content you are extracting:

your_list_of_items_that_correspond_to_checked_checkboxes.Add(int.Parse(myTextBlock.Text));

Further more, if you want to operate anything on this values you have to traverse the collection

foreach (var item in your_list_of_items_that_correspond_to_checked_checkboxes)
            {
                //do whatever needed on each item
            }

Upvotes: 0

mm8
mm8

Reputation: 169190

You could try to cast the DataContext of the CheckBox itself to a Reservation:

private void CheckBox_breakfast(object sender, RoutedEventArgs e)
{
    CheckBox checkBox = sender as CheckBox;
    Reservation reservation = checkBox.DataContext as Reservation;
    string name = reservation.user_name;
}

This should work if you haven't explicitly set the DataContext of the CheckBox or any of its parent elements in the cell to something else.

Upvotes: 3

Related Questions