WilliamArm
WilliamArm

Reputation: 17

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

Upvotes: 0

Views: 91

Answers (2)

WilliamArm
WilliamArm

Reputation: 17

Updated code:

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

I managed to figure the answer out. Thanks

Upvotes: 1

Shahid Manzoor Bhat
Shahid Manzoor Bhat

Reputation: 1335

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
         }

}

Upvotes: 0

Related Questions