Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview, textbox and a search button . when i search my gridview only first page displays the correct data. when i click on next page it shows all the unwanted data also with the results. suppose i have 10 records matching my criteria and my page size is 5. i just want my filtered data in 2 pages ..not all of the data. how to do it plz help . i have done this with paging off on search button click.. but i want my paging on when i search .


here is my search button code
protected void SearchButton_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
DataTable dt2 = new DataTable();
//Database connectionstring//
SqlConnection con = new SqlConnection(ss);
string query = string.Format("Select ProductId,ProductName,Description,Image,CategoryName,IsActive from Product inner join Category on Product.CategoryId=Category.CategoryId where ProductName like '%" + SearchTextBox.Text.Replace("%", "[%]").Replace("'", "''") + "%'");
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;

cmd.Connection = con;
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt2);
GridView1.DataSource = dt2;
GridView1.DataBind();

}

my paging code

public void BindTable()
{

SqlConnection con = new SqlConnection(ss);
SqlCommand cmd = new SqlCommand("Select ProductId,ProductName,Description,Image,CategoryName,IsActive from Product inner join Category on Product.CategoryId=Category.CategoryId");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindTable();

}
aspx code
VB
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ProductId" AutoGenerateColumns="False"
          AllowPaging="True" PageSize="5"
          OnPageIndexChanging="GridView1_PageIndexChanging" CellPadding="4"
      Font-Names="Lucida Sans Unicode" ForeColor="#333333" GridLines="None"
          Height="124px" AllowSorting="True" OnSorting="GridView1_Sorting"
          Width="814px">
Posted
Updated 18-Jul-14 0:54am
v6
Comments
KaushalJB 18-Jul-14 6:42am    
What code have you applied for paging and gridview binding ?? Update your question.
Member 10949397 18-Jul-14 6:58am    
i have updated my question plz suggest possible changes to be made
TrushnaK 18-Jul-14 6:52am    
Check you have assigning filtered data in page index changing event..
Member 10949397 18-Jul-14 6:58am    
i have updated my question plz suggest possible changes to be made

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900