473,407 Members | 2,676 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,407 software developers and data experts.

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.Tables[0].Rows[dgJobs.CurrentRowIndex];

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 15143
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.Tables[0].DefaultView[dgJobs.CurrentRowIndex].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.Tables[0].Rows[dgJobs.CurrentRowIndex];

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.Tables[0].Rows[dgJobs.CurrentRowIndex];

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.Tables[0].DefaultView[dgJobs.CurrentRowIndex].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.Tables[0].Rows[dgJobs.CurrentRowIndex];

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 =
(CurrencyManager)dgJobs.BindingContext[dgJobs.DataSource, dgJobs.DataMember];
DataRowView xDRV = (DataRowView)xCM.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.Tables[0].Rows[dgJobs.CurrentRowIndex];

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.Tables[0].DefaultView[dgJobs.CurrentRowIndex].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.Tables[0].Rows[dgJobs.CurrentRowIndex];

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.

((CurrencyManager)BindingContext[dataGrid1.DataSource]).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
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...
1
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...
2
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...
6
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 >...
3
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...
3
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...
6
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...
4
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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,...

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.