WPF- How to get selected row index in datagrid?

2019-07-22 14:22发布

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>

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 .

1条回答
2楼-- · 2019-07-22 14:34

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.

A handler code example

 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.

查看更多
登录 后发表回答