c# code for search or filter data in datagridview by using textbox with respect to combobox in .net



This is A C# Practice 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.SqlClient;

        namespace search
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dataentry;Integrated Security=True");
                private void textBox1_TextChanged(object sender, EventArgs e)
                {
                    if (comboBox1.Text == "ID")
                    {
                        SqlDataAdapter sda = new SqlDataAdapter("SELECT CUS_ID, CUS_NAME, MOB_NO, CITY, LAND_LINE_NO, ADDRESS FROM CUS_ENTRY where cus_id like '"+textBox1.Text+"%'", con);
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                    else if (comboBox1.Text == "Name")
                    {
                        SqlDataAdapter sda = new SqlDataAdapter("SELECT CUS_ID,CUS_NAME, MOB_NO, CITY, LAND_LINE_NO, ADDRESS FROM CUS_ENTRY where CUS_NAME like '"+textBox1.Text+"%'", con);
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }

                private void Form1_Load(object sender, EventArgs e)
                {

                }
            }
        }

Video For Result:



Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Facebook Comment