473,487 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning correct datarow when datagrid is sorted

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
4 1250
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1760
by: Phil | last post by:
Hi, I have a client/server app. that uses a windows service for the server and asp.net web pages for the client side. My server class has 3 methods that Fill, Add a new record and Update a record....
4
5430
by: Randy | last post by:
I have a DataTable in a DataGrid. If I click on the DataGrid, HitTestInfo in dataGrid1_MouseDown returns a Row and Column number. I can use the Row number to say: DataRow dr = dataTable.Rows;...
2
5447
by: Bajgon | last post by:
Hello, I have got a DataGrid and a DataTable, which is its DataSource. I can use DataTable.Rows.Find(key_value) method to locate any row in the DataTable, using values of key column. The...
1
33339
by: ad | last post by:
What is the difference between DataRow and DataRowView?
1
3987
by: John Chorlton | last post by:
I've been attempting to pass a chunk of data back from a child Windows form using public properties on the form and have been getting some odd errors. I wanted to return a row of data to avoid...
3
2720
by: Sam | last post by:
Hi All, I have a very strange issue with ms sql stored procedure and sqlDataReader and I hope that someone can tell me what's going on . I have an asp.net application which has one of its page...
9
3917
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
2
15050
by: clogg02 | last post by:
Hi, I have bound a DataView to a DataGridView using this.dataGridView1.DataSource = new DataView(this.dataTable1); I allow for sorting of the datagridview using clicks on the headers. Later...
3
5540
by: joelzn | last post by:
I have a hand built dataset where some columns come from a sql database and others are calculated and eventually I will have two different query results going into one table because its impossible...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.