search database and display in datagrid view
Please Help. I am developing an address book application.I have a textbox for search.But i dont need a button for search. When i type something in my text box,i want it to be searched in the database and display result in my datagrid view.What query should i usee? how can i get the result to my Datagrid view?Please Help.Any Help will be greatly appreciated.Thanks a lot for youur help.
Satyapriya NayakPosted Aug 20, 2010, 11:21 PM
This is a simple application i did it of your requirement.Run the attachments.
Code:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Display_data_in_datagridview_with_textbox
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bindgrid();
}
void bindgrid()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "employee";
con.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = str = "select * from employee where empname like '" + textBox1.Text + "%'";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "employee";
con.Close();
}
}
}
Thanks
If it helps you plz mark it as Accepted answer