473,588 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Current Selected Row in a DataGrid

Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Table s[0].Rows[dgJobs.CurrentR owIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve
Aug 11 '05 #1
4 15168
You need to compare your row index from the datagrid to the row index on the
datatable's default view rather than the datatable itself

Something like this
DataRow CurrentRow =
dsJobList.Table s[0].DefaultView[dgJobs.CurrentR owIndex].Row

"Steve" wrote:
Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Table s[0].Rows[dgJobs.CurrentR owIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve

Aug 11 '05 #2

That doesnt seem to work, it points to the same record as just doing

DataRow CurrentRow = dsJobList.Table s[0].Rows[dgJobs.CurrentR owIndex];

Do i need to set the default view somehow?
"Ben Reese" wrote:
You need to compare your row index from the datagrid to the row index on the
datatable's default view rather than the datatable itself

Something like this
DataRow CurrentRow =
dsJobList.Table s[0].DefaultView[dgJobs.CurrentR owIndex].Row

"Steve" wrote:
Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Table s[0].Rows[dgJobs.CurrentR owIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve

Aug 11 '05 #3
** SOLVED **

I did this:
CurrencyManager xCM =
(CurrencyManage r)dgJobs.Bindin gContext[dgJobs.DataSour ce, dgJobs.DataMemb er];
DataRowView xDRV = (DataRowView)xC M.Current;
DataRow xRow = xDRV.Row;

xRow is then a pointer to the current selected row in my datagrid.

Cheers

"Steve" wrote:

That doesnt seem to work, it points to the same record as just doing

DataRow CurrentRow = dsJobList.Table s[0].Rows[dgJobs.CurrentR owIndex];

Do i need to set the default view somehow?
"Ben Reese" wrote:
You need to compare your row index from the datagrid to the row index on the
datatable's default view rather than the datatable itself

Something like this
DataRow CurrentRow =
dsJobList.Table s[0].DefaultView[dgJobs.CurrentR owIndex].Row

"Steve" wrote:
Visual Studio 2003 .Net / C#

I have a datagrid, which is bound to a dataset at runtime when my page
loads. When the user double clicks a row, I need to find out which row they
have selected so I can pass the key value onto another page, which is showing
me more details for the selected row. So in the double click event of the
datagrid I have this code:

DataRow CurrentRow = dsJobList.Table s[0].Rows[dgJobs.CurrentR owIndex];

This generally seemed to be working. BUT.....when the user sorts the
Datagrid by clicking on one of the column headings things go screwy! They
double click for example on row 3, which on the service is now Job number 8
as they are sorting the grid on another value. But row 3 in my dataset is
not Job 8, it is Job 3!

How can I get round this? Is there a better way of getting the selected row
out of datagrid??

Thanks

Steve

Aug 11 '05 #4
Steve,

Assuming that your datagrid is a windowforms datagrid, than the
currencymanager position gives you the position of the current row number in
the datasource.

((CurrencyManag er)BindingConte xt[dataGrid1.DataS ource]).Position;

I hope this helps,

Cor
Aug 11 '05 #5

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

Similar topics

0
1216
by: Jawahar Rajan | last post by:
All, I have a datagrid that I use in a ASP.net web page that loads customer data based on the selected customer ID Some Customers will have no data in the database (which is oracle 9.2) . So I check the .HasRows property first then if it is false I skip the data bind like so If OraReader.HasRows Then grdCustomerDetails.AutoGenerateColumns = False grdCustomerDetails.DataSource = OraReader
1
2240
by: amber | last post by:
I have a datagrid, based on a dataview (filled with dataset data from SQL server). The dataview contains an ID field, but this field isn't in the datagrid. How can I get the ID associated with the current selected row in the datagrid? Possible? Thanks in advance,
2
1798
by: mrstrong | last post by:
Gday, Am new to using datagrids so if anyone could assist or point in the right direction in terms of resources - that would be much appreciated... I have a form that has: - a datagrid that displays summary data - other controls (eg textboxes, comboboxes) that are editable
6
13565
by: aaa | last post by:
Hi I am trying to create a read-only DataGrid that would always have current row selected. Currently, I am using method: public void SelectDataGridRow(DataGrid dg) { if (dg.CurrentRowIndex > -1) { dg.Select(dg.CurrentRowIndex);
3
2119
by: Christopher Weaver | last post by:
I want to set a value in a specific field in the current row of a DataSet. This seems like the most basic thing to do but I can't find the syntax for identifying the current row. IOW, I can do this: SomeRow = 'value'; But how do I set SomeRow to the row that the user is currently viewing?
3
2253
by: Carolyn Vo | last post by:
I have a datagrid in my web control class that I am trying to get the current rows displayed for. I have enabled paging on the datagrid so if the user is currently on page 3 of 8, and if I have allowed for the table to show only 5 rows at a time, for example, I want to get the 5 rows that are displayed on page 3 of 8. How do I do this???? Thanks!
6
2000
by: Becker | last post by:
Very simple, I have a datagrid on a windows form. I load some rows in it. I click a column header and it sorts ascending. I click it again it sorts descending. This is great, but what I want to do is after the sort, I want to know what row is now the "selected" row. I want to do this so I can update some stuff on the form that correlates to this row. This does not seem so easy...please advise. Thanks, Ben
4
1837
by: Steve | last post by:
Visual Studio 2003 .Net / C# I have a datagrid, which is bound to a dataset at runtime when my page loads. When the user double clicks a row, I need to find out which row they have selected so I can pass the key value onto another page, which is showing me more details for the selected row. So in the double click event of the datagrid I have this code: DataRow CurrentRow = dsJobList.Tables.Rows;
0
1420
neo008
by: neo008 | last post by:
Hi all, I'm using Datagrid to show data on forms. All other things are working fine..like add,update, delete and pointer movement. I have selected HIghlite row property of datagrid and it highligts each row selected by mouse. But it does not highlight row when I traverse programatically. for example by using refresh method DATA POINTER goes to the firstmost position in current recordset. but why does it not highlight it untill I click...
0
7860
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8354
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...
0
8223
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
5726
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
3847
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
3883
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2371
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
0
1195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.