Click here to Skip to main content
15,070,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is .xaml file code

<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>
</Window>


and .cs file 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 = 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;
         }
     }


how to edit datagrid value ??
Posted
Updated 28-Jan-15 0:35am
v2

1 solution

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;
}
}
   

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