C# 与WPF DataGrid等效的WinForm DataGridView代码
我在DataGrid中有一个数据,有n行,DataGrid是只读的。现在,我的目标是,当我选择任何一行或多行,然后按编辑按钮时,所有选定的行仅变为ReadOnly=false。因此,我想编辑选定行中的一些数据。之后,当我按下更新按钮时,使用EntityFramework仅更新选定的行。C# 与WPF DataGrid等效的WinForm DataGridView代码,c#,wpf,winforms,datagridview,datagrid,C#,Wpf,Winforms,Datagridview,Datagrid,我在DataGrid中有一个数据,有n行,DataGrid是只读的。现在,我的目标是,当我选择任何一行或多行,然后按编辑按钮时,所有选定的行仅变为ReadOnly=false。因此,我想编辑选定行中的一些数据。之后,当我按下更新按钮时,使用EntityFramework仅更新选定的行。 我在WinFormDataGridView中完成了这项任务。现在我想在WPF DataGrid中实现同样的功能 private void editCust_Click(object sender, EventAr
我在WinFormDataGridView中完成了这项任务。现在我想在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.");
}
}
private void editCust\u单击(对象发送方,事件参数e)
{
如果(dataGridView1.SelectedRows.Count>0)
{
foreach(dataGridView1.SelectedRows中的DataGridViewRow项)
{
对于(int i=1;i 0)
{
foreach(dataGridView1.SelectedRows中的DataGridViewRow项)
{
cutable.Customer_Name=(字符串)item.Cells[1]。值;
cutable.Counter=int.Parse(Convert.ToString(item.Cells[2].Value));
cutable.Buying_Cost=float.Parse(Convert.ToString(item.Cells[3].Value));
对于(int i=1;i首先将DataGridView
转换为DataTable
然后转换为DataGrid
将DataGridView转换为DataTable
使用以下命令:
DataTable myDataTable=(DataTable)(myDataGridView.DataSource);
myDataGrid.ItemsSource=myDataTable.DefaultView;
myDataGrid.AutoGenerateColumns = true;
myDataGrid.CanUserAddRows = false;
如需更多参考,请浏览
现在将DataTable转换为DataGrid
使用以下命令:
DataTable myDataTable=(DataTable)(myDataGridView.DataSource);
myDataGrid.ItemsSource=myDataTable.DefaultView;
myDataGrid.AutoGenerateColumns = true;
myDataGrid.CanUserAddRows = false;
如需更多参考,请浏览
任何查询的注释
更新:
尝试创建自己的代码和逻辑,不要依赖于别人给出的直接解决方案
我在StackOverflow中发现了一个类似的问题[已解决]
参考这个问题
希望有帮助!我想要WPF DataGrid中的EQIALENT WinForm DataGridView代码。你能解释一下代码背后的逻辑或动机吗,这样我们就可以为DataGrid提供一个解决方案谢谢@Learning的回复。在WPF中,我在DataGrid中有一个包含n行的数据,DataGrid是只读的。现在我的目标是选择任意一行或多行,然后按“编辑”按钮,则所有选定的行仅变为ReadOnly=false。因此,我想编辑选定行中的一些数据。在此之后,当我按下更新按钮时,仅使用EntityFramework更新选定的行。域模型/表名的名称是什么?将在其中更新数据。我在WinForm DataGridView中完成此任务。现在,我希望在WPF DataGrid中完成相同的任务。请参见上文。我更新了上面的帖子描述和代码。请参见更新的st\u甚至单击t在上面。