在WPF应用程序中使用DataGrid使用LINQ更新数据库。 [英] Using DataGrid to Update Database using LINQ in a WPF aaplication.

查看:61
本文介绍了在WPF应用程序中使用DataGrid使用LINQ更新数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个包含三个字段的DataGrid,还有一个Checkbox。在检查复选框时,我想更新数据库。我试过这样做。但这些数值并没有在数据库中反映出来。更新数据的表没有主键。这也很重要吗?



这是代码。

Hello,
I have a DataGrid with three fields and also a Checkbox. On the check of the Checkbox, i want to update the DataBase. I have tried doing it. But the values are not reflecting in the database. The table to which am updating the data has no primary key. Will it also matter??

Here is the Code.

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    using (DataClasses1DataContext context = new DataClasses1DataContext())
    {

        DataGridCellInfo defect = DataGrid.SelectedCells[0];

        string value = ((TextBlock)defect.Column.GetCellContent(defect.Item)).Text;

        int DefectID = Convert.ToInt32(value);

        var updateStatus = (from maintain in context.MaintenanceDefects
                            where maintain.DefectID == DefectID
                                select maintain).ToList();

         foreach (var pvalue in updateStatus)
        {
            System.Console.WriteLine(updateStatus[0].DefectStatus);
            System.Console.WriteLine(updateStatus[0].DefectID);
            System.Console.WriteLine(updateStatus[0].MaintenanceID);

            updateStatus[0].DefectStatus = "Fixed";

            context.SubmitChanges();

        }
    }
}





请提出解决方案。



谢谢&问候

raj_arenem



Kindly suggest the solution.

Thanks & regards
raj_arenem

推荐答案

我看了一眼。我认为问题出在foreach循环中:

I had a quick look. I think the problem is in the foreach loop:
foreach (var pvalue in updateStatus)
 {
 System.Console.WriteLine(updateStatus[0].DefectStatus);
 System.Console.WriteLine(updateStatus[0].DefectID);
 System.Console.WriteLine(updateStatus[0].MaintenanceID);

 updateStatus[0].DefectStatus = "Fixed";

 context.SubmitChanges();

 }





它没有更新DefectStatus,因为foreach循环没有触发。在列表执行之前,确保名为updateStatus的列表变量中有数据。尝试类似:





Its not updating DefectStatus because the foreach loop isn''t triggering. Ensure there''s data in the list variable named updateStatus before the list executes. Try something like:

if (updateStatus.Count == 0) //Might be: (updateStatus.Count() == 0)
{
  //Throw no data exception and handle it
}

for (int i = 0; i < updateStatus.Count; i++)
{
   //Do work here and submit
}


这篇关于在WPF应用程序中使用DataGrid使用LINQ更新数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