2014-06-14 29 views
0

我有一個dataGridView和一個按鈕,當我點擊按鈕時,每行的第三個單元格包含值「1」,但我希望只有包含數據的行應在第三列包含「1」 。 我想這個代碼使得那些之前對其進行測試,但結果顯示的圖像:我得到第三列「一」的所有行!

private void button4_Click(object sender, EventArgs e) 
     { 
      DateTime dt = DateTime.Now; 
      dataGridView1.Rows.Add("" + dt.Year.ToString() + "", "En cours", "0"); 


      if (dataGridView1.SelectedRows.Count == 1) 
      { 
       DataGridViewRow row = dataGridView1.SelectedRows[0]; 

       for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 
        // int ik = (int)dataGridView1.Rows[i].Cells[3].Value; 
        foreach (DataGridViewRow row1 in dataGridView1.Rows) 
        { 
         foreach (DataGridViewCell cell in row1.Cells) 
         { 
          if (cell.Value != null) 
          { 
           dataGridView1.Rows[i].Cells[2].Value = 1; 
          } 
          else //if (string.IsNullOrEmpty(cell.Value.ToString())) 
          { 
           break; 
          } 


         } 
        } 
       } 
      } 
     } 

結果:爲幫助

enter image description here

感謝

+0

我希望dataGridView中的每一行在第三列中包含值「1」,只有包含數據的行,但對於其他空行(cell.value == null,有必要把「1」):) – Lina

+0

我需要幫助,請:'( – Lina

回答

1

也許這將幫助:

private void button4_Click(object sender, EventArgs e) 
     { 
      DateTime dt = DateTime.Now; 
      dataGridView1.Rows.Add("" + dt.Year.ToString() + "", "En cours", "0"); 


      if (dataGridView1.SelectedRows.Count == 1) 
      { 
       DataGridViewRow row = dataGridView1.SelectedRows[0]; 

       for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 
        // int ik = (int)dataGridView1.Rows[i].Cells[3].Value; 
        foreach (DataGridViewRow row1 in dataGridView1.Rows) 
        { 
         foreach (DataGridViewCell cell in row1.Cells) 
         { 
          string st = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value); 
          if (cell.Value == null && st == "0") 
          { 
           dataGridView1.Rows[i].Cells[2].Value = 1;         
          } 
          else //if (string.IsNullOrEmpty(cell.Value.ToString())) 
          {         
           break;         
          } 
         } 
        } 
       } 
      }    
     } 

編輯

我不確定有些事情應該像你這樣做,這個項目的結果是什麼。但我修改和調試它現在正在工作的代碼,我希望它會幫助

+0

感謝杜桑,但與計數器當我試圖調試代碼,調試器無法訪問「如果條件」喘息的行不空,再次感謝您的幫助 – Lina

+0

對不起Dusan,但它並沒有幫助我:( – Lina

+0

看到上面的編輯 – Dusan

相關問題