本文介绍了如何编辑wpf中的选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是.xaml文件代码
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>
和.cs文件代码是: -
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;
}
}
如何编辑datagrid值?
how to edit datagrid value ??
推荐答案
这篇关于如何编辑wpf中的选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!