473,411 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Returning Correct DataTable Row from DataGrid

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[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 5426
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.Select("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***************@nwrdny03.gnilink.net...
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[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 = (CurrencyManager)this.BindingContext[
dataGrid1.DataSource,
dataGrid1.DataMember];
DataView view = (DataView)cm.List;
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***************@nwrdny03.gnilink.net...
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[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**********@sympatico.ca.antispam> wrote in message
news:9W******************@news20.bellglobal.com...
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.Select("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***************@nwrdny03.gnilink.net...
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[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.com> wrote
in message news:O3**************@TK2MSFTNGP10.phx.gbl...
CurrencyManager cm = (CurrencyManager)this.BindingContext[
dataGrid1.DataSource,
dataGrid1.DataMember];
DataView view = (DataView)cm.List;
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***************@nwrdny03.gnilink.net...
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[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
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...
2
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...
3
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...
1
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...
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...
10
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...
4
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"...
0
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...
9
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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...

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.