简体   繁体   中英

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. 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
         }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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