Search as you type using c#

 Add Datagridview1, Textbox control and two radio buttons on form, radio button name rd1, rd2.
//   Form Mudule
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;
namespace Search
{
    public partial class  Search : Form
    {
        ClassS  a = new ClassS ();
        DataTableds = new DataTable();
        public  Search()
        {
            InitializeComponent();
        }
   
        privatevoid textBox1_TextChanged (object sender, EventArgse)
        {
            stringstr = textBox1.Text;
            if(rd1.Checked == true)
            { a.flg = true; }
            if(rd2.Checked == true)
            { a.flg = false; }
            intcnt = str.Length;
            if(cnt > 0)
            {
                ds = a.gridSearch(“Tablename”,textBox1.Text);
                dataGridView1.DataSource = ds;
            }
        }
    }
}
//   Calss Mudule
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb ;
using System.Data;
using System.ComponentModel;
using System.Windows.Forms;
namespace Search
{
    class ClassS
    {
        OleDbConnection  aConnection =newOleDbConnection() ;
        privateString s;
        public int i = 0;
        public Boolean flg;
        publicClassS()
        {
       
aConnection = new OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=”+ Application.StartupPath + @”Database.mdb”);
      
        }
    # region gridSearch
    public DataTable gridSearch(stringa, string b)
    {
        stringquery=“”;
        if (flg == true)
        {
query = “select  fname,mname,lname  from “ + a + ” where fname like ‘” + b + “%’ ORDER BY fname ASC”;
        }
        if (flg == false)
        {
              
      query = “select fname,mname,lname  from “ + a + ” where lname
      like ‘” + b + “%’ ORDER BY lname ASC”;
        }
           
        using (OleDbCommand comand = newOleDbCommand(query, aConnection))
        {
            OleDbDataAdapter  da = new OleDbDataAdapter ();
            da.SelectCommand = comand;
            DataTableds = new DataTable();
            ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
            da.Fill(ds);
            returnds;
        }
    }
    # endregion
    }
}

Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *