.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Search Particular Employee based on his name in Datagrid

Posted By:Mohanraj       Posted Date: July 17, 2012    Points: 40    Category: DataGrid    URL: http://www.dotnetspark.com  

To select the particular employee and view its details in the datagrid by entering the name of the employee in the textbox
 

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        static string s = Application.StartupPath + "\\Data\\Test.accdb";
        static string constr = "Provider= Microsoft.ACE.OLEDB.12.0; Data Source=" + s + ";";
        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand cmd = new OleDbCommand();
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable dt = new DataTable();

        public Form1()
        {
            InitializeComponent();
            refreshGrid();
        }

        void refreshGrid() 
        { 
            string query;
            con.Open();
            if (textBox1.Text == "")
            {
                query = "select EmpNo,EmpName,Salary from Employee_details";
            }
            else
            {
                query = "Select EmpNo,EmpName,Salary from Employee_details where EmpName like '%" + textBox1.Text + "%'";
            }
            cmd = new OleDbCommand(query, con);
            da = new OleDbDataAdapter(cmd);
            dt.Clear();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            refreshGrid();
        }
    }
}

     

Further Readings:

Responses
Author: N SRILATHA         Company URL: http://www.dotnetspark.com
Posted Date: September 24, 2012

Ya it works fine

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend