473,789 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1821
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataK eys[this.dataGrid1. SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossman s.net> wrote in message
news:%2******** ********@tk2msf tngp13.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_da rkfalz.com> wrote in message
news:ue******** ******@tk2msftn gp13.phx.gbl...
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataK eys[this.dataGrid1. SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossman s.net> wrote in message
news:%2******** ********@tk2msf tngp13.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.So rt = this.dataGrid1. SortOrder;
this.dataGrid1. DataKeyField = "PrimaryKeyFiel dName";
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***@grossman s.net> wrote in message
news:%2******** *******@tk2msft ngp13.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_da rkfalz.com> wrote in message
news:ue******** ******@tk2msftn gp13.phx.gbl...
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataK eys[this.dataGrid1. SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossman s.net> wrote in message
news:%2******** ********@tk2msf tngp13.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_SortC ommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridSortCommand EventArgs) Handles
DataGrid1.SortC ommand
Dim dtParam As DataTable = DsChanges1.Tabl es(0)
dvwParam = dtParam.Default View
dvwParam.Sort = e.SortExpressio n
DataGrid1.DataS ource = dvwParam
DataGrid1.DataB ind()
End Sub

dvwParam is a dataview that binds to the above datatable
(dtparam.defaul tview) 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_EditC ommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
DataGrid1.EditC ommand

'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.Ce lls(1).Text, String)
'Pass this ID to the Details form
Server.Transfer ("frmChange_Det ails.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_Edit Command' is pointing to.
"Curt_C [MVP]" <software_AT_da rkfalz.com> wrote in message
news:eI******** ******@TK2MSFTN GP12.phx.gbl...
I'm assuming you are doing something like this....

yourObj.View.So rt = this.dataGrid1. SortOrder;
this.dataGrid1. DataKeyField = "PrimaryKeyFiel dName";
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***@grossman s.net> wrote in message
news:%2******** *******@tk2msft ngp13.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_da rkfalz.com> wrote in message
news:ue******** ******@tk2msftn gp13.phx.gbl...
make sure to set a DataKeyField in the binding, then use
myValue = dataGrid1.DataK eys[this.dataGrid1. SelectedIndex].ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Jason" <ja***@grossman s.net> wrote in message
news:%2******** ********@tk2msf tngp13.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***@grossman s.net> wrote in message
news:eo******** ******@TK2MSFTN GP12.phx.gbl...
Close, here's my Sort Proecdure:

Private Sub DataGrid1_SortC ommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridSortCommand EventArgs) Handles
DataGrid1.SortC ommand
Dim dtParam As DataTable = DsChanges1.Tabl es(0)
dvwParam = dtParam.Default View
dvwParam.Sort = e.SortExpressio n
DataGrid1.DataS ource = dvwParam
DataGrid1.DataB ind()
End Sub

dvwParam is a dataview that binds to the above datatable
(dtparam.defaul tview) 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_EditC ommand(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
DataGrid1.EditC ommand

'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.Ce lls(1).Text, String)
'Pass this ID to the Details form
Server.Transfer ("frmChange_Det ails.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_Edit Command' is pointing to.
"Curt_C [MVP]" <software_AT_da rkfalz.com> wrote in message
news:eI******** ******@TK2MSFTN GP12.phx.gbl...
I'm assuming you are doing something like this....

yourObj.View.So rt = this.dataGrid1. SortOrder;
this.dataGrid1. DataKeyField = "PrimaryKeyFiel dName";
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***@grossman s.net> wrote in message
news:%2******** *******@tk2msft ngp13.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_da rkfalz.com> wrote in message
news:ue******** ******@tk2msftn gp13.phx.gbl...
> make sure to set a DataKeyField in the binding, then use
> myValue = dataGrid1.DataK eys[this.dataGrid1. SelectedIndex].ToString(); >
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> Site: http://www.Darkfalz.com
> Blog: http://blog.Darkfalz.com
>
>
> "Jason" <ja***@grossman s.net> wrote in message
> news:%2******** ********@tk2msf tngp13.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
4845
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 datagrid. How can I find out the new (sorted) index if I know the row in the datatable? kind regards
3
1652
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 trying to accomplish: The datagrid is sorted using: dgWIP.DataSource = dvWIP dvWIP.Sort = Me.SortColumn & " " & Me.SortOrder dgWIP.DataBind()
1
2417
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 of the selected row in textboxes on dialog that execute by the user press a button. The following code works if I do not sort the datagrid before I press the button to bring up the dialog. It is when I sort it the datagrid I have the issue ...
2
2371
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 me how to do this. I know how to launch the 2nd form and how to make a public variable to hold the value but I can't figure out how to get the value. In Delphi what I need should be something like dataSet1.FieldByName('UserID').Value; Is...
0
1281
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"): Private Sub dgrMain_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles
3
3768
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 being used to order the sorted data. I can't see in the microsoft DataGrid in vb.net the Event 'SortCommand'. I think that its because I'm using vb.net not asp. Thanks in advance.
10
4956
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 the column order of the datatable). I also allow the user to click on a column header to sort the datagrid by that column. I need to identify the row and column in the datatable when the user clicks on a cell in the datagrid. Using the...
1
1630
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, I want the underlying DataTable to be sorted. I've tried referencing DefaultView on the DataTable but it is never sorted. How can I get it to sort? Thanks, Brett
3
2749
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 populated with data onto a datagrid and recently the records are not sorted correctly on the datagrid(we do all the sorting in our stored procedure and no sorting on the code behind page). The strange things is that:
0
9511
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
10200
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
9984
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
9020
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
6769
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
5418
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...
1
4093
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
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.