473,804 Members | 2,140 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 15194
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
1229
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
2255
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
1806
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
13585
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
2133
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
2265
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
2007
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
1426
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
9715
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
10603
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...
1
10356
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
10099
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...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
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.