CodeVerge.Net Beta


   Explore    Item Entry    Members      Register  Login  
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML





Zone: > NEWSGROUP > Asp.Net Forum > visual_studio.visual_studio_2005 Tags:
Item Type: NewsGroup Date Entered: 1/24/2008 4:00:04 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 13 Views: 54 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
14 Items, 1 Pages 1 |< << Go >> >|
StrangerMike
Asp.Net User
Gridview sort and select problem1/24/2008 4:00:04 PM

0/0

Hello, 

I just found a problem with my gridview.   It includes the line command 'Select', which when clicked sends the user to another screen using the record id from the gridview;  However, when I use the builtin sorts for each column heading in the gridview after I sort, if I use the 'Select' command it uses the Record Id of the record that was in the spot prior to the sort.  It's like the select column does not sort along with the rest of the gridview.

Below is the code:   Any help is appreciated.   Also, I had a routine I was going to use, but could not get to work. It is a rowcommand procedure, and perhaps this is my problem.

I tried assigning a cell to use to pass the Record id to another screen. but I get an error stating that  (e.item.cells(1).text)   ...e.items is not a member gridviewcommand arguments.  So instead I use SelectedIndexChanged event...is this the problem? 

*** This procedure is commented out because of the compilation error.   How can I reference cell 1?

    Protected Sub MyGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGridView.RowCommand
        '      Dim strRecId As String

      '   If e.CommandName = "Select" Then
      '   strRecId = (e.item.cells(1).text)                         '****** compilation error on this line
      '   Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)
      '   End If
    End Sub
**** in its place I use this ....but row.cell(1).text is the original data prior to the sort! 

    Protected Sub MyGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyGridView.SelectedIndexChanged
        Dim strRecId As String
        Dim row As GridViewRow = MyGridView.SelectedRow

        strRecId = row.Cells(1).Text
        Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)

    End Sub
  
Thanks
rmaiya
Asp.Net User
Re: Gridview sort and select problem1/24/2008 5:57:54 PM

0/0

The best way is set the DataKeyNames property of GridView to your KeyID, and on selected index changed event you can use

Dim strID as String = myGridView.SelectedValue;

Response.Redirect("yourpage.aspx?id" & strID)


Raghu
(MCSD.NET, MCAD.NET, MCDBA)
[Don't forget to click on Mark as answer on the post that helped you ]
vinz
Asp.Net User
Re: Gridview sort and select problem1/24/2008 6:18:09 PM

0/0

Check this

http://weblogs.asp.net/hpreishuber/archive/2005/08/05/421712.aspx 


-Mark as Answer- if this post helped you!

Best regards,
vinz
SDE - .NET/ASPNET/C#

A fool is always a fool, but an idiot can do something..
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/24/2008 7:13:14 PM

0/0

Thanks

 I don't understand, how do I set DataKeyName property?   


Thanks
vinz
Asp.Net User
Re: Gridview sort and select problem1/24/2008 7:22:04 PM

0/0

DataKeys is one of the attributes in the GridView to set that just go the properties of gridview and set the DataKeys field like

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Key" >
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField  ReadOnly="True"  />
            </Columns>
        </asp:GridView>

BTW: have you refer to my last post? Just check it out.. that might help you out


-Mark as Answer- if this post helped you!

Best regards,
vinz
SDE - .NET/ASPNET/C#

A fool is always a fool, but an idiot can do something..
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/24/2008 7:28:46 PM

0/0

I tried this code (below) and got this error on the Dim row line during execution:

"Unable to cast Object of type "asp.selectionResults_aspx" to type system.web.ui.webcontrols.gridviewrow" 

Any ideas? 

    Protected Sub MyGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGridView.RowCommand

        Dim strRecId As String

        Select Case
e.CommandName
            Case "Select"
                Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
                Dim id As Guid = MyGridView.DataKeys(row.RowIndex).Value

                strRecId = CType(row.Cells(1), DataControlFieldCell).Text
                '   strRecId = row.Cells(1).Text
                Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)

        End Select

      
      
    End Sub

 
Thanks
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/24/2008 7:36:40 PM

0/0

Yes I checked it out and the last post is where I am stuck. I will try the DatakeyName property,   another question. 

For the "Key", am I using the SQL field name that is coming back from the query to fill the gridview, or do I assign an ID to that column and use that as the name?  (I think I answered this during a test, I use the SQL name, at least that got me back to the same point as the prior error).

Still received the same error on the same line...'unable to cast object...'


Thanks
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/24/2008 7:42:18 PM

0/0

