wpf 如何在wpf中编辑数据网格中的选择行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28191766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to edit select row in datagrid in wpf
提问by Joy
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 :-
xaml 代码是:-
<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 :-
和 .cs 代码是:-
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 ??
谁能告诉我如何在单击按钮时进行编辑?
回答by Amit Raz
- Add Binding for SelectedItem property in the grid to your ViewModel
- Add Binding for Command property of the Button to a Command in your ViewModel
- In the ViewModel implement the Command.Executed method and use the Selected item propety to get the data.
- 将网格中 SelectedItem 属性的绑定添加到您的 ViewModel
- 将按钮的 Command 属性绑定添加到 ViewModel 中的命令
- 在 ViewModel 中实现 Command.Executed 方法并使用 Selected 项属性来获取数据。
回答by Joy
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;
}
}

