简体   繁体   中英

Get Checkbox value in Datagrid bound to an ItemsSource in wpf

In WPF, I created a Datagrid that is bound to an ItemsSource and then I added another column which is not bound to the itemssource. I'm trying to iterate over rows in Datagrid and get the checkbox value(true or false). Here is a snippet from my Datagrid:

<DataGridTemplateColumn x:Name="CheckBoxColumn" Width="Auto">
    <DataGridTemplateColumn.Header>
        <CheckBox Content="Select All" Style="{StaticResource StdCheckBoxStyle}" x:Name="headerCheckBox"/>
    </DataGridTemplateColumn.Header>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox x:Name="checkBoxRow" Margin="45 2 0 0" Style="{StaticResource StdCheckBoxStyle}"
                            IsChecked="{Binding IsChecked, ElementName=headerCheckBox, 
                                        Mode=OneWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Index, Mode=OneWay}" IsReadOnly="True" Width="Auto" Header="Index"/>

Datagrid is bound to an ItemsSource, which is an ICollection of MyObject. Here is what I did in code behind:

    public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if (null != row) yield return row;
        }
    }

    private void OnUpdate(object obj)
    {
      var rows= GetDataGridRows(MyDatagrid); 

        foreach (DataGridRow r in rows)  
        {  
            DataRowView rv = (DataRowView)r.Item;
            foreach (DataGridColumn column in MyDatagrid.Columns)
            {
                if (column.GetCellContent(r) is CheckBox)
                {
                    CheckBox cellContent = column.GetCellContent(r) as CheckBox;
                 //do something
                }
            }
        } 

    }

But after running this piece of code, I get an exception:

Unable to cast object of type '...MyObject' to type 'System.Data.DatarowView'.

I looked a little bit and found out that in MSDN:

the DataItem property of the GridViewRow "Gets the underlying data object to which the GridViewRow object is bound."

I know the problem, I know the reason but couldn't find a way to fix it. Thanks for your attention.

In your code, the exception:

Unable to cast object of type '...MyObject' to type 'System.Data.DatarowView'.

is raised because of this line:

DataRowView rv = (DataRowView)r.Item;

Which means the r.item object is of type MyObject. The cast is invalid. Use instead:

MyObject rv= r.Item;

[EDIT] The first part of the answer only solved the problem with the exception. You'll have to convert your datagrid to a datagridview. To access the content of the cell:

    foreach (DataGridViewRow r in MyDataGridView.Rows)  
    {  
        foreach (DataGridViewCell c in r.Cells)
        {
            if (c.Value is CheckBox)
            {
                CheckBox cellContent = c.Value as Checkbox;
             //do something
            }
        }
    } 

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