WPF-如何在datagrid中获取选定的行索引? [英] WPF- How to get selected row index in datagrid?

查看:252
本文介绍了WPF-如何在datagrid中获取选定的行索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagrid中有文本框。数据取自数据库。假设我有这些文本框值的10行。一旦我单击该行,便可以获取此选定的行索引。我的目标是,一旦文本框值被更改,我需要检测它是哪一行(哪个值),并基于该值进行一些计算,然后需要显示同一行的另一个字段。因此,我可以知道哪一行被命中。我正在使用带有以下声明的Datagrid:

I have the textbox in datagrid. Data are taking from database. assume I have 10 rows with these textbox value. once I click on this row, able to get this selected row index. My goal is if once textbox value are get change, I need to detect which row it is (which value)and do some calculation based on this value then need to display another field of the same row. So I am in a position to know which row being get hit. `I am using Datagrid with following declarations:

    <dg:DataGrid Name="dgBudgetAllocation" CanUserDeleteRows="False" CanUserAddRows="False" CanUserSortColumns="True"
                        IsSynchronizedWithCurrentItem="True" MaxHeight="400" RowHeight="70" SelectionUnit="Cell" SelectedValue="" SelectionMode="Single"
                 AutoGenerateColumns="False" GridLinesVisibility="None"  HeadersVisibility="Column"  PreviewMouseDown="DgBudgetAllocation_OnPreviewMouseDown" SelectedCellsChanged="DgBudgetAllocation_OnSelectedCellsChanged" MouseDown="DgBudgetAllocation_OnMouseDown" PreviewMouseUp="DgBudgetAllocation_OnPreviewMouseUp"  PreviewKeyDown="DgBudgetAllocation_OnPreviewKeyDown" HorizontalAlignment="Left">


                       <dg:DataGridTemplateColumn Header="Budget Type" SortMemberPath="BUDGETYPE"
                                       MinWidth="50" HeaderStyle="{DynamicResource dgHeaderLeftJust}" CellStyle="{DynamicResource dgColumnRightJust}">
                <dg:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding BUDGETYPE}" HorizontalAlignment="left" VerticalAlignment="Top" Margin="0,0,3,0" />
                    </DataTemplate>
                </dg:DataGridTemplateColumn.CellTemplate>

            </dg:DataGridTemplateColumn>

我根据各种人的建议尝试了以下代码段。我得到的所有选定索引都是-1。

I have tried the following snippet based on various person suggestion. for all I am getting the selected index is -1.

DataRowView drv = (DataRowView)dgBudgetAllocation.SelectedItem;
                object item = dgBudgetAllocation.SelectedItem;
                string ID = (dgBudgetAllocation.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
                DataGrid row1 = (DataGrid)dgBudgetAllocation.SelectedItems[1];
                var row = dgBudgetAllocation.SelectedItems[0]; 

什么都没用。
请建议我如何进一步操作。

Nothing is working. Please suggest me how to proceed further .

推荐答案

c订阅选择更改事件(SelectionChanged = ItemsView_OnSelectionChanged)并使用处理程序获取所需的所有东西。您可以在行为(和MVVM)中做到这一点,或者只是将处理程序放在代码中。

cSubscribe for the selection changing event( SelectionChanged="ItemsView_OnSelectionChanged") and use the handler to get all things you need. You can do that in behavior(and MVVM) or just put the handler inside your code behind.

处理程序代码示例

 private void ItemsView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var dg = sender as DataGrid;
        if (dg == null) return;
        var index = dg.SelectedIndex;
        //here we get the actual row at selected index
        DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;

        //here we get the actual data item behind the selected row
        var item = dg.ItemContainerGenerator.ItemFromContainer(row);

    }

让我知道是否需要更多说明。
的问候。

Let me know if you need more explanation. Regards.

这篇关于WPF-如何在datagrid中获取选定的行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