locked
Sorting with sqldatasource declared in code behind RRS feed

  • Question

  • User1408931231 posted

    Hello all! 

    Is it possible to sort and page a gridview with a datasource declared in code-behind? 

    here is a snippet of what I am doing so far:

            ds = New SqlDataSource
            ds.ConnectionString = "myconnstring"
            ds.DataSourceMode = SqlDataSourceMode.DataSet
    
            ds.SelectCommand = "select top 20 " _
                             & "Last,First,address,City,State,zipcode " _
                             & "from " _
                             & "Customer "
            Session("ds") = ds
            gvMain.DataSource = TryCast(Session("ds"), SqlDataSource)
            gvMain.DataBind()

    How can I handle the onSorting and onPaging events?

    Thanks

    Thursday, October 27, 2011 9:53 AM

Answers

  • User3866881 posted

    So, what can't I just declare the datasource in code behind with events and accomplish the same thing? I tried the on page_init event but still no lick. 

    Because when you initialize the Datasource bind to the GridView manually, everytime when your page refreshes, the new SqlDataSource is created again and again, so you never get the original one, and the original data contents fetched from that cannot gotten correctly.

    My suggestions is you can write a method like this:

    private void Binding()

    {

         GridView1.DataSource = Your DataSource like DataTable……;

         GridView1.DataBind();

    }

    Everytime when paging and sorting, set something like GridView1.PageIndex =e.NewPageIndex and call Binding method, thus you can do that manually.

    For more plz look at this:

    http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 28, 2011 9:52 PM

All replies

  • User1408931231 posted

    This sorts everything ascending....any way to change the direction?

        Private Sub gvMain_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvMain.Sorting
    
            Dim args As New DataSourceSelectArguments
    
            args.SortExpression = e.SortExpression
    
            Dim dv As DataView
            dv = ds.Select(args)
    
            gvMain.DataSource = dv
            gvMain.DataBind()
        End Sub
    Thursday, October 27, 2011 12:14 PM
  • User1408931231 posted

    Ok. I have found something interesting...mayby someone can explain why it works to me. 

    If I add the sqldatasource control to the aspx page. I can set the connection and select commands on page_load. After that, I do not even have to add the events on_sorting and on_paging to make this work.

    So, what can't I just declare the datasource in code behind with events and accomplish the same thing? I tried the on page_init event but still no lick. 

    Just trying to understand how this works!

    Thursday, October 27, 2011 9:16 PM
  • User3866881 posted

    So, what can't I just declare the datasource in code behind with events and accomplish the same thing? I tried the on page_init event but still no lick. 

    Because when you initialize the Datasource bind to the GridView manually, everytime when your page refreshes, the new SqlDataSource is created again and again, so you never get the original one, and the original data contents fetched from that cannot gotten correctly.

    My suggestions is you can write a method like this:

    private void Binding()

    {

         GridView1.DataSource = Your DataSource like DataTable……;

         GridView1.DataBind();

    }

    Everytime when paging and sorting, set something like GridView1.PageIndex =e.NewPageIndex and call Binding method, thus you can do that manually.

    For more plz look at this:

    http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 28, 2011 9:52 PM