473,761 Members | 10,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning Correct DataTable Row from DataGrid

I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_Mouse Down returns a Row
and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted the
table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy

Nov 15 '05 #1
4 5443
Randy,

Once you have your datagrid row, you can select against your source table.
But you will need to have a unique-valued column.
for example, if you have an "ID" column;

DataTable tblSource = datagrid.source as DataTable;
DataRow[] rowSelected = tblSource.Selec t("ID" + dataGrid[e.Row,
nIDColumn].ToString());//Where nIDColumn is the column that holds unique
values

When using a Datable.Select( ), it is possible that more than one row meets
the Select criteria. But if your column has unique values, then the result
you want is always at index 0. So in this case rowSelected[0] would be your
only result.

hope this helps,

regards,

Marco

"Randy" <rb***@sumaria. net> wrote in message
news:0D******** *******@nwrdny0 3.gnilink.net.. .
I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_Mouse Down returns a Row and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted the table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy

Nov 15 '05 #2
CurrencyManager cm = (CurrencyManage r)this.BindingC ontext[
dataGrid1.DataS ource,
dataGrid1.DataM ember];
DataView view = (DataView)cm.Li st;
DataRowView rowView = view[cm.Position];
DataRow row = rowView.Row;

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Randy" <rb***@sumaria. net> wrote in message
news:0D******** *******@nwrdny0 3.gnilink.net.. .
I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_Mouse Down returns a Row and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted the table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy


Nov 15 '05 #3
Marco,

Many thanks. That works!

Randy

"Marco Martin" <ma**********@s ympatico.ca.ant ispam> wrote in message
news:9W******** **********@news 20.bellglobal.c om...
Randy,

Once you have your datagrid row, you can select against your source table.
But you will need to have a unique-valued column.
for example, if you have an "ID" column;

DataTable tblSource = datagrid.source as DataTable;
DataRow[] rowSelected = tblSource.Selec t("ID" + dataGrid[e.Row,
nIDColumn].ToString());//Where nIDColumn is the column that holds unique
values

When using a Datable.Select( ), it is possible that more than one row meets
the Select criteria. But if your column has unique values, then the result you want is always at index 0. So in this case rowSelected[0] would be your only result.

hope this helps,

regards,

Marco

"Randy" <rb***@sumaria. net> wrote in message
news:0D******** *******@nwrdny0 3.gnilink.net.. .
I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_Mouse Down returns a

Row
and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted

the
table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy


Nov 15 '05 #4
Dmitriy,

Many thanks. That works too!

Randy

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:O3******** ******@TK2MSFTN GP10.phx.gbl...
CurrencyManager cm = (CurrencyManage r)this.BindingC ontext[
dataGrid1.DataS ource,
dataGrid1.DataM ember];
DataView view = (DataView)cm.Li st;
DataRowView rowView = view[cm.Position];
DataRow row = rowView.Row;

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Randy" <rb***@sumaria. net> wrote in message
news:0D******** *******@nwrdny0 3.gnilink.net.. .
I have a DataTable in a DataGrid.

If I click on the DataGrid, HitTestInfo in dataGrid1_Mouse Down returns a

Row
and Column number. I can use the Row number to say:

DataRow dr = dataTable.Rows[Row];

Of course, I get completely the wrong row of data if the user has sorted

the
table by clicking any of the column headers.

Any thoughts on how to get the clicked-on dataTable row, regardless of
whether or not the column headers are being used to sort?

Thanks,
Randy

Nov 15 '05 #5

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

Similar topics

0
3189
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some text. Before changing to another cell in the DataGrid, I move my mouse pointer off the DataGrid and change the selected item in a ListBox or TreeView. Here's my question Which should fire first, the DataTable's RowChanged/CellChanged event or...
2
1559
by: ALI-R | last post by:
Hi All, My datagrid is now bound and working, Now I have two other issues: 1)My datagrid is in another form and I need to return the value of a coulmn which user has selected to the caller form (in a textbox),I know how the get the value of an specific coulmn in the selected row ,but I don't know how to return it to the called
3
15410
by: Tim::.. | last post by:
I currently have the following datagrid but want to turn the name and email column into a hyperlink in the codebehind! Can someone please tell me how I achieve this! Thanks Private Sub createTable() Dim tbcontacts As DataTable = New DataTable("contacts") tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
1
935
by: Arch Stanton | last post by:
Wassup guys/gals. I'm really new to VB *cringe*, and i'm tryna to easily, expeditiously return a row(that i double click) from a data grid populated with data from an access database. what's the simplest way to do this? and if i wanted to update it later on?
4
1264
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 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!!!
10
4952
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than the column order of the datatable). I also allow the user to click on a column header to sort the datagrid by that column. I need to identify the row and column in the datatable when the user clicks on a cell in the datagrid. Using the...
4
1183
by: blackstaronline.net | last post by:
Here is my working code to pull Yahoo business news RSS feed. Can anyone show me how to only return the top 3 or 4 news articles? <%@ Import Namespace="System.Xml" %> <script language="VB" runat="server"> Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) If Cache("RSS1") Is Nothing then Dim dt as DataTable = GetRSSFeed("http://rss.news.yahoo.com/rss/business")
0
1026
by: flowk1337 | last post by:
I have a grid view with a datatable as it's datasource in VB. I can successfully import the data into the grid view and see it. I have my website set up so that the user can edit all fields at once and mass update them. So on a button click(fulfill), the program retrieves all rows from the datagrid and saves them to a new datatable. This works while debugging and on another server I have updated it too. But when I publish this website onto...
9
3998
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 format. after a hectic struggle still i dont know 1--who can i solve it and 2--where should i have to place the function GETDATA . who is it possible to keep all the functions in a separate file and to call that file in required page. here is the...
0
9521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7324
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5214
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
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 we have to send another system
3
3442
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.