2012-08-23 115 views
0

我需要一些帮助,一个为每个语句,基本上发生的事情是,当用户在小区内编辑的值,我的foreach将在DataGrid中应用此于所有细胞,并改变他们所有的价值,我需要我的foreach语句通过数据网格迭代工作,但只改变已被编辑全部C#foreach语句

try 
{ 
    //loop through each of the rows in the dgv 
    foreach (DataGridViewRow row in dgvDetials.SelectedRows) 
    { 
     int intQtyInsp = 0; 

     //if quantity inspected is empty: 
     if (row.Cells[2].Value.ToString() == "") 
     { 
      //quantity inspected is 0. Prevents null value errors: 
      intQtyInsp = 0; 
     } 

     intQtyInsp = 
      Int32.Parse(dgvDetials.CurrentRow.Cells[2].Value.ToString()); 

     if (intQtyInsp < 0) // checks the cell for a negative value 
     { 
      intQtyInsp = 0; // if cells is negative submits value as Zero 
     } 
     else 
     { 
      //sets quantity inspected to value entered 
      intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString()); 
     } 

     if (intQtyInsp == 0) //if quantity inspected is 0. Ignore row. 
     { 

     } 
     else //else gather details and insert row as production. 
     { 
      area = dtArea2.Rows[0]["area_code"].ToString(); 
      inspDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy"); 
      inspShift = cbShift.Text; 
      partNo = row.Cells[0].Value.ToString(); 
      // dieCode = row.Cells[0].Value.ToString(); 
      dieCode = ""; 
      machine = ""; 
      qtyInsp = intQtyInsp; 
      qtyInspRecorded = Int32.Parse(row.Cells[5].Value.ToString()); 
      comment = ""; 
      //machine = row.Cells[3].Value.ToString(); 

      if (qtyInspRecorded == 0) 
      { 
       SQLMethods.insertProduction(area, 
              inspDate, 
              inspShift, 
              partNo, 
              dieCode, 
              qtyInsp, 
              comment, 
              machine); 
      } 
      else 
      { 
       SQLMethods.updateProduction(area, 
              inspDate, 
              inspShift, 
              partNo, 
              dieCode, 
              (qtyInspRecorded + qtyInsp), 
              comment, 
              machine); 
      } 
     } 
    } 
    retrieveData(); //reset values 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(
     "Error instering production values. Processed with error: " 
     + ex.Message); 
}  
+2

这是一个很大的代码,并没有问题。你发布的代码有什么问题? –

+0

不能看到任何错误 – Asken

+0

使用'Linq'只得到这一行,你需要改变。人的问题是如何只更新1更改的行不是所有'DataGrid' – harry180

回答

1

首先选定行,我会被拆分为几种方法可称为此处略简化代码从For循环。这会让阅读更容易,从而也更容易帮助你。只需提供一个例子,以下内容:

int intQtyInsp = SetQuantityInspected(); 

然后该方法可以包含如结构:

if (intQtyInsp < 0) // checks the cell for a negative value 
{ 
    intQtyInsp = 0; // if cells is negative submits value as Zero 
} 
else 
{ 
    //sets quantity inspected to value entered 
    intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString()); 
} 

可能喜欢的东西来代替。对循环中代码的其他部分重复此操作。相信我,这会让你的生活更轻松。

而且,它好像是从来没有使用过这部分的结果;的intQtyInsp值覆盖右后!:

if (row.Cells[2].Value.ToString() == "") 
{ 
    //quantity inspected is 0. Prevents null value errors: 
    intQtyInsp = 0; 
} 

至于你的问题:我不知道你将如何获得当前正在编辑的行的id。 (如果可能(?),它可能是getter循环访问datagrid后面的表/数据源?)。

在任何情况下,你需要做的是一样的东西你的循环内的以下内容:

if(IsCurrentlyEditedRow(row)){ 
    ... 

    // (all the stuff currently in the body of your loop goes here) 
    ... 
} 

现在,您可以实现该方法IsCurrentlyEditedRow()返回TrueFalse根据的的ID是否当前行与您正在编辑的行相同。

很抱歉,如果这不是一个非常具体和详细的​​解答,希望这是一些使用的反正。

+1

看到它周围编辑当前行的第一次,当我输入了第一时间的值,但它是当我重新编辑同一行,当所有行更新 –