Joy
Joy

Reputation: 157

how to edit select row in datagrid in wpf

i have a button column edit corresponding to each row in datagrid on click of that button i want to get correessponding row edit value how can we do that

xaml code is :-

<Window x:Class="Wpf_grid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="326" Width="946">
<Grid>
    <DataGrid Name="MyDataGrid" Uid="MyDataGrid" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" Margin="0,31,0,0" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=RegId}" IsReadOnly="True" Header="Registration Id" Width="sizeToHeader"/>
            <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=FName}" Header="Father Name" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=ContactNumber}" Header="Contact Number" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=email}" Header="email" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=password}" Header="password" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=paddress}" Header="paddress" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=pcity}" Header="pcity" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=pstate}" Header="pstate" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=ppinCode}" Header="ppinCode" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=caddress}" Header="caddress" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=ccity}" Header="ccity" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=cstate}" Header="cstate" Width="sizeToHeader" />
            <DataGridTextColumn Binding="{Binding Path=cpinCode}" Header="cpinCode" Width="sizeToHeader" />
            <DataGridTemplateColumn Header="Edit Row">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Edit" Click="btnEdit_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Delete Row">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete" Click="btnDelete_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

    <Button Content="LoadCustomer" Name="LoadCustomer" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="0,5,0,0" Click="LoadCustomer_Click" />

</Grid>

and .cs code is :-

    private void btnEdit_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            gridDataContext datacontext = new gridDataContext();
            registration registrationRow = MyDataGrid.SelectedValue as registration;
            int m = registrationRow.RegId;
            //registration Registration = (from p in datacontext.registrations where p.RegId == registrationRow.RegId select p).Single();
            registration Registration = datacontext.registrations.Where(A => A.RegId == m).Single();
            Registration.Name = registrationRow.Name;
            Registration.FName = registrationRow.FName;
            datacontext.SubmitChanges();
            MessageBox.Show("Row Updated Successfully");
            LoadCustomerDetail();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

anyone tell me how can i edit on button click ??

Upvotes: 2

Views: 6011

Answers (2)

Joy
Joy

Reputation: 157

finally I got solution... we can create an object which is store row data. now i can access each cell item like that

here is code:-

 private void btnEdit_Click(object sender, RoutedEventArgs e)
    {
        try
        {
        object item = MyDataGrid.SelectedItem;
            gridDataContext datacontext = new gridDataContext();

            int m = int.Parse((MyDataGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text);

            registration Registration = datacontext.registrations.Where(A => A.RegId == m).Single();
            Registration.Name = (MyDataGrid.SelectedCells[1].Column.GetCellContent(item) as TextBlock).Text;
            Registration.FName = (MyDataGrid.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;
            datacontext.SubmitChanges();
            MessageBox.Show("Row Updated Successfully");
            LoadCustomerDetail();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

Upvotes: 0

Amit Raz
Amit Raz

Reputation: 5536

  1. Add Binding for SelectedItem property in the grid to your ViewModel
  2. Add Binding for Command property of the Button to a Command in your ViewModel
  3. In the ViewModel implement the Command.Executed method and use the Selected item propety to get the data.

Upvotes: 2

Related Questions