当一个单元格为空时我想显示消息,否则运行一个进程

时间:2013-03-11 12:50:47

标签: c# winforms datagridview

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的代码,但是当一个单元格为空时,我收到错误。

如何处理空单元格的条件?

2 个答案:

答案 0 :(得分:0)

显示一个消息框并设置:e.Cancel = true;

答案 1 :(得分: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");
            }
         }
      }
   }
}