简体   繁体   中英

Access DataGrid Cell in CodeBehind File WPF

I am trying to access Datagrid cell value in wpf while iterating through all its row.I want to get value of first column of every row.I have tried so many things but couldn't succeed.Please help

Here is what I am trying

 var dg = productFilterResult.ProductsGrid;
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    var cellContent = dg.Columns[1].GetCellContent(row);
                    var x = (ContentPresenter)cellContent;
                    var y = x.Content;
                }

This is my xaml file:

                <DataGridTemplateColumn Header="Id" Visibility="Collapsed" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Id}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Index"  Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Name="IndexCell" Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource RowToIndexConvertor}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Name" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Description" Width="*" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Description}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Update" Width="Auto" MinWidth="200" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image  x:Name="EditImg" Style="{DynamicResource EditImg}" Source="/CPOSApplication;component/Icons/edit.png" Tag="{Binding}" MouseDown="Edit_Click"></Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Delete" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image  x:Name="DeleteImg" Style="{DynamicResource DeleteImg}" Source="/CPOSApplication;component/Icons/delete.png" Tag="{Binding}" MouseDown="Delete_Click"></Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

Try the below code.

 public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

Reference link http://svgvijay.blogspot.com/2013/01/how-to-get-datagrid-cell-in-wpf.html

This is how it worked for me

 var dg = productFilterResult.ProductsGrid;
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    var cellContent = dg.Columns[1].GetCellContent(row);
                    var cellContentPresenter = (ContentPresenter)cellContent;
                    DataTemplate editingTemplate = cellContentPresenter.ContentTemplate;
                    TextBlock CellTextBox = editingTemplate.FindName("IndexCell", cellContentPresenter) as TextBlock;
                    string CellValue = CellTextBox.Text;
                    var BindedModel = cellContentPresenter.Content;
                }

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