By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Returning correct datarow when datagrid is sorted

P: n/a
I'm sure this is a very dumb question but when a user clicks on a row in my
data table (which has been sorted) how do I return the correct row index for
my underlying data table? The CurrentRowIndex property gives me the selected
row of the data grid which no longer matches the data table when the columns
have been resorted.

Thanks!!!
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
One of the easier ways to deal with this scenario is to just address the
data via the Grid's properties, ie dataGrid1(0,2) . That will give you the
value of the beginning row in the grid at the second column. You can set
that value as you see fit and whatever you do to that cell will be reflected
in the underlying data object. In most instances, this should get you what
you want. If this won't work though, let me know and I'll walk you through
a few other ways.

cheers,

Bill

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/...ity/newsgroups
"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
I'm sure this is a very dumb question but when a user clicks on a row in my data table (which has been sorted) how do I return the correct row index for my underlying data table? The CurrentRowIndex property gives me the selected row of the data grid which no longer matches the data table when the columns have been resorted.

Thanks!!!

Nov 20 '05 #2

P: n/a
Thank you for the quick reply!!

Unfortunately this scenario won't work for me, my data grid does not contain
any unique fields and when the database is small it probably won't make any
difference, but as it grows so does the potential for duplicates. The
underlying data table does have an ID Primary Key, but that column does not
show in the data grid. I need to make sure that the row they select matches
the correct data table row.

I appreciate the help!
"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
One of the easier ways to deal with this scenario is to just address the
data via the Grid's properties, ie dataGrid1(0,2) . That will give you the value of the beginning row in the grid at the second column. You can set
that value as you see fit and whatever you do to that cell will be reflected in the underlying data object. In most instances, this should get you what you want. If this won't work though, let me know and I'll walk you through a few other ways.

cheers,

Bill

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/...ity/newsgroups
"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
I'm sure this is a very dumb question but when a user clicks on a row in

my
data table (which has been sorted) how do I return the correct row index

for
my underlying data table? The CurrentRowIndex property gives me the

selected
row of the data grid which no longer matches the data table when the

columns
have been resorted.

Thanks!!!


Nov 20 '05 #3

P: n/a
Hi,

Use the defaultview for the datatable. Here is an 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.Positi on)

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

End Sub

Ken

---------------------------

"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
I'm sure this is a very dumb question but when a user clicks on a row in
my
data table (which has been sorted) how do I return the correct row index
for
my underlying data table? The CurrentRowIndex property gives me the
selected
row of the data grid which no longer matches the data table when the
columns
have been resorted.

Thanks!!!

Nov 20 '05 #4

P: n/a
That worked perfectly! Thanks for the help!!!!
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
Hi,

Use the defaultview for the datatable. Here is an 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.Positi on)

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

End Sub

Ken

---------------------------

"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
I'm sure this is a very dumb question but when a user clicks on a row in
my
data table (which has been sorted) how do I return the correct row index
for
my underlying data table? The CurrentRowIndex property gives me the
selected
row of the data grid which no longer matches the data table when the
columns
have been resorted.

Thanks!!!


Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.