无法更改datagridview中的行值

时间:2015-05-24 03:15:51

标签: c# linq datagridview

JavaScript

此代码运行正常,但最后一段代码由于某种原因无效,第6列第X行的值根本没有变化。在我看来,我几乎没有解决方案。

1 个答案:

答案 0 :(得分:0)

这是最直接的转换,虽然你真的应该在DetailTrainingAllocations上进行JOIN,而不是做一个循环,为每条记录做一个查询。

 void refreshtable()  
    {
        var query = (from x in de.HeaderTrainingAllocations
                     join y in de.MsTrainings on x.TrainingID equals y.TrainingID
                     join z in de.MsUsers on x.UserID equals z.UserID
                  select new
                     {
                         x.AllocationID,
                         x.TrainingID,
                         z.UserName,
                         x.TrainingStartDate,
                         TrainingEndDate = System.Data.Objects.EntityFunctions.AddDays(x.TrainingStartDate, ((y.TrainingDuration - 1) * 7)),
                         y.TrainingDuration,
                         x.Capacity
                     }
                     ).ToList();//get all data from tables
          foreach (var z in query)
          {
              var cek = (from x in de.DetailTrainingAllocations
                         where x.AllocationID==z.AllocationID
                         select x).Count(); //to get amount of data in table detailTransaction

              z.Capacity-=cek;//this code won't change the value of the rows in datagridview
          }

        dataGridView1.DataSource = query;
    }