473,408 Members | 1,867 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,408 software developers and data experts.

Getting a value from a sorted datagrid in asp.net?

I've been trying to figure out a good way to do this but haven't had much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a column of
this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure
what to use in a web form.

Thanks in advance,

Jason
Nov 18 '05 #1
5 1795
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've been trying to figure out a good way to do this but haven't had much
luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value
of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a column of
this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure
what to use in a web form.

Thanks in advance,

Jason

Nov 18 '05 #2
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item
after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've been trying to figure out a good way to do this but haven't had much luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a column of this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure what to use in a web form.

Thanks in advance,

Jason


Nov 18 '05 #3
I'm assuming you are doing something like this....

yourObj.View.Sort = this.dataGrid1.SortOrder;
this.dataGrid1.DataKeyField = "PrimaryKeyFieldName";
this.dataGrid1.DataSource = yourObj.View;
this.dataGrid1.DataBind();

?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item
after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've been trying to figure out a good way to do this but haven't had much luck, any input would be greatly appreciated.

Basically, after a datagrid is sorted, how can I get the primary key value of the item selected by the user? That is, not the datagrid index
'location' of the item the user selected, but the value inside a
column
of this item the user clicked on.

I would use the CurrencyManager in a windows application but I'm not sure what to use in a web form.

Thanks in advance,

Jason



Nov 18 '05 #4
Close, here's my Sort Proecdure:

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand
Dim dtParam As DataTable = DsChanges1.Tables(0)
dvwParam = dtParam.DefaultView
dvwParam.Sort = e.SortExpression
DataGrid1.DataSource = dvwParam
DataGrid1.DataBind()
End Sub

dvwParam is a dataview that binds to the above datatable
(dtparam.defaultview) as above. So, whenever I actually click on one of the
datagrid1's headhers to sort by that column it sorts just fine, but if I
click on a record, it gives the 'original' value (presorted record) not the
now-sorted-value (record).

The sub that's called when I click on a record is:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.EditCommand

'This should be the value of the cell(1) a.k.a. Primary key value
"Change_ID" in the datagrid
propChange_ID = CType(e.Item.Cells(1).Text, String)
'Pass this ID to the Details form
Server.Transfer("frmChange_Details.aspx")
End Sub

I hope this makes sense. It looks like although the dataview is getting
sorted (which makes it appear the datagrid is sorted as well) the underlying
data table (or whatever is underlying to a dataview/grid) isn't being sorted
which is what my 'DataGrid1_EditCommand' is pointing to.
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:eI**************@TK2MSFTNGP12.phx.gbl...
I'm assuming you are doing something like this....

yourObj.View.Sort = this.dataGrid1.SortOrder;
this.dataGrid1.DataKeyField = "PrimaryKeyFieldName";
this.dataGrid1.DataSource = yourObj.View;
this.dataGrid1.DataBind();

?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item after a sort it still returns the wrong value. Does it matter that I'm
sorting using a dataview of the datagrid?
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> I've been trying to figure out a good way to do this but haven't had

much
> luck, any input would be greatly appreciated.
>
> Basically, after a datagrid is sorted, how can I get the primary key

value
> of the item selected by the user? That is, not the datagrid index
> 'location' of the item the user selected, but the value inside a

column
of
> this item the user clicked on.
>
> I would use the CurrencyManager in a windows application but I'm not

sure
> what to use in a web form.
>
> Thanks in advance,
>
> Jason
>
>



Nov 18 '05 #5
not certain but try setting the sort expression prior to setting the source.

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:eo**************@TK2MSFTNGP12.phx.gbl...
Close, here's my Sort Proecdure:

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand
Dim dtParam As DataTable = DsChanges1.Tables(0)
dvwParam = dtParam.DefaultView
dvwParam.Sort = e.SortExpression
DataGrid1.DataSource = dvwParam
DataGrid1.DataBind()
End Sub

