Click to See Complete Forum and Search --> : paging problems


luckybrit
October 10th, 2003, 05:45 PM
Hello,

I'm having a lot of problems with paging a datagrid in asp.net. I have to manage a database of more than 4000 records. So i use paging. Lets say 20 records a page and i'm showing 10 numbers so i get 12345678910....
The problem is when i'm clicking on the .... i get
11121314151617181920....
and that's where the first problem begins.

1)When i click lets say on sixteen, the currentpageindex is then 5.

an other problem is that when i click on the .... again

2)i don't get 21222324252627282930.... but
12345678910...

Is their a way i can solve those problems?

thx in advance,

Luckybrit

Rohit Kukreti
October 11th, 2003, 03:47 AM
Hi luckybrit,

r u setting the page index properly in the pageindex changed event

DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();

Also, i fu can post the code it wud b better to diagnose the prob.
But try what i said if u haven't

hope this helps
---
Rohit

luckybrit
October 11th, 2003, 06:59 AM
hi,

yups, it has something to do with the databind

thx for your post

luckybrit

luckybrit
October 11th, 2003, 10:54 AM
Hello,

When i click on the .... of the paging, and in my pageLoad i have the following code

dgHomeWijzigen.DataSource=ds.tables("Page")

dim intTabBlad as integer

intTabBlad=Session("TabBlad")

dgHomeWijzigen.CurrentPageIndex=intTabBlad

dgHomeWijzigen.DataBind()
adapter.dispose()
Conn.Dispose()

the paging works fine, but then i'm having problems with my search button. When the currentPageIndex is bigger than the pagecount after the search (for example only records that starts with an 'a'), this code doesn't work.

So i'll try to catch this error with

dgHomeWijzigen.DataSource=ds.tables("Page")

dim intTabBlad as integer
dim intPagCount as integer

intPagCount=session("Grootte")

intTabBlad=Session("TabBlad")

if intPagCount >= intTabBlad then
dgHomeWijzigen.CurrentPageIndex=intTabBlad
else
dgHomeWijzigen.CurrentPageIndex=0
end if

dgHomeWijzigen.DataBind()
adapter.dispose()
Conn.Dispose()

but using this code, the search button works fine, but the paging doesn't work again. I do not understand this. How can i solve this problem.


greetz,

luckybrit

Rohit Kukreti
October 13th, 2003, 03:07 AM
Hi luckybrit,

This is the way i handle the paging, search, etc..

DataView dv;
SqlConnection myConnection = "ur conn str";
try
{
string lastFilter="";
if (Page.IsPostBack)
{
lastFilter= (string)ViewState["LastFilter"];
}
else
{
// Databind the data grid on the first request only
// (on postback, bind only in editing, paging and sorting
commands)
ViewState["LastFilter"] = "";
}
BindGrid();
if (lastFilter != "")
{
dv.RowFilter= lastFilter;
DataGrid1.DataSource =dv;
DataGrid1.DataBind();
}
if (!Page.IsPostBack) //for adding the flds in the drop down list
box for searching
{
drpField.DataSource = null;
foreach ( DataColumn x in dv.Table.Columns)
{
if (x.GetType().Name == "DataColumn")
{
drpField.Items.Add(x.ToString());
}
}
}
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}


private void BindGrid()
{
try
{
SqlConnection myConnection = "ur conn str";
SqlDataAdapter myCommand = new SqlDataAdapter
(SelectCommand, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "BindGrid");
dv = ds.Tables["BindGrid"].DefaultView;
if (ViewState["LastFilter"].ToString() != "")
{
dv.RowFilter = ViewState["LastFilter"].ToString();
}
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
DataGrid1.Columns[2].Visible = false;
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}
}

//For search
private void btnSearch_Click(object sender, System.EventArgs e)
{
string strFilter = "";
//u can use the drop down or any ctrl u want
switch (this.drpListCompare.SelectedItem.ToString().ToLower())
{
case "contains":
strFilter = dv.Table.Columns[ this.drpField.SelectedIndex ] + "
like '%" + this.txtSearch.Text.Trim() + "%'";
break;
case "begins with":
strFilter = dv.Table.Columns[ this.drpField.SelectedIndex] + "
like '" + this.txtSearch.Text.Trim() + "%'";
break;
case "equal to":
strFilter = dv.Table.Columns[ this.drpField.SelectedIndex] + "
= '" + this.txtSearch.Text.Trim() + "'";
break;
}
ViewState["LastFilter"] = strFilter;
BindGrid();
dv.RowFilter= strFilter;
DataGrid1.CurrentPageIndex= 0; // important! goto first page
DataGrid1.DataBind();
}

// For Paging

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

The above code works for me, u try and c if it works for u

hope this helps
---
Rohit

luckybrit
October 25th, 2003, 11:18 AM
hi,

With try catch end try, it works fine.

thx for your post

Greetz