5 Replies - 5869 Views - Last Post: 05 February 2012 - 07:42 PM Rate Topic: -----

#1 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

datagridview get selected value problem

Posted 05 February 2012 - 06:56 PM

I have datagridviev with one column filled with data. When I click on the button1 I want value of selected cell to be displayed in the label. Usually I am using
Label1.text = DatagridView1.SelectedRows(0).Cells(0).value
now when the datagridview is filled with data it gets error "index is out of range".
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.text = DataGridView1.SelectedRows(0).Cells(0).value  ' error: index is out of range
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: datagridview get selected value problem

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: datagridview get selected value problem

Posted 05 February 2012 - 07:08 PM

Selectedrows works off a selection, the 0 is the first selected row. Try
Label1.Text = Datagridview1.Rows(0).Cells(0).Value

Was This Post Helpful? 0
  • +
  • -

#3 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: datagridview get selected value problem

Posted 05 February 2012 - 07:19 PM

View PostCharlieMay, on 05 February 2012 - 07:08 PM, said:

Selectedrows works off a selection, the 0 is the first selected row. Try
Label1.Text = Datagridview1.Rows(0).Cells(0).Value

but how can I get selected cell value from this if I don't know row/column index?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: datagridview get selected value problem

Posted 05 February 2012 - 07:30 PM

Sorry, I totally mis-read that

You can use
DataGridView1.CurrentCell.Value


Or
DataGridView1.CurrentRow.Cells(0).value

Was This Post Helpful? 1
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: datagridview get selected value problem

Posted 05 February 2012 - 07:35 PM

OK, for what you had above, in order for it to work, it appears you would have to select the entire row by clicking on the row header. I just tried what you had and by doing this, it displayed the cell(0) value. However, if I just clicked in the cell it gave the error you're receiving.

You could also use
DataGridView1.Rows(0).Cells(0).Value

This post has been edited by CharlieMay: 05 February 2012 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: datagridview get selected value problem

Posted 05 February 2012 - 07:42 PM

View PostCharlieMay, on 05 February 2012 - 07:35 PM, said:

OK, for what you had above, in order for it to work, it appears you would have to select the entire row by clicking on the row header. I just tried what you had and by doing this, it displayed the cell(0) value. However, if I just clicked in the cell it gave the error you're receiving.


Yes, now I understand. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1