Click here to Skip to main content
15,063,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a button column edit corresponding to each row in datagrid on click of that button i want to get corresponding 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 ??
Posted
Updated 14-Aug-18 0:35am

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;
        }
    }
   
Comments
Member 13948738 14-Aug-18 6:33am
   
i want code for Edit,Update,Delete,Cancel buttons in datagrid by using WPF with SQLSERVER
Hello
Am new to WPF iam struggling with datagrid
Edit,UPdate,Delete,Cancel buttons inside Datagrid table by using Wpf technology in sqlserver
   
Comments
Graeme_Grant 14-Aug-18 8:48am
   
Already answered. Please start a new question. But please read posting FAQ first: Code Project Quick Answers FAQ[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900