Hi Ruben12345,
I have created sample code which fulfill your requirement.
HTML
<div>
<asp:GridView ID="gvCustomers" AutoGenerateColumns="true" runat="server" EmptyDataText="No Records Found"
OnDataBound="gvCustomers_DataBound" />
<br />
<asp:Button ID="Button1" Text="SHOW ALL" OnClick="onclick" runat="server" />
</div>
C#
string constring = ConfigurationManager.ConnectionStrings["constr"].ToString();
int customerId = 0;
string Name;
string country;
protected void Page_Load(object sender, EventArgs e)
{
this.GetData();
}
private void GetData()
{
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("SELECT * from Customers", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void gvCustomers_DataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i <= gvCustomers.HeaderRow.Cells.Count - 1; i++)
{
TableHeaderCell cell = new TableHeaderCell();
TextBox txtsearch = new TextBox();
txtsearch.Attributes["Placeholder"] = gvCustomers.HeaderRow.Cells[i].Text;
txtsearch.ID = "txt-" + gvCustomers.HeaderRow.Cells[i].Text;
txtsearch.CssClass = "textbox";
txtsearch.TextChanged += new EventHandler(txtsearch_TextChanged);
txtsearch.AutoPostBack = true;
txtsearch.EnableViewState = true;
cell.Controls.Add(txtsearch);
row.Controls.Add(cell);
}
gvCustomers.HeaderRow.Parent.Controls.AddAt(1, row);
}
protected void txtsearch_TextChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
GridViewRow row = ((sender as TextBox).NamingContainer as GridViewRow);
string HeaderName = ((System.Web.UI.Control)(sender)).ID.Split('-')[1];
switch (HeaderName.ToUpper())
{
case "CUSTOMERID":
customerId = Convert.ToInt32((row.FindControl("txt-" + HeaderName + "") as TextBox).Text.Trim());
break;
case "NAME":
Name = (row.FindControl("txt-" + HeaderName + "") as TextBox).Text.Trim();
break;
case "COUNTRY":
country = (row.FindControl("txt-" + HeaderName + "") as TextBox).Text.Trim();
break;
default:
break;
}
string query = "SELECT [CustomerID],[Name],[Country] FROM [Customers]";
string whereCondition = string.Empty;
if (customerId > 0)
{
whereCondition = "CustomerId ='" + customerId + "'";
}
if (!string.IsNullOrEmpty(Name))
{
if (!string.IsNullOrEmpty(whereCondition))
{
whereCondition += "And Name LIKE '%" + Name + "%'";
}
else
{
whereCondition += "Name LIKE '%" + Name + "%'";
}
}
if (!string.IsNullOrEmpty(country))
{
if (!string.IsNullOrEmpty(whereCondition))
{
whereCondition += "And Country LIKE '%" + country + "%'";
}
else
{
whereCondition += "Country LIKE '%" + country + "%'";
}
}
whereCondition = !string.IsNullOrEmpty(whereCondition) ? " WHERE " + whereCondition : whereCondition;
query += whereCondition;
using (SqlCommand cmd = new SqlCommand(query, con))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void onclick(object sender, EventArgs e)
{
this.GetData();
}
Vb.net
Private constring As String = ConfigurationManager.ConnectionStrings("constr").ToString()
Private customerId As Integer = 0
Private Name As String
Private country As String
Protected Sub Page_Load(sender As Object, e As EventArgs)
Me.GetData()
End Sub
Private Sub GetData()
Dim con As New SqlConnection(constring)
Dim cmd As New SqlCommand("SELECT * from Customers", con)
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Protected Sub gvCustomers_DataBound(sender As Object, e As EventArgs)
Dim row As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
For i As Integer = 0 To gvCustomers.HeaderRow.Cells.Count - 1
Dim cell As New TableHeaderCell()
Dim txtsearch As New TextBox()
txtsearch.Attributes("Placeholder") = gvCustomers.HeaderRow.Cells(i).Text
txtsearch.ID = "txt-" + gvCustomers.HeaderRow.Cells(i).Text
txtsearch.CssClass = "textbox"
txtsearch.TextChanged += New EventHandler(AddressOf txtsearch_TextChanged)
txtsearch.AutoPostBack = True
txtsearch.EnableViewState = True
cell.Controls.Add(txtsearch)
row.Controls.Add(cell)
Next
gvCustomers.HeaderRow.Parent.Controls.AddAt(1, row)
End Sub
Protected Sub txtsearch_TextChanged(sender As Object, e As EventArgs)
Dim con As New SqlConnection(constring)
Dim row As GridViewRow = TryCast(TryCast(sender, TextBox).NamingContainer, GridViewRow)
Dim HeaderName As String = DirectCast(sender, System.Web.UI.Control).ID.Split("-"C)(1)
Select Case HeaderName.ToUpper()
Case "CUSTOMERID"
customerId = Convert.ToInt32(TryCast(row.FindControl((Convert.ToString("txt-") & HeaderName) + ""), TextBox).Text.Trim())
Exit Select
Case "NAME"
Name = TryCast(row.FindControl((Convert.ToString("txt-") & HeaderName) + ""), TextBox).Text.Trim()
Exit Select
Case "COUNTRY"
country = TryCast(row.FindControl((Convert.ToString("txt-") & HeaderName) + ""), TextBox).Text.Trim()
Exit Select
Case Else
Exit Select
End Select
Dim query As String = "SELECT [CustomerID],[Name],[Country] FROM [Customers]"
Dim whereCondition As String = String.Empty
If customerId > 0 Then
whereCondition = "CustomerId ='" + customerId + "'"
End If
If Not String.IsNullOrEmpty(Name) Then
If Not String.IsNullOrEmpty(whereCondition) Then
whereCondition += (Convert.ToString("And Name LIKE '%") & Name) + "%'"
Else
whereCondition += (Convert.ToString("Name LIKE '%") & Name) + "%'"
End If
End If
If Not String.IsNullOrEmpty(country) Then
If Not String.IsNullOrEmpty(whereCondition) Then
whereCondition += (Convert.ToString("And Country LIKE '%") & country) + "%'"
Else
whereCondition += (Convert.ToString("Country LIKE '%") & country) + "%'"
End If
End If
whereCondition = If(Not String.IsNullOrEmpty(whereCondition), Convert.ToString(" WHERE ") & whereCondition, whereCondition)
query += whereCondition
Using cmd As New SqlCommand(query, con)
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Sub
Protected Sub onclick(sender As Object, e As EventArgs)
Me.GetData()
End Sub
Screenshot
