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

Home Posts Topics Members FAQ

Returning the selected datagrid row from the underlying data table

Ok, I know this is an elementary question but I have a data grid that is
bound to a data table and I can't seem to find a way to match the selected
row in the grid with it's respective row in the underlying data table.

If the rows in the grid are in the same order as the rows in the table then
I can use the Datagrid.CurrentRowIndex to return the same row from the data
table. But if the user sorts the data in the grid then the row order in the
grid no longer match the row order in the data table.

My question is: How do I return a reference to the row in the underlying
data source from the data grid?

Thanks!!


Nov 20 '05 #1
2 2686
Hi,

Quick example. Needs a windows form with a datagrid and button.

Public WithEvents 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 conn As OleDbConnection
Dim da As OleDbDataAdapter

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 Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cmCurrent As CurrencyManager =
CType(Me.BindingContext(DataGrid1.DataSource), CurrencyManager)

Dim dr As DataRowView =
ds.Tables(0).DefaultView.Item(cmCurrent.Position)
Debug.WriteLine(dr.Item(1))
End Sub
Ken
------------------------
"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:O4**************@TK2MSFTNGP12.phx.gbl...
Ok, I know this is an elementary question but I have a data grid that is
bound to a data table and I can't seem to find a way to match the selected
row in the grid with it's respective row in the underlying data table.

If the rows in the grid are in the same order as the rows in the table
then
I can use the Datagrid.CurrentRowIndex to return the same row from the
data
table. But if the user sorts the data in the grid then the row order in
the
grid no longer match the row order in the data table.

My question is: How do I return a reference to the row in the underlying
data source from the data grid?

Thanks!!

Nov 20 '05 #2
Yep, that DataRowView did it for me.

Thanks!!!
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:O%****************@TK2MSFTNGP11.phx.gbl...
Hi,

Quick example. Needs a windows form with a datagrid and button.

Public WithEvents 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 conn As OleDbConnection
Dim da As OleDbDataAdapter

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 Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cmCurrent As CurrencyManager =
CType(Me.BindingContext(DataGrid1.DataSource), CurrencyManager)

Dim dr As DataRowView =
ds.Tables(0).DefaultView.Item(cmCurrent.Position)
Debug.WriteLine(dr.Item(1))
End Sub
Ken
------------------------
"Brian Mitchell" <Ma********@hotmail.com> wrote in message
news:O4**************@TK2MSFTNGP12.phx.gbl...
Ok, I know this is an elementary question but I have a data grid that is
bound to a data table and I can't seem to find a way to match the selected row in the grid with it's respective row in the underlying data table.

If the rows in the grid are in the same order as the rows in the table
then
I can use the Datagrid.CurrentRowIndex to return the same row from the
data
table. But if the user sorts the data in the grid then the row order in
the
grid no longer match the row order in the data table.

My question is: How do I return a reference to the row in the underlying
data source from the data grid?

Thanks!!


Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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....
2
by: ChrisM | last post by:
Can anyone please tell me what I'm doing wrong here. I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting. It is easy to reproduce the problem I have. If...
1
by: kll | last post by:
I have been on newsgroup for week. I have seen anything that will help me. Basically, I have a form with a datagrid that can be sorted or unsorted (it is up to the user). Then, I display the data...
4
by: Cat | last post by:
If you have a datagrid with an underlying dataset and rows are added to the dataset during a program run, how do you get the datagrid to update itself to repflect the changes in the underlying...
5
by: Jim | last post by:
is it possible to add a row to a Windows datagrid witthout having a datatable? I need to add a row to the datagrid control. Thanks.
1
by: Sean | last post by:
What is the best way to access the Underlying data of a DataGrid. I have two SQL tables joined and display some data from both, but I want to be able to on clicking of a row bring up a form that...
11
by: Fred Nelson | last post by:
I have an application in which it would be VERY beneficial if I could obtain the names of the colums in a datagrid. For example dim datagrid1 as new datagrid datagrid1.datasource = (stored...
7
by: Aaron | last post by:
Complete code follows. I am new to .NET programming (and programming in general) and I am having a difficult time understanding how to fill a variable in one sub, and then access it from...
4
by: Brian Mitchell | last post by:
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...
0
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
1
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.