简体   繁体   English

如何检查单元格是否为空。 c#/datagridview

[英]How to check if the cell is empty. c#/datagridview

I've got an event handler for a cell click.我有一个用于单击单元格的事件处理程序。 I want to check if the cell i click on contains something.我想检查我单击的单元格是否包含某些内容。 If not then send a error message.如果不是,则发送错误消息。 In addition to this, i only want the user to be able to click on the first 2 columns, and if any other columns are clicked then it wont do anything.除此之外,我只希望用户能够单击前 2 列,如果单击了任何其他列,则它不会执行任何操作。 Any ideas?有任何想法吗?

FirstName   |   LastName   | Monday   | Tuesday   |   Wednesday  |
            |              |          |           |              |
William     |   Oliver     |          |           |              |
James       |   Price      |          |           |              |

So if the names are selected then it will do something.因此,如果选择了名称,那么它会做一些事情。 If the blank cells in Monday/Tuesday etc are clicked then nothing will happen.如果单击星期一/星期二等中的空白单元格,则不会发生任何事情。

Hope this makes sense.希望这是有道理的。

code:代码:

private void metroDataGrid1_CellClick(object sender, DataGridViewCellEventArgs e) 
{
     //if Statement to see if cell contains anything, if it does then open a new form below...

    frmUserDiary userdiaryclick = new frmUserDiary();
    userdiaryclick.ShowDialog();
}

Thanks谢谢

Updated code:更新代码:

if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
{
    frmUserDiary userdiaryclick = new frmUserDiary();
    userdiaryclick.ShowDialog();
}

I managed to figure the answer out.我设法弄清楚了答案。 Thanks谢谢

What you are trying to achieve should be something like this你想要实现的应该是这样的

private void metroDataGrid1_CellClick(object sender, DataGridViewCellEventArgs e) 
{

//IF Statement to see if cell contains anything, if it does then open a new form below...
         if (metroDataGrid1.CurrentCell != null && metroDataGrid1.CurrentCell.Value != null)
         {
             var cellValue = metroDataGrid1.CurrentCell.Value.ToString();
             frmUserDiary userdiaryclick = new frmUserDiary(); 
             userdiaryclick.ShowDialog();
         }
         else   
         { 
            // do something
         }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM