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

Discussion in 'ASP .Net' started by Jason, Jun 10, 2004.

  1. Jason

    Jason Guest

    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
    Jason, Jun 10, 2004
    #1
    1. Advertising

  2. Jason

    Curt_C [MVP] Guest

    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" <> wrote in message
    news:%...
    > 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
    >
    >
    Curt_C [MVP], Jun 10, 2004
    #2
    1. Advertising

  3. Jason

    Jason Guest

    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:...
    > 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" <> wrote in message
    > news:%...
    > > 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
    > >
    > >

    >
    >
    Jason, Jun 10, 2004
    #3
  4. Jason

    Curt_C [MVP] Guest

    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" <> wrote in message
    news:%...
    > 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:...
    > > 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" <> wrote in message
    > > news:%...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
    Curt_C [MVP], Jun 10, 2004
    #4
  5. Jason

    Jason Guest

    Close, here's my Sort Proecdure:

    Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridSortCommandEventArgs) 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:...
    > 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" <> wrote in message
    > news:%...
    > > 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:...
    > > > 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" <> wrote in message
    > > > news:%...
    > > > > 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
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Jason, Jun 10, 2004
    #5
  6. Jason

    Curt_C [MVP] Guest

    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" <> wrote in message
    news:...
    > Close, here's my Sort Proecdure:
    >
    > Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridSortCommandEventArgs) 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:...
    > > 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" <> wrote in message
    > > news:%...
    > > > 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:...
    > > > > 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" <> wrote in message
    > > > > news:%...
    > > > > > 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
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Curt_C [MVP], Jun 10, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Pfeifer

    Have std::map sorted by value

    Matthias Pfeifer, Aug 27, 2007, in forum: C++
    Replies:
    7
    Views:
    734
    Jeff F
    Aug 29, 2007
  2. samir25

    urgent going crazy data not getting sorted!

    samir25, Dec 28, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    97
    samir25
    Dec 28, 2005
  3. samir25

    urgent going crazy data not getting sorted!

    samir25, Dec 28, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    109
    samir25
    Dec 30, 2005
  4. MrMike

    Referencing Datagrid Rows while Datagrid is Sorted

    MrMike, May 20, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    182
    MrMike
    May 23, 2005
  5. Jari Williamsson

    Putting value in sorted array

    Jari Williamsson, Dec 8, 2007, in forum: Ruby
    Replies:
    4
    Views:
    105
    Gary Wright
    Dec 8, 2007
Loading...

Share This Page