nam
nam

Reputation: 23868

WPF DataGrid - How to make selected row editable on Button click

The following DataGrid is set to ReadOnly. But, when Edit Button on a row is clicked I need that row to become editable. How can we achieve this task?

Please note that the Edit button click event is still fired on a ReadOnly grid. Maybe, in this event we need to set DataGridCell.IsEditing Property to true for each cell on that row. But I am not able to get the DataGridCell object in the row. Or, there may be some better options.

<Window x:Class="MyTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        .....
        Title="MainWindow">
    <Grid>
        <DataGrid x:Name="MyDatagrid" IsReadOnly="True" AutoGenerateColumns="False" SelectionMode="Single">
            <DataGrid.Columns>
               <DataGridTemplateColumn Header="Edit">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="btnEdit" Content="Edit" Click="btnEdit_Click"></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
               </DataGridTemplateColumn>
                <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding MyClassId}" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MyClass and other relevant code:

public class MyClass
{
    [Key]
    public int MyClassId { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    .........
    .........
}

//DataGrid binding to source:
List<MyClass> lstMyClass = db.MyClass.ToList();
MyDatagrid.ItemsSource = lstMyClass;
............
.........

Upvotes: 0

Views: 4708

Answers (2)

mm8
mm8

Reputation: 169370

Set the CurrentCell property to a DataGridCellInfo and call BeginEdit() if you want to enter the edit mode programmatically:

private void btnEdit_Click(object sender, RoutedEventArgs e)
{
    MyDatagrid.IsReadOnly = false;
    MyDatagrid.CurrentCell = new DataGridCellInfo((sender as Button).DataContext, dataGrid.Columns[0]);
    MyDatagrid.BeginEdit();
}

Note that you have to set IsReadOnly to false to be able to enter the edit mode. You cannot edit a read-only DataGrid. You may set it back to true in the CellEditEnding event handler.

Upvotes: 0

박석양
박석양

Reputation: 39

 <DataGrid x:Name="MyDatagrid" AutoGenerateColumns="False" SelectionMode="Single">
        <DataGrid.Columns>
           <DataGridTemplateColumn Header="Edit">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ToggleButton x:Name="btnEdit" Content="Edit" IsChecked="{Binding Is_Checked}"></ToggleButton>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>
            <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding MyClassId}" />
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" IsReadOnly="{Binding Is_Checked}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" IsReadOnly="{Binding Is_Checked}"/>
        </DataGrid.Columns>
    </DataGrid>

First, Each Column Must Have IsReadOnly.

And IsReadOnly Binding MyClass's Is_Checked.

ToggleButton's IsChecked Binding MyClass's Is_Checked,too.

Then, When ToggleButton Click , Change Column's IsReadOnly.

All of this should be MVVM

public class MyClass
{
    [Key]
    public int MyClassId { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public bool Is_Checked{ get; set; }
    .........
    .........
}

Upvotes: 2

Related Questions