noushad mohammed
noushad mohammed

Reputation: 375

How to get the first cell value of selected row of a gridview?

error while using cells

I have Gridview with several columns. When I select particular row of gridview, I am not able to get the value of first cell. Its showing following error.

Error 15 'System.Windows.Forms.DataGridView' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 0

Views: 1162

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

Mihai Caracostea Showed your problem correctly.
Just to learn how to work with selected row(s) please consider these notes:

  • If you want only one row could be selected, set MultiSelect yo false
  • Check yourGrid.SelectedRows.Count==1if you want to check if there is a selected row
  • When you want to read selected row values, first check if there is selection then use yourGrid.SelectedRows[0].Cells[index of column] or yourGrid.SelectedRows[0].Cells[nam of column]

Upvotes: 1

Mihai Caracostea
Mihai Caracostea

Reputation: 8466

You must do

foreach (DataGridViewRow row in ... )

You have a wrong type in your iteration variable.

Upvotes: 3

Related Questions