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
<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">