Home All Groups Group Topic Archive Search About

how to use CurrentRowIndex for datagrid control ?

Author
29 Mar 2005 10:01 AM
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

Author
29 Mar 2005 10:52 AM
Ken Tucker [MVP]
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
--------------------
"Sam" <samuel.berthe***@voila.fr> wrote in message
news:1112090513.935395.212760@o13g2000cwo.googlegroups.com...
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
Author
29 Mar 2005 11:01 AM
Sam
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 ?
Author
29 Mar 2005 8:58 PM
M. Posseth
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]






Show quoteHide quote
"Sam" <samuel.berthe***@voila.fr> wrote in message
news:1112094096.867957.206530@f14g2000cwb.googlegroups.com...
> 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 ?
>