SIGN UP MEMBER LOGIN:    
ARTICLE

Searching Record in DataGridView in ADO.NET

Posted by Alok Pandey Articles | ADO.NET in C# January 13, 2012
In this article you will learn how to search record in a DataGridView.
Reader Level:
Download Files:
 

Introduction

In this article I am displaying a record from a database table in a DataGridView and searching the records in a DataGridView. At first we should have a database with some records. In this example my Database name is "student" and Database table name is "student_detail" which has some records. I will bind these records with DataGridView and perform a different operation.

  • Take a Windows Forms Application.
  • Take some UI controls and arrange them like in the following figure.

Write the following 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
searchingrecord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter dadapter;
        DataSet dsset;
        CurrencyManager cmgr;
        DataRowView drv;
       
        string connatring = "server=.;database=student;user=sa;password=wintellect";
 
        private void Form1_Load(object sender, EventArgs e)
        {
            dadapter = new SqlDataAdapter("select * from student_detail", connatring);
            dsset = new DataSet();
            dadapter.Fill(dsset);
            dataGridView1.DataSource = dsset.Tables[0];
            cmgr = (CurrencyManager)dataGridView1.BindingContext[dsset.Tables[0]];
        }
       private void btnfirst_Click(object sender, EventArgs e)
        {
            cmgr.Position = 0;
            drv = (DataRowView)cmgr.Current;
            txtrollno.Text = drv.Row[0].ToString();
            txtname.Text = drv.Row[1].ToString();
            txtcity.Text = drv.Row[2].ToString();
        }
       private void btnnext_Click(object sender, EventArgs e)
        {
            if (cmgr.Position != cmgr.Count)
            {
                cmgr.Position += 1;          
                drv = (DataRowView)cmgr.Current;
                txtrollno.Text = drv.Row[0].ToString();
                txtname.Text = drv.Row[1].ToString();
                txtcity.Text = drv.Row[2].ToString();
            }                        
        }
       private void btnprevious_Click(object sender, EventArgs e)
        {
            if (cmgr.Position != 0)
            {
                cmgr.Position -= 1;
                drv = (DataRowView)cmgr.Current;
                txtrollno.Text = drv.Row[0].ToString();
                txtname.Text = drv.Row[1].ToString();
                txtcity.Text = drv.Row[2].ToString();
            }
        }
       private void btnlast_Click(object sender, EventArgs e)
        {
            cmgr.Position = cmgr.Count;
            drv = (DataRowView)cmgr.Current;
            txtrollno.Text = drv.Row[0].ToString();
            txtname.Text = drv.Row[1].ToString();
            txtcity.Text = drv.Row[2].ToString();
        }
      private void btnsearch_Click(object sender, EventArgs e)
       {
           try
           {
               DataView dview = new DataView(dsset.Tables[0]);
               dview.Sort = "rollno";
               int i = dview.Find(txtrollnosearch.Text);
               if (i < 0)
               {
                   MessageBox.Show("No Recods Found");
               }
               else
               {
                   cmgr.Position = i;
                   drv = (DataRowView)cmgr.Current;
                   txtrollno.Text = drv.Row[0].ToString();
                   txtname.Text = drv.Row[1].ToString();
                   txtcity.Text = drv.Row[2].ToString();
               }
           }
           catch (Exception)
           {
               MessageBox.Show("Incorrect Input..");
               txtrollnosearch.Clear();
               txtrollnosearch.Focus();
           }
       }
    }
}
 
Run the application.

Output


 
I am giving a short descriptive image of the output window below.

Click the ">" button. The selected row will be moved to the next row in the DataGridView as well as the record will be displayed in the TextBoxes.


Similarly, click other buttons (such as First, Last or Previous) to move to another record. You can search for a record on the basis of the roll number of the student. For example I found a record for the student whose "rollno" is 122.


Click the "Search" button. It will select the appropriate row in DataGridView as will as show record in TextBox.

Here are some related resources

Login to add your contents and source code to this article
share this article :
post comment
 

Good but always use exception handling in your code.

Posted by Pradip Pandey Jan 16, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor