Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am just a beginner,
Want to know how can I use gridview with MS ACCESS for realtime search
Like

SQL
select*From Source where desc like '%textSrch.text%

as an example.

I tried to get the data, but instead of deleting the number of rows it is adding.

Help
Posted
Updated 3-Jan-12 3:43am
v2
Comments
Amir Mahfoozi 3-Jan-12 9:26am    
Please provide the filtering part of your code.

One way to do the filtering is to repopulate the underlying data from the database. However, depending on the situation this may be both time and resource consuming.

Other possibilities are for example:

If you're using for example BindingSource, you can apply a filter on it. This way the data remains the same but only the relevant portion is shown. For more information, see: BindingSource.Filter Property [^]

If you fetch the data into a DataTable from the database you can define a DataView which filters the desired rows from your DataTable and assign that view as your datasource to a grid etc. More information: DataView.RowFilter Property [^]

The expressions you can use are listed in DataColumn.Expression Property [^]
 
Share this answer
 
Populate the search result in grid view using the below code:

C#
protected void Page_Load(object sender, EventArgs e)
{
string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("select * From Source where desc like '%textSrch.text%'", sqlConnection);
sqlConnection.Open(); 

SqlDataReader reader = sqlCommand.ExecuteReader();       

GridView1.DataSource = reader;
GridView1.DataBind();
}
 
Share this answer
 
Thanx for helping,
It was not much hard, I did by self using this
C#
private void textBox1_TextChanged(object sender, EventArgs e)
{
 Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Database.mdb";
            string txt;
            txt = "%" + textBox1.Text + "%";
            string query = "Select keys, desc from source where desc like '"+txt+"'";
            OleDbDataAdapter da = new OleDbDataAdapter(query, Conn);
            
            OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);
            DataTable dt = new DataTable();
            da.Update(dt);
            da.Fill(dt);

            BindingSource bn = new BindingSource();
            bn.DataSource = dt;

            dataGridView1.DataSource = bn;
}


and it is working fine...
 
Share this answer
 
v2
Comments
Wendelius 3-Jan-12 11:28am    
A comment on your solution. Instead of concatenating the value from the text box to your query, use SqlParameter[^]. For example you can try what happens if you enter "some'string" to your text box :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900