不能更改数据视图中的行值


  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
                     }
                     );//get all data from tables
        dataGridView1.DataSource = query;


          for (int i = 0; i < dataGridView1.RowCount; i++)
          {
              int currentcapacity;
              Int32.TryParse(dataGridView1.Rows[i].Cells[6].Value.ToString(),out currentcapacity);
              String idA = dataGridView1.Rows[i].Cells[0].Value.ToString(); //read allocation ID on cell 0
              var cek = (from x in de.DetailTrainingAllocations
                         where x.AllocationID==idA
                         select x).Count(); //to get amount of data in table detailTransaction
              dataGridView1.Rows[i].Cells[6].Value =currentcapacity- cek;//this code won't change the value of the rows in datagridview
          }
    }

这段代码工作得很好,但由于某种原因,最后一段代码没有工作,X行第6列的值根本没有改变。我几乎没有解决这个问题的办法。

这是最直接的转换,尽管您确实应该对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;
    }

相关内容

  • 没有找到相关文章

最新更新