简体   繁体   中英

How do I figure out if a row in a DataGridCheckBoxColumn is checked in a DataGrid?

If I have a DataGridCheckBoxColumn in a DataGrid (rather than using the data grid's multiple selection mode) how do I keep track or discover which rows have been checked?

Is there a suitable property in the DataGrid to enumerate over for each row and inspecting the column? DataGrid.ItemsSource will give me the underlying collection - I want to be able to get at the columns in the grid row items themselves

If I responded to the CheckBox's Click event, how would I figure out which row for which item in the underlying collection this CheckBox belonged to?

<DataGrid x:Name="dgPlayers" AutoGenerateColumns="False" Height="450" CanUserAddRows="False" AlternationCount="2" AlternatingRowBackground="WhiteSmoke" GridLinesVisibility="None">
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn></DataGridCheckBoxColumn>
                    <DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"></DataGridTextColumn>
                    <DataGridTextColumn Header="Position" Binding="{Binding Path=PositionCode, Converter={StaticResource NFLPositionGrouper}}"></DataGridTextColumn>
                    <DataGridTextColumn Header="College" Binding="{Binding Path=CollegeName}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>

As you can see, I'm not binding the DataGridCheckBoxColumn to any property - my purpose is to use it for selection (yes I know, the DataGrid has multiple-selection already built in ...this is purely an academic exercise)

you can try something like this

 foreach (GridViewRow row in yourgrid.Rows)
    {
        Access the CheckBox
        CheckBox cb = (CheckBox)row.FindControl("youcheckboxid");
        if (cb != null && cb.Checked==true)
        {

          //you should now know the row where the checkbox was cheked 
         }
    }

While I wasn't able to figure out how to enumerate over each DataGridRow, as you would and might be able to do in ASP.NET via a .Rows collection, I realize that this is not a good idea anyway, and Microsoft probably intentionally omitted this property.

This is because the DataGrid, like many other list controls uses UI virtualization, meaning that it only generates DataGridRow items for VISIBLE rows anyway - if you could enumerate over every row, this would defeat the purpose of this feature and unnecessarily consume memory.

As many people have mentioned (to whom I do not disagree with) ultimately the best way is to use the grid the way MS intended, which is in an OO-manner, binding to an underlying object with direct access to it.

However, I did find something close to what I wanted, that does give me access to the actual DataGridRow object:

myDataGridInstance.ItemContainerGenerator.ContainerFromItem()

Or

myDataGridInstance.ItemContainerGenerator.ContainerFromIndex()

Both methods will give you DataGridRow corresponding to the specified collection-object, after which you can dissect and navigate it's visual tree and properties (eg it's own DataContext would be the collection item instance) as you would expect any other normal element

An alternative way I found to keep track of all my CheckBox ticks, was to associate the underlying object binding via the Control.Tag property - I would set this to {Binding} which simply binds it to the underlying collection object itself, rather than a specific property ...in the event handler for the CheckBox I could then manually keep track of a list of items that are checked / unchecked.

Definitely not a smart way to do it - but it is possible, and that was what I was looking for - having said that, I will now go back to doing things the way MS intended us to do :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM