Issue with DataGrid Selection in WPF Application

Kamyab Faghih 65 Reputation points
2024-12-23T11:22:35.7+00:00

A WPF application has been developed using .NET 5, which includes a DataGrid and a button. The following is the code for the button's click event.

The issue encountered is that the index and selectedRow values of the DataGrid are only correctly stored during the first click. Subsequent clicks on another row do not update the values of index and selectedRow. Extensive searches for a solution have not yielded results. Guidance is appreciated.

private void ModifyProductSellPrices(object sender, RoutedEventArgs e) 
{   
  var selectedRow = (ProductSize)dgrdMain.SelectedValue;     
  int index = dgrdMain.SelectedIndex;         
  AppWindows.ModifyProductSellPrices modify = new ModifyProductSellPrices(selectedRow);     
  modify.ShowDialog();   
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,011 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 3,645 Reputation points Microsoft Vendor
    2024-12-25T02:25:50.54+00:00

    Hi, @Kamyab Faghih.

    This is not a good example of what might cause the selectedRow and index to be incorrect. You could check and correct dgrdMain.SelectedIndex and dgrdMain.SelectedValue in the DataGrid's Selected event.

    However, if your Button is in each row, you could find a different way to get the selectedRow and index. Adjust the code provided in the comments:

    MainWindow.xaml

        <Grid>
            <DataGrid x:Name="dgrdMain" AutoGenerateColumns="False" CanUserAddRows="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}" ></DataGridTextColumn>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
                    <DataGridTemplateColumn Header="Option">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Button  Height="50" Width="100" Content="Clike Me" Click="ModifyProductSellPrices"></Button>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    

    MainWindow.xaml.cs

            private void ModifyProductSellPrices(object sender, RoutedEventArgs e)
            {
                // Get the DataContext of the button, which should be the corresponding data row
                var button = sender as Button;
                var selectedRow = button.DataContext as ProductSize;
    
                // Find the index of the row in the DataGrid
                int index = dgrdMain.Items.IndexOf(selectedRow);
    
                // Output to verify
                if (selectedRow != null && index != -1)
                {
                    MessageBox.Show(selectedRow?.ToString() + "\n" + index.ToString());
                }
                else
                {
                    MessageBox.Show("Unselected Item");
                }
                // Correct DataGrid's SelectedItem and SelectedIndex
                dgrdMain.SelectedIndex = index;
                dgrdMain.SelectedValue = selectedRow;
            }
    

    If you still cannot get the correct result, you could narrow down the scope of the problem: Is there an error between the data bound to each row and the data bound to DataGrid.ItemSource.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.