How to apply filters from textbox & show it in gridview?

Posted by Arjunan_Csharp under ASP.NET on 1/17/2013 | Points: 10 | Views : 2891 | Status : [Member] | Replies : 2
Hi all,
string connStr =WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();

string sqlProducts = "SELECT ProductID, ProductName, UnitPrice FROM Products";

SqlDataAdapter da = new SqlDataAdapter(sqlProducts, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Products");

gridView1.DataSource = ds.Tables["Products"];
gridView1.DataBind();
in the above code i am just getting data from the database and show it in grid view.now i am adding one textbox.I enter the value in the textbox and click on the button.when i click on the button,Gridview should display the data with the filters.

How to achieve this.




Responses

Posted by: Sushilkumar.Sush on: 1/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

There is many way to filter the data when you click the button with the text box value .
Here i m giving two simple way.

1 . Store the data In session variable when you bind the data at first time
after clicking on the button i.e "filter" retrieve the data from session and add the following code.
//first store data in "SessionVerible " as "storedata"
Dataset ds= (Dataset)session("storedata") // Casting the data from session to dataset
// now in ds you will get all data which is already binded in GridView
gridView1.DataSource = ds.Tables["Products"].Select("Name = '" + text-box value/Filter value +"'");
gridView1.DataBind();
//In select statement you can the apply the filter as per your criteria.

2. Second Method when you click on the button then recall the following code with your filter criteria
string connStr =WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();

string sqlProducts = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductName ='"+ text-box value/Filter value+"'";

SqlDataAdapter da = new SqlDataAdapter(sqlProducts, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Products");

gridView1.DataSource = ds.Tables["Products"];
gridView1.DataBind();


Thanks and regards,
Sushilkumar Shinde





Sushilkumar Shinde

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 1/18/2013 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this,

string sqlProducts = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductName='" + TextBox1.Text + "' OR UnitPrice='" + TextBox1.Text + "'";


Please Mark As Answer if this helps you

.

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response