Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Records Disappear on Sort 1

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
US
I have a web form with a text box, button and a data grid. when clicked the button will populate the data grid with records that match the text box value. This part works.

The column headers are sortable so the user can put the records in whatever order they want. However when I click a header to sort by the records disappear. The code executes without error, but no records are returned. I have researched Page.IsPostBack and View States to see if that would solve the problem, but it hasn't yet.

Below is my code to populate the data grid and sort the table. I found the code for sorting in the help files. The prerender code is from ASP.Net for Dummies. They say it is good practice to store all your variables after the page loads.
Code:
    Dim UserName As String

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsPostBack Then
            UserName = CStr(ViewState("UName"))
        End If
    End Sub

    Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        ViewState("UName") = UserName
    End Sub

    Private Sub btnFillGrids_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillGrids.Click
        If fvUserName.IsValid Then
            UserName = txtUserName.Text
            cmdAssigned.Parameters.Item(0).Value = UserName
            daAssigned.SelectCommand.Connection.Open()
            daAssigned.Fill(dsAssigned)
            daAssigned.SelectCommand.Connection.Close()
            dgAssigned.DataBind()

            cnnCWO2.Dispose()
            cmdAssigned.Dispose()
            cnnCWO2 = Nothing
            cmdAssigned = Nothing
        End If
    End Sub

    Private Sub dgAssigned_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgAssigned.SortCommand
        dvAssigned.Sort = e.SortExpression
        dgAssigned.DataBind()
    End Sub
The other piece that is confusing me is I have a data grid on another page that works fine. That data grid has no parameters passed to it though.

Thank you in advance.

Jason Meckley
Database Analyst
WITF
 
I haven't looked as closely as I should at this code, but have been using datagrids extensively. You need to "refresh" your datasource. The data behind the datagrid is lost after every time the page is done being rendered, all that is left is the data in the datagrid. Every time you databind the datagrid you need to have your datasource full of data. Try something like... ( you may need to modify, but hopefully you'll see what I'm talking about )

Private Sub dgAssigned_SortCommand(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgAssigned.SortCommand
dgAssigned.Sort = e.SortExpression
cmdAssigned.Parameters.Item(0).Value = UserName
daAssigned.SelectCommand.Connection.Open()
daAssigned.Fill(dsAssigned)
daAssigned.SelectCommand.Connection.Close()
dgAssigned.DataBind()
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top