PJW
PJW

Reputation: 5397

Testing DataGridView to see if Cell has been Selected

I have a Winforms application where several events can trigger the following code . . .

DataGridViewRow row = (DataGridViewRow)dataContactBusiness.CurrentCell.OwningRow;
int busID = (int)row.Cells["ID"].Value;
//...further processes utilising this ID

Most of the time this is OK, but it is possible for this code to be triggered before the User has selected a row on the DataGridView or even before the DataGridView is populated. When this happens I get an exception 'object not set to an instance' etc

What is the best way to test to see if

(a) The DataGridView has data, AND (b) The User has selected a cell or row in that DataGridView

Upvotes: 0

Views: 3538

Answers (1)

PJW
PJW

Reputation: 5397

if (dataContactBusiness.CurrentCell != null) did the trick

Upvotes: 2

Related Questions