how to use CurrentRowIndex for datagrid control ?

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi,
I'm trying to get the value of an item in my datagrid. When I click on
a button I call the following code :

Dim selectedRow As Integer = dgEquivalents.CurrentRowIndex()
Dim test As Integer = dgEquivalents.Item(selectedRow, 0)

It works, the only issue is that dgEquivalents.CurrentRowIndex() always
return 0 :(
Why ???
Thx
 
Hi,

Use the datatables default view to return right datarow. Quick
example.


Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

DataGrid1.DataSource = ds.Tables("Categories")

End Sub



Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.DoubleClick

Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager)

Dim drv As DataRowView

drv = ds.Tables("Categories").DefaultView.Item(cm.Position)

MessageBox.Show(drv.Item("CategoryName").ToString)

End Sub



Ken
--------------------
Hi,
I'm trying to get the value of an item in my datagrid. When I click on
a button I call the following code :

Dim selectedRow As Integer = dgEquivalents.CurrentRowIndex()
Dim test As Integer = dgEquivalents.Item(selectedRow, 0)

It works, the only issue is that dgEquivalents.CurrentRowIndex() always
return 0 :(
Why ???
Thx
 
Thanks,
i've tried that and it is the exact same issue.... Whichever row I
select, the position returned is always 0...
any idea why ?
 
well as far as i know the datagrid is column based so

if you use the rowindex you should first select a row

like this


Private Sub dgEquivalents_CurrentCellChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles dgEquivalents.CurrentCellChanged
dgEquivalents.Select(dgEquivalents.CurrentCell.RowNumber())

Dim selectedRow As Integer = dgEquivalents.CurrentRowIndex()
Dim test As Integer = dgEquivalents.Item(selectedRow, 0)




End Sub

ofcourse this could also be like this

Private Sub dgEquivalents_CurrentCellChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles dgEquivalents.CurrentCellChanged
dgEquivalents.Select(dgEquivalents.CurrentCell.RowNumber())' if you like the
row selected

Dim test As Integer =
dgEquivalents.Item(dgEquivalents.CurrentCell.RowNumber(), 0)




End Sub



correct me if i am wrong :-)

Michel Posseth [MCP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top