DataGrid not sorting and paging

aliweb

Active member
Joined
Sep 14, 2005
Messages
40
Programming Experience
3-5
Hello

I am using DataGrid with the following options

Allow Sorting - Yes
Allow Paging - Yes (default paging)

When I run the application it shows that there are 5 pages but when I click on any of them for e.g. 2 , 3, 4, 5 it always displays the first page and not the rest. Plus it doesn't sort the records when I click on column heading.
What could be the problem?
 
for paging you need to use pageindexchanged event and eter this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].dg.CurrentPageIndex = e.NewPageIndex[/SIZE]
[SIZE=2]'refill grid after this[/SIZE]
[SIZE=2]LoadPolls()
[/SIZE]

for sorting you need to sort command of datagrid and write:
VB.NET:
[SIZE=2]ViewState("_OrderBy") = e.SortExpression
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ViewState("_OrderWay") = "DESC" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ViewState("_OrderWay") = "ASC"
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]ViewState("_OrderWay") = "DESC"
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]''refill grid after this
LoadPolls()[/SIZE]

here is the code to fill datagrid
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] LoadPolls()
cn = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbConnection(ConfigurationSettings.AppSettings("DatabaseString").ToString)
cmd = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbCommand
cmd.CommandText = "SELECT * FROM Questions"
cmd.Connection = cn
da = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbDataAdapter(cmd)
cn.Open()
da.Fill([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ds, "Questions")
cn.Close()
dv.Sort = ViewState("_OrderBy") & " " & ViewState("_OrderWay")
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].dg.DataBind()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Note that you have to create dataset and dataview in desiger or code.
 
Ok I changed my code like you told for paging (haven't done sorting part yet) but it is still not working.

My datagrid name is dtgMain here is what I did.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] dtgMain_PageIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] source [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.DataGridPageChangedEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] dtgMain.PageIndexChanged
     dtgMain.SelectedIndex = e.NewPageIndex
     dtgMain.DataSource = CreateDataSource()
     dtgMain.DataBind()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE]

The function I am using to fill datagrid is CreateDataSource() which is as follows:

VB.NET:
[SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] CreateDataSource() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ICollection
 
     OpenConnection()
[/SIZE]

[SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] dt [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable
[/SIZE][SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
[/SIZE][SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] strSQL [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbCommand
[/SIZE][SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] rs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbDataReader
[/SIZE]

[SIZE=2][COLOR=#008000]    'create a DataTable
[/COLOR][/SIZE][SIZE=2]    dt = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable
     dt.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Category ID", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
     dt.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Category Name", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
     dt.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Action", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
[/SIZE]
 
[SIZE=2][COLOR=#008000]    'Make some rows and put some sample data in
[/COLOR][/SIZE][SIZE=2]    strSQL = "SELECT CATEGORY_ID, CATEGORY_NAME FROM TBL_CATEGORY"
     cmd = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Data.OleDb.OleDbCommand(strSQL, con)
     rs = cmd.ExecuteReader
[/SIZE]
 
[SIZE=2][COLOR=#0000ff]   If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] rs.HasRows [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]        dtgMain.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]        lblNoRecords.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]        rs.Close()
         con.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]        Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]    While[/COLOR][/SIZE][SIZE=2] rs.Read
          dr = dt.NewRow
          dr(0) = rs("CATEGORY_ID")
          dr(1) = rs("CATEGORY_NAME")
          dr(2) = "<a href='editcatgegory.aspx'>Edit</a> | <a href='deletecatgegory.aspx'>Delete</a>"
          dt.Rows.Add(dr)
[/SIZE][SIZE=2][COLOR=#0000ff]    End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]    rs.Close()
     con.Close()
 
[/SIZE]
[SIZE=2][COLOR=#008000]    'return a DataView to the DataTable
[/COLOR][/SIZE][SIZE=2]    CreateDataSource = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataView(dt)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]
 
you cannot call CreateDataSource() when paging, it will recreate everything. try to separate creating structure (you this just one time) and filling it with data. why dont you use dataset in designer, it much faster way.
 
Back
Top