2014-01-22 39 views
1

我希望我的第一列數據網格不爲空。所以我創建了這個檢查,如果字符串爲空或空。字符串爲空或爲空的數據網格

private void provjeri_unose() 
    { 
     string šifra = "šifra"; 
     for (int t = 0; t < dataGridView1.Rows.Count; t++) 
     { 
      šifra = Convert.ToString(dataGridView1.Rows[t].Cells[0].Value); 
     } 
     Proizvod.Šifra = šifra; 
     if (string.IsNullOrEmpty(Proizvod.Šifra)) 
     { 
      MessageBox.Show("Šifra mora biti unesena"); 
     } 
    } 

我進入裏面在首列的數據網格的價值,但它仍然顯示消息框,如果第一列內沒有輸入數據,只顯示。 我的問題是,我犯了什麼錯誤。

+0

你的目標是什麼樣的應用,我認爲你有一個空的最後一行,並因爲你迭代所有行它顯示最後一行的單元格的值。 – Habib

+0

+是否要確保網格第一列中沒有空單元格,還是要確保第一列第一行單元格不是空的? – David

+0

我想你給了我一個想法,改變了一些代碼,並加上了-1來反擊。現在工作! – mateoZD

回答

0

工作代碼:

private void provjeri_unose() 
    { 
     string šifra = "šifra"; 
     for (int t = 0; t < dataGridView1.Rows.Count-1; t++) 
     { 
      DataGridViewRow row = dataGridView1.Rows[t]; 
      if (row.IsNewRow) break; 
      šifra = Convert.ToString(dataGridView1.Rows[t].Cells[0].Value); 
      Proizvod.Šifra = šifra; 
      if (string.IsNullOrEmpty(Proizvod.Šifra)) 
      { 
       MessageBox.Show("Šifra mora biti unesena"); 
      } 
     } 

    } 
+1

這不會檢查最後一行中的單元格。將'for(int t = 0; t David

-1
  gridgetrequest.DataSource = dt; 
      gridgetrequest.DataBind(); 

      string emptydata = dt.Rows[0]["processdate"].ToString(); 

      if (dt.Rows.Count > 0) 
      { 

       if (emptydata == "") 
       { 
        foreach (GridViewRow row in gridgetrequest.Rows) 
        { 
         Label checkPRNNo = (Label)row.FindControl("lblprocessdate"); 

         checkPRNNo.Text = "anand"; 
        } 



       } 
      }