如何在wpf中编辑datagrid中的选择行

时间:2015-01-28 12:00:31

标签: wpf datagrid

我有一个按钮列编辑对应于datagrid中的每一行点击该按钮我想获得相应的行编辑值我们怎么能这样做

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>

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

任何人都告诉我如何点击按钮进行编辑?

2 个答案:

答案 0 :(得分:1)

  1. 将网格中SelectedItem属性的绑定添加到ViewModel
  2. 将Button的Command属性的绑定添加到ViewModel中的命令
  3. 在ViewModel中实现Command.Executed方法并使用Selected item propety来获取数据。

答案 1 :(得分:0)

最后我得到了解决方案...... 我们可以创建一个存储行数据的对象。现在我可以访问每个单元格项目

这里是代码: -

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