4

I have a datagridview in my Winforms Application. The user can click anywhere on the datagridview And then click a button to perform an action on that datarow However, in order to do this I need to recover the ID from that row Now bearing in mind the user probably hasnt clicked the ID column So SelectedCell.Value is no use to me here

I have tried the following code

DataGridViewRow row = dataIPs.SelectedRows[0];
DataGridViewCell cell = row.Cells[0];
string value = cell.Value.ToString();

But this produces an Out Of Bounds error.

How can I obtain the value from a specific column of the datagridview regardless of which column the user actually selects.

ANSWER

The code that worked for me in the end was as follows:

DataGridViewRow row = dataIPs.CurrentCell.OwningRow;
string value = row.Cells["IPID"].Value.ToString();
PJW
  • 5,197
  • 19
  • 60
  • 74

4 Answers4

3

You appear to be trying to access the SelectedRows collection when no row has been selected.

The default selection mode for the DataGridView is CellSelect and in this mode when you click on a cell no row is selected.

You need to either change the selection mode or get the desired row some other way.

  • You can change the selection mode to FullRowSelect which can be done in the designer or in code:

    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    
  • Or you can access the selected cell and the row from that:

    DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
    

    Or like this:

    DataGridViewRow row = dataGridView1.CurrentCell.OwningRow;
    
David Hall
  • 32,624
  • 10
  • 90
  • 127
  • Problem there is right click and double click on a cell produce different events. So I do need the user to be able to select individual cells. Hence I cant use fullrow select. I need a way then to recover the row index for the selected cell, without an available mouseeventargs. – PJW May 31 '12 at 13:56
  • @PJW see the second option - selecting the row based on the selected cell. You have to have something selected unless you store mouse click locations somewhere, since when they click on the button the mouse is no longer on the grid obviously. – David Hall May 31 '12 at 13:57
  • @PJW and there is always the option that sickunit proposed to introduce a column of button type. – David Hall May 31 '12 at 14:04
0

Try to get the value from event args.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • Its a button click - thats the issue I'm having - so the event args has no rowindex, columnindex etc. The 'value' from EventArgs will be the value in the highlighted cell, not necessarily the value from the column I want. For example, if my grid has 4 columns, then I want the value from the first column for the selected row, irrespective of which of the 4 columns is actually selected. – PJW May 31 '12 at 13:49
0

Set the SelectionMode property to FullRowSelect. This is needed for SelectedRow to work

Rupesh Saini
  • 85
  • 1
  • 7
0

You can also try to add a column with buttons in your datagridview. after that you can catch an event "CellContentClick" from your datagridview, which has "RowIndex" and "ColumnIndex" properties in eventargs.

sickUnit
  • 319
  • 2
  • 5