locked
paging and sortin a gridview from a dataset RRS feed

  • Question

  • User1256512446 posted

    Hi all:

    I know how to page and sort a gridview with data from sql server.

    But how to page and sort with data from a dataset?

    Thanks in advance

    Monday, March 9, 2015 2:02 PM

Answers

  • User1256512446 posted

    Hi all:

    At last I found the solution.

    Affter assigning the datasource I put gridview.pagesize=records. So now it paginates.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 12, 2015 8:45 AM

All replies

  • User61956409 posted

    Hi volar5,

    Thanks for your post.

    You could refer to the following sample to sort records from DataSet.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting">
                </asp:GridView>
                <asp:HiddenField ID="idSortDirection" runat="server" Value="Ascending" />
                <asp:HiddenField ID="nameSortDirection" runat="server" Value="Ascending" />
                <asp:HiddenField ID="pwdSortDirection" runat="server" Value="Ascending" />
            </div>
        </form>
    </body>
    </html> 
    
    DataTable dt;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
            dt = new DataTable("mydt");
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("PWD");
    
            dt.Rows.Add("1", "JOJO", "11122333");
            dt.Rows.Add("2", "AMIN", "95364812");
            dt.Rows.Add("3", "MIKE", "22334455");
            dt.Rows.Add("4", "KIM", "12345678");
            dt.Rows.Add("5", "WALL", "32156455");
            dt.Rows.Add("6", "SAM", "86513479");
            dt.Rows.Add("7", "KAKAO", "00022288");
    
            ds = new DataSet();
            ds.Tables.Add(dt);
    
            //ds.Tables["mydt"].DefaultView.Sort = "ID desc";
    
            GridView1.DataSource = ds.Tables["mydt"];
            GridView1.DataBind();
    }
    
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string fieldname = e.SortExpression;
    
        if (fieldname == "ID")
        {
            string sortdirection = idSortDirection.Value.ToString();
    
            if (sortdirection == "Ascending")
            {
                ds.Tables["mydt"].DefaultView.Sort = fieldname + " desc";
    
                idSortDirection.Value = "Descending";
    
                GridView1.DataSource = ds.Tables["mydt"];
                GridView1.DataBind();
                return;
            }
            else
            {
                ds.Tables["mydt"].DefaultView.Sort = fieldname + " asc";
    
                idSortDirection.Value = "Ascending";
    
                GridView1.DataSource = ds.Tables["mydt"];
                GridView1.DataBind();
                return;
            }
        }
        else if (fieldname == "Name")
        {
            string sortdirection = nameSortDirection.Value.ToString();
    
            if (sortdirection == "Ascending")
            {
                ds.Tables["mydt"].DefaultView.Sort = fieldname + " desc";
    
                nameSortDirection.Value = "Descending";
    
                GridView1.DataSource = ds.Tables["mydt"];
                GridView1.DataBind();
                return;
            }
            else
            {
                ds.Tables["mydt"].DefaultView.Sort = fieldname + " asc";
    
                nameSortDirection.Value = "Ascending";
    
                GridView1.DataSource = ds.Tables["mydt"];
                GridView1.DataBind();
                return;
            }
        }
        else if (fieldname == "PWD")
        {
            string sortdirection = pwdSortDirection.Value.ToString();
    
            if (sortdirection == "Ascending")
            {
                ds.Tables["mydt"].DefaultView.Sort = fieldname + " desc";
    
                pwdSortDirection.Value = "Descending";
    
                GridView1.DataSource = ds.Tables["mydt"];
                GridView1.DataBind();
                return;
            }
            else
            {
                ds.Tables["mydt"].DefaultView.Sort = fieldname + " asc";
    
                pwdSortDirection.Value = "Ascending";
    
                GridView1.DataSource = ds.Tables["mydt"];
                GridView1.DataBind();
                return;
            }
        }
    
                
    } 
    



    Best Regards,

    Fei Han

    Monday, March 9, 2015 11:09 PM
  • User1256512446 posted

    Hi again:

    I use visual basic.

    This is the procedure to fill the datatable and bind to the gridview:

        Sub rellenar_ficheros_publico()
            Dim cnconexion As New SqlConnection(ConfigurationManager.ConnectionStrings("Cresolia").ConnectionString)
            Dim sqlc As New SqlCommand("seleccionar_desglose_obras", cnconexion)
            Dim strcarpeta As String, intobra As Integer = Request.QueryString("obra"), strfichero As String, dt As New DataTable, dr As DataRow, fi As FileInfo
            Dim strfoundfile As String, dectamano As Decimal, strtamano As String, sqlr As SqlDataReader
            sqlc.CommandType = CommandType.StoredProcedure
            sqlc.Parameters.AddWithValue("@obra", intobra)
            sqlc.Parameters.Add("@fichero", SqlDbType.NVarChar, 25)
            strcarpeta = Choose(intobra, "Anuncios", "Aplicaciones", "Articulos", "Canciones", "Cuadros", "Cuentos", "Cursos", "Diapositivas", "Documentales", "Estudios", "Eventos", "Fotos", "Negocios", "Novelas", "Peliculas", "Poemas")
            dt.Columns.Add("id0", System.Type.GetType("System.Int16"))
            dt.Columns.Add("concepto", System.Type.GetType("System.String"))
            dt.Columns.Add("fichero", System.Type.GetType("System.String"))
            dt.Columns.Add("tamano", System.Type.GetType("System.String"))
            For Each strfoundfile In My.Computer.FileSystem.GetFiles(Server.MapPath("../" & strcarpeta & " publico/"), Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.*")
                fi = New FileInfo(strfoundfile)
                If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden Then
                    strfichero = fi.Name
                    sqlc.Parameters("@fichero").Value = strfichero
                    cnconexion.Open()
                    sqlr = sqlc.ExecuteReader
                    sqlr.Read()
                    dr = dt.NewRow()
                    strtamano = " kb."
                    If sqlr.HasRows Then
                        dr("id0") = sqlr("id0")
                        dr("concepto") = sqlr("concepto")
                        dr("fichero") = fi.Name
                        dectamano = fi.Length / 1024
                        If dectamano >= 1000 Then
                            dectamano /= 1000
                            strtamano = " mb."
                        End If
                        dr("tamano") = Format(dectamano, "n2") & strtamano
                    Else
                        dr("id0") = 0
                        dr("concepto") = "<span style=""color:red"">--</span>"
                        dr("fichero") = "<span style=""color:red"">Sobra " & fi.Name & "</span>"
                        dectamano = fi.Length / 1024
                        If dectamano >= 1000 Then
                            dectamano /= 1000
                            strtamano = " mb."
                        End If
                        dr("tamano") = "<span style=""color:red"">" & Format(dectamano, "n2") & strtamano & "</span>"
                    End If
                    dt.Rows.Add(dr)
                    sqlr.Close()
                    cnconexion.Close()
                End If
            Next
            sqlc.Parameters.Clear()
            sqlc.Parameters.AddWithValue("@obra", intobra)
            sqlc.Parameters.AddWithValue("@verficheros", True)
            cnconexion.Open()
            sqlr = sqlc.ExecuteReader
            Do While sqlr.Read
                strfichero = Trim(sqlr("fichero"))
                If strfichero <> "" Then
                    fi = Nothing
                    fi = New FileInfo(Server.MapPath("../" & strcarpeta & " publico/") & strfichero)
                    If fi Is Nothing Then
                        dr = dt.NewRow()
                        dr("id0") = sqlr("id0")
                        dr("concepto") = "<span style=""color:red"">" & sqlr("concepto") & "</span>"
                        dr("fichero") = "<span style=""color:red"">Falta " & strfichero & "</span>"
                        dr("tamano") = "<span style=""color:red"">0 kb.</span>"
                        dt.Rows.Add(dr)
                    End If
                End If
            Loop
            sqlr.Close()
            gvficherospublico.DataSource = dt
            gvficherospublico.DataBind()
        End Sub
    

    This is the gridview markup:

        <asp:GridView ID="gvficherospublico" SortedAscendingHeaderStyle-BackColor="Blue" SortedDescendingHeaderStyle-BackColor="BlueViolet" HeaderStyle-CssClass="titulo" EmptyDataRowStyle-CssClass="centro" EmptyDataRowStyle-ForeColor="red" EmptyDataText="No hay registros" AllowPaging="true" AllowSorting="true" Width="100%" AutoGenerateColumns="false" DataKeyNames="id0" runat="server">
    

    The first time the gridview shows me all the records, But after a postback it paginates.

    Can u tell me in vb how to the paging and sorting?

    Thanks in advance

    Tuesday, March 10, 2015 11:34 AM
  • User1256512446 posted

    Hi all:

    At last I found the solution.

    Affter assigning the datasource I put gridview.pagesize=records. So now it paginates.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 12, 2015 8:45 AM
  • User1256512446 posted

    Hi all again:

    I am trying to order the datatable, but fails

    In gridview.sorting I do

            If strtabla = "ficherosprivado" OrElse strtabla = "ficherospublico" Then
                Dim strorden As String, strdireccion As String, dt As DataTable
                strorden = e.SortExpression
                strdireccion = e.SortDirection
                dt = DirectCast(ViewState("ficherospublico"), DataTable)
                If strdireccion = SortDirection.Ascending Then
                    dt.DefaultView.Sort = strorden + " desc"
                Else
                    dt.DefaultView.Sort = strorden + " asc"
                End If
                If strtabla = "ficherosprivado" Then
                    rellenar_ficheros_privado()
                Else
                    rellenar_ficheros_publico()
                End If
            End If
    

    What is wrong? I use vb

    Thanks in advance

    Friday, March 13, 2015 6:08 AM