2017-02-24 54 views
-1

我在DataGrid中有n行的数据,DataGrid是ReadOnly。现在我的目标是当我选择任何行或行时,然后按下EDIT按钮,那么所有选定的行只会变成ReadOnly = false。以便我想编辑选定行中的一些数据。在此之后,当我按下更新按钮时,只有选定的行使用EntityFramework进行更新。 我在WinForm DataGridView中完成了这个任务。现在我想在WPF DataGrid中做同样的事情。等价的WinForm DataGridView代码WPF DataGrid

private void editCust_Click(object sender, EventArgs e) 
    { 
     if (dataGridView1.SelectedRows.Count > 0) 
     { 
      foreach (DataGridViewRow item in dataGridView1.SelectedRows) 
      { 
       for (int i = 1; i <= 3; i++) 
       { 
        item.Cells[i].ReadOnly = false; 
       } 
      } 
     } 

     else 
     { 
      MessageBox.Show("No Row Is Selected To Edit."); 
     } 
    } 

    private void updateCust_Click(object sender, EventArgs e) 
    { 
     if (dataGridView1.SelectedRows.Count > 0) 
     { 
      foreach (DataGridViewRow item in dataGridView1.SelectedRows) 
      { 
       cutable.Customer_Name = (string)item.Cells[1].Value; 
       cutable.Counter = int.Parse(Convert.ToString(item.Cells[2].Value)); 
       cutable.Buying_Cost = float.Parse(Convert.ToString(item.Cells[3].Value)); 

       for (int i = 1; i <= 3; i++) 
       { 
        item.Cells[i].ReadOnly = true; 
       } 
      } 

      db.SaveChanges(); 
      MessageBox.Show("Record Is Update."); 
     } 

     else 
     { 
      MessageBox.Show("No Row Is Selected To Update."); 
     } 
    } 
+0

你能解释什么是逻辑或什么是你的代码背后的动机,以便我们可以给你一个Datagrid的解决方案 –

+0

Thanks @Learning for the reply。 在WPF中,我在DataGrid中有n行的数据,而DataGrid是ReadOnly。现在我的目标是当我选择任何行或行时,然后按下EDIT按钮,那么所有选定的行只会变成ReadOnly = false。以便我想编辑选定行中的一些数据。在此之后,当我按下更新按钮,那么只有选定的行使用EntityFramework –

+0

更新域名模型/表名称是什么?在哪些数据将被更新 –

回答

0

首先转换到DataGridView然后DataTableDataGrid

对于DataGridView的数据表来

使用此:

DataTable myDataTable=(DataTable)(myDataGridView.DataSource); 

更多参考经过DataGridView to DataTable

现在对于DataTable的DataGrid中来

使用此:

myDataGrid.ItemsSource=myDataTable.DefaultView; 
myDataGrid.AutoGenerateColumns = true; 
myDataGrid.CanUserAddRows = false; 

更多参考经过DataTable to DataGrid

评论对任何查询

UPDATE:

尝试创建自己的代码和逻辑,不要依赖于某人提供的直接解决方案。

以及在StackOverflow [解决]中发现的类似问题。

参考这个问题how to edit select row in datagrid in wpf

希望它能帮助!

+0

我想要WPF DataGrid中的Eqiavalent WinForm DataGridView代码。 –