I just tried this:  (and set the DataKeyName to the key field), but the result was the same as I started with:  After doing a sort, the value in the strRecId is the value of my key prior to doing the column sort.

 

 Protected Sub MyGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyGridView.SelectedIndexChanged

        Dim strRecId As String = MyGridView.SelectedValue

       Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)


    End Sub
  
   <asp:GridView ID="MyGridView" AllowPaging="True"  runat="server" AllowSorting="True" AutoGenerateColumns="False"
                EmptyDataText="No records found" PagerSettings-Mode="NumericFirstLast"
                OnPageIndexChanging="gridViewSB_PageIndexChanging" OnSorting="gridViewSB_Sorting"
               Font-Size="10pt" ForeColor="Black"  Width="952px" AutoGenerateSelectButton="True" CellPadding="1" CellSpacing="1" Font-Bold="True" DataKeyNames="CpmsNumber">
 
Thanks
vinz
Asp.Net User
Re: Gridview sort and select problem1/24/2008 7:45:29 PM

0/0

I assumed that you used a Button to fire the RowCommand event so try it this way

Dim b As Button = CType(sender, Button)

Dim grow As GridViewRow = CType(b.NamingContainer, GridViewRow)

Dim id As Guid = MyGridView.DataKeys(row.RowIndex).Value


-Mark as Answer- if this post helped you!

Best regards,
vinz
SDE - .NET/ASPNET/C#

A fool is always a fool, but an idiot can do something..
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/24/2008 8:26:45 PM

0/0

Vinz,

I am sorry I am getting a little confused.  First, should I be doing this in the SelectedIndexChanged Event or the RowCommand event?

And yes I am using an AutoGeneratedSelectButton in the grid.

 

 


Thanks
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/24/2008 8:33:51 PM

0/0

Here is the latest code and error:

"Invalid cast exception"  on the 'Dim b As Button' line.

 Protected Sub MyGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGridView.RowCommand
        Select Case e.CommandName
            Case "Select"

                Dim b As Button = CType(sender, Button)

                Dim row As GridViewRow = CType(b.NamingContainer, GridViewRow)

                Dim id As Guid = MyGridView.DataKeys(row.RowIndex).Value

                Dim strRecId As String = id.ToString

                Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)

        End Select

    End Sub
 
Thanks
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/25/2008 6:49:48 PM

0/0

rmaiya,

 I tried your suggestion also, but I still get the orginal sort key value from the way the gridview looked prior to using a sort column. 

DataKeyName is set and this is my code: 

    Protected Sub MyGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyGridView.SelectedIndexChanged

        Dim strRecId As String = MyGridView.SelectedValue

        Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)


    End Sub

Any ideas, could there be something else I need to do?  
 
Thanks
Benson Yu - MSF
Asp.Net User
Re: Gridview sort and select problem1/29/2008 5:47:11 AM

0/0

StrangerMike:

*** This procedure is commented out because of the compilation error.   How can I reference cell 1?

    Protected Sub MyGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGridView.RowCommand
        '      Dim strRecId As String

      '   If e.CommandName = "Select" Then
      '   strRecId = (e.item.cells(1).text)                         '****** compilation error on this line
      '   Response.Redirect("RecordActions2.aspx?RecId=" & strRecId)
      '   End If
    End Sub
 

Hi StrangerMike,

The RowCommand of MyGridView should be like the following which works on my project. Hope it is helpful to you.

    Protected Sub MyGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGridView.RowCommand
        Dim strRecId As String
        If e.CommandName = "Select" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = MyGridView.Rows(index)
            strRecId = Server.HtmlDecode(row.Cells(2).Text)
            Response.Redirect("Default.aspx?RecId=" & strRecId)
        End If
    End Sub


Sincerely,
Benson Yu
Microsoft Online Community Support

Please remember to click ?Mark as Answer? on the post that helps you, and to click ?Unmark as Answer? if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
StrangerMike
Asp.Net User
Re: Gridview sort and select problem1/31/2008 2:46:12 PM

0/0

14 Items, 1 Pages 1 |< << Go >> >|



Search This Site:


Meet Our Sponsors:



Other Resources:

GridView select event slower with more rows - asp.net_ajax.asp ... ... client-side and when they do a sort, page, edit or ... problem while install visual studio 2005 server pack 1. debugging dnn. upload problems? ...
Sending Internet Mail to a Resource - novell.support.groupwise.7x ... Sending Internet Mail to a Resource, > ROOT > NEWSGROUP > Novell Forums > novell.support.groupwise.7x.install-setup-admin, ... gridview sort and select problem ...



 
All Times Are GMT