Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Getting a value from a sorted datagrid in asp.net?

Reply
Thread Tools

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

 
 
Jason
Guest
Posts: n/a
 
      06-10-2004
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


 
Reply With Quote
 
 
 
 
Curt_C [MVP]
Guest
Posts: n/a
 
      06-10-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      06-10-2004
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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Curt_C [MVP]
Guest
Posts: n/a
 
      06-10-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Jason
Guest
Posts: n/a
 
      06-10-2004
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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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:(E-Mail Removed)...
> > > 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" <(E-Mail Removed)> wrote in message
> > > news:%(E-Mail Removed)...
> > > > 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
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Curt_C [MVP]
Guest
Posts: n/a
 
      06-10-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > 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:(E-Mail Removed)...
> > > > 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" <(E-Mail Removed)> wrote in message
> > > > news:%(E-Mail Removed)...
> > > > > 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
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
urgent going crazy data not getting sorted! samir25 ASP .Net Datagrid Control 2 12-30-2005 08:01 AM
urgent going crazy data not getting sorted! samir25 ASP .Net Datagrid Control 0 12-28-2005 08:01 AM
Referencing Datagrid Rows while Datagrid is Sorted MrMike ASP .Net Web Controls 2 05-23-2005 12:02 PM
Firefox, getting pulldown bookmarks sorted? Galaxy Firefox 4 07-01-2004 02:01 AM
Firefox, getting pulldown bookmarks sorted? Galaxy Firefox 1 06-28-2004 11:19 AM



Advertisments