Is there an example of filter gridview with textbox for specifics column doing filter and sort using sql data?
I was looking at the following, but i was not able to figure out how to use SQL data here
Filter GridView based on GridView Header TextBox with Paging and Sorting without using Stored Procedure in ASP.Net
Error: Sys.WebForms.PageRequestManagerServerErrorException: Cannot find column ASC
<asp:UpdatePanel runat="server">
<ContentTemplate>
<br />
<asp:HiddenField ID="hfClassApplied" runat="server" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" CellPadding="5" ForeColor="Black"
GridLines="Both" AllowPaging="False" CellSpacing="1" EmptyDataText="No outstanding orders found"
OnRowDataBound="GridView1_RowDataBound" >
<FooterStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField="DEPT" HeaderText="DEPT"/>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblJobno" Text="Job No" runat="server" />
<asp:TextBox ID="txtJobno" runat="server" AutoPostBack="true" OnTextChanged="TextChanged" />
<asp:LinkButton ID="lnkJobno" runat="server" OnClick="Sort" CssClass="glyphicon glyphicon-arrow-up"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" Text='<%# Eval("[Job No]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Tracking" HeaderText="Tracking" />
<asp:BoundField DataField="ItemNum" HeaderText="ItemNum" />
</Columns>
<PagerStyle HorizontalAlign="Center" CssClass="pgr" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [PerTab]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
public string GetCon()
{
return ConfigurationManager.ConnectionStrings["hr"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public string sortDirection
{
get
{
return ViewState["sortDirection"] == null ? "ASC" : ViewState["sortDirection"].ToString(); // your default sort direction i.e. ASC.
}
set
{
ViewState["sortDirection"] = value;
}
}
public string sortExpression
{
get
{
return ViewState["SortExpression"] == null ? "[Job No]" : ViewState["SortExpression"].ToString(); //Your by default sort expression. i.e. ColumnName
}
set
{
ViewState["SortExpression"] = value;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton lnkJobno = e.Row.FindControl("lnkJobno") as LinkButton;
Label lblJobno = e.Row.FindControl("lblJobno") as Label;
if (!string.IsNullOrEmpty(hfClassApplied.Value))
{
if (sortExpression == lblJobno.Text)
{
lnkJobno.CssClass = hfClassApplied.Value;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
SortGridView(sortExpression);
}
public void BindGrid()
{
using (SqlConnection con = new SqlConnection(GetCon()))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM [PerTab]";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (ViewState["GridView"] == null)
{
ViewState["GridView"] = dt;
}
GridView1.DataSource = ViewState["GridView"] as DataTable;
GridView1.DataBind();
}
}
}
private void SortGridView(string sortExpression)
{
DataTable dt = new DataTable();
if (ViewState["GridView"] != null)
{
dt = ViewState["GridView"] as DataTable;
}
DataView dv = new DataView(dt);
if (sortDirection == "ASC")
{
dv.Sort = sortExpression + " " + "ASC";
}
else
{
dv.Sort = sortExpression + " " + "DESC";
}
GridView1.DataSource = dv;
GridView1.DataBind();
}
protected void Sort(object sender, EventArgs e)
{
string assignedClass = (sender as LinkButton).CssClass.Split('-')[2];
if (assignedClass.ToUpper() == "UP")
{
sortDirection = "ASC";
hfClassApplied.Value = "glyphicon glyphicon-arrow-down";
}
else
{
sortDirection = "DESC";
hfClassApplied.Value = "glyphicon glyphicon-arrow-up";
}
string selectedId = (sender as LinkButton).ID;
Label lblJobno = (sender as LinkButton).FindControl("lblJobno") as Label;
LinkButton lnkJobno = (sender as LinkButton).FindControl("lnkJobno") as LinkButton;
if (lnkJobno.ID == selectedId)
{
sortExpression = lnkJobno.Text;
}
SortGridView(sortExpression);
}