2013-03-11 29 views
0
private void dgvComp_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
    { 
     if (dgvComp.Rows[dgvComp.CurrentRow.Index].Cells["EmplId"].ToString() != "") 
     { 
      if (dgvComp.Columns[e.ColumnIndex].Name == "EmplId") 
      { 

       { 

        DataTable dt = new DataTable(); 

        string auto = dgvComp.Rows[e.RowIndex].Cells["EmplId"].EditedFormattedValue.ToString(); 
        dt = dataAcc.rtrvData("empl_Name,empl_Id,desg_Name", "dbo.Empldmgrphcs INNER JOIN dbo.Designation ON dbo.Empldmgrphcs.desg_Id = dbo.Designation.desg_Id ", "empl_EmpId='" + auto + "'"); 
        dgvComp.Rows[e.RowIndex].Cells["dgvname"].Value = dt.Rows[0][0].ToString(); 
        dgvComp.Rows[e.RowIndex].Cells["empID"].Value = dt.Rows[0][1].ToString(); 
        dgvComp.Rows[e.RowIndex].Cells["dgvdesi"].Value = dt.Rows[0][2].ToString(); 
       } 
      } 
     } 
    } 

上面的代碼是我使用填補DataGridView什麼,但是當一個細胞是空的,我得到錯誤。當細胞是空的我要顯示的消息,否則運行過程

如何處理空單元格的條件?

+0

你得到什麼錯誤? – ChrisF 2013-03-11 12:59:47

+0

請分享錯誤及其發生的路線。 – Khan 2013-03-11 13:00:14

+0

位置0沒有行。錯誤 – user1918410 2013-03-11 13:01:26

回答

0

顯示一個消息框,並設置:e.Cancel = true;

0
private void dgvComp_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
{ 
    if (dgvComp.Rows[dgvComp.CurrentRow.Index].Cells["EmplId"].ToString() != "") 
    { 
     if (dgvComp.Columns[e.ColumnIndex].Name == "EmplId") 
     { 
     { 
      DataTable dt = new DataTable(); 

      string auto = dgvComp.Rows[e.RowIndex].Cells["EmplId"].EditedFormattedValue.ToString(); 
      dt = dataAcc.rtrvData("empl_Name,empl_Id,desg_Name", "dbo.Empldmgrphcs INNER JOIN dbo.Designation ON dbo.Empldmgrphcs.desg_Id = dbo.Designation.desg_Id ", "empl_EmpId='" + auto + "'"); 

      if(dt.Rows.Count > 0) 
      { 
       dgvComp.Rows[e.RowIndex].Cells["dgvname"].Value = dt.Rows[0][0].ToString(); 
       dgvComp.Rows[e.RowIndex].Cells["empID"].Value = dt.Rows[0][1].ToString(); 
       dgvComp.Rows[e.RowIndex].Cells["dgvdesi"].Value = dt.Rows[0][2].ToString(); 
      } 
      else 
      { 
       e.Cancel = true; 
       MessageBox.Show("Validation Fail"); 
      } 
     } 
     } 
    } 
} 
+0

你不需要'如果你在'if'語句之後返回'else'。 :) – Khan 2013-03-11 13:03:42