Hi fafibutt,
I have created sample. Refer the below code.
For this sample I have used NorthWind database which you can get by clicking on the link below.
Download Northwind Database
HTML
<div align="center">
<table>
<tr>
<td>
<asp:TextBox runat="server" ID="searchAddItemTextBox" />
</td>
<td>
<asp:Button ID="btnGetProduct" Text="Get Product" runat="server" OnClick="GetProductClick" />
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
Code
private DataTable GetProduct(string item)
{
DataTable dt = new DataTable();
string query = "SELECT ProductName,UnitPrice,QuantityPerUnit FROM Products WHERE ProductName LIKE '" + item + "%'";
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
{
SqlCommand cmd = new SqlCommand(query, sqlConnection);
sqlConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
sqlConnection.Close();
return dt;
}
}
protected void GetProductClick(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(searchAddItemTextBox.Text.Trim()))
{
if (ViewState["Products"] == null)
{
DataTable dt = GetProduct(searchAddItemTextBox.Text.Trim());
ViewState["Products"] = dt;
}
else
{
DataTable previousProducts = ViewState["Products"] as DataTable;
DataTable currentProducts = GetProduct(searchAddItemTextBox.Text.Trim());
currentProducts.Merge(previousProducts);
currentProducts.DefaultView.Sort = "ProductName";
ViewState["Products"] = currentProducts;
}
// Get distinct record if you click multiple time for same item it will filter with distinct record.
DataTable distinctProduct = ViewState["Products"] as DataTable;
GridView1.DataSource = distinctProduct.DefaultView.ToTable(true);
GridView1.DataBind();
}
}
Screenshot