A
A
Anton_repr2019-11-05 18:03:10
WPF
Anton_repr, 2019-11-05 18:03:10

How to open a form on click on a row in DataGrid?

I don't really understand how to track a click on the DataGrid. here is the xaml markup:

<DataGrid AutoGenerateColumns="False" x:Name="dg_recipes" Margin="10,249,10,10">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Название" Binding="{Binding Path='name'}" Width="320"/>
                <DataGridTextColumn Header="Время приготовления" Width="280"/>
            </DataGrid.Columns>
        </DataGrid>

By clicking on the selected line in 1 column, a form should open

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Space Purr, 2019-11-05
@Anton_repr

  • We create a column, the content in the cells of which will represent a Button with the Button_Click event.
    <DataGrid Name="MyDataGrid" ItemsSource="{Binding Table}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Click="Button_Click">Open Form</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

  • Let there be some form TosterForm, the constructor of which takes one string argument - the value in the 3rd column of the selected line.
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TosterForm form = new TosterForm(((DataRowView)MyDataGrid.SelectedItem).Row.ItemArray[3].ToString());
        form.ShowDialog();
    }

    Regarding getting the value in the cell, there may be another option, I had practically never worked with the DataGrid before.
    Actually, like everything.
    5dc1d8876549f349153436.gif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question