dvwParam is a dataview that binds to the above datatable
(dtparam.defaultview) as above. So, whenever I actually click on one of the datagrid1's headhers to sort by that column it sorts just fine, but if I
click on a record, it gives the 'original' value (presorted record) not the now-sorted-value (record).

The sub that's called when I click on a record is:

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.EditCommand

'This should be the value of the cell(1) a.k.a. Primary key value
"Change_ID" in the datagrid
propChange_ID = CType(e.Item.Cells(1).Text, String)
'Pass this ID to the Details form
Server.Transfer("frmChange_Details.aspx")
End Sub

I hope this makes sense. It looks like although the dataview is getting
sorted (which makes it appear the datagrid is sorted as well) the underlying data table (or whatever is underlying to a dataview/grid) isn't being sorted which is what my 'DataGrid1_EditCommand' is pointing to.
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:eI**************@TK2MSFTNGP12.phx.gbl...
I'm assuming you are doing something like this....

yourObj.View.Sort = this.dataGrid1.SortOrder;
this.dataGrid1.DataKeyField = "PrimaryKeyFieldName";
this.dataGrid1.DataSource = yourObj.View;
this.dataGrid1.DataBind();

?
--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossmans.net> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Thanks for the reply Curt.

I've set a DataKeyField in the binding but when I click on a datagrid item after a sort it still returns the wrong value. Does it matter that I'm sorting using a dataview of the datagrid?
"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
> make sure to set a DataKeyField in the binding, then use
> myValue = dataGrid1.DataKeys[this.dataGrid1.SelectedIndex].ToString(); >
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> Site: http://www.Darkfalz.com
> Blog: http://blog.Darkfalz.com
>
>
> "Jason" <ja***@grossmans.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > I've been trying to figure out a good way to do this but haven't had much
> > luck, any input would be greatly appreciated.
> >
> > Basically, after a datagrid is sorted, how can I get the primary key value
> > of the item selected by the user? That is, not the datagrid index
> > 'location' of the item the user selected, but the value inside a

column
of
> > this item the user clicked on.
> >
> > I would use the CurrencyManager in a windows application but I'm not sure
> > what to use in a web form.
> >
> > Thanks in advance,
> >
> > Jason
> >
> >
>
>



Nov 18 '05 #6

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

Similar topics

3
by: Oliver Drobnik | last post by:
Hi, I have a datagrid bound to a datatable. Now if I sort the datagrid by clicking on one of the column heads I can no longer use the index in the datagrid to find the corresponding row in the...
3
by: sam | last post by:
Hello group, The datagrid is sorted using a dataview. Can I get the sorted dataview or table from the datagrid or any other way. All I need to get is the sorted data. Here is my code and what I am...
1
by: kll | last post by:
I have been on newsgroup for week. I have seen anything that will help me. Basically, I have a form with a datagrid that can be sorted or unsorted (it is up to the user). Then, I display the data...
2
by: Red Green | last post by:
I have a form with a datagrid and a dataset bound to it. I want to launch a second form and pass it a value from whichever record the user has selected. I can't find an example anywhere that shows...
0
by: Eugene | last post by:
Hi everybody! Got the logical error (wrong row (SSN) selected on SelectedIndexChange event) when made a sort by non-key field in Datagrid. Used this Sub to keep the keyfield ("SSN"): ...
3
by: paul | last post by:
Hi, I've a DataGrid and a DataSet (not a DataView) as the DataSource. Users can click on column headers and sort the contents of the DataGrid. I want to know how to determine which column is...
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...
1
by: Brett Romero | last post by:
I'm binding a DataTable to a datagrid. I'd like the DataTable to use any kind of sort the datagrid is using so the two stay in synch. When some one clicks a data column header and sorts the grid,...
3
by: Sam | last post by:
Hi All, I have a very strange issue with ms sql stored procedure and sqlDataReader and I hope that someone can tell me what's going on . I have an asp.net application which has one of its page...
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
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
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
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...
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,...
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.