Home / Learn programming language / Filter Datagridview using textbox (How to search using textbox in c#)

Filter Datagridview using textbox (How to search using textbox in c#)

There are different ways of searching records in ms access and it’s always important to know the easiest and convenient way of how to search data from database in c#.

Datagridview is one of the ways of viewing records in database and maybe the easiest way to view records. To filter datagridview c# using textbox is one easy way of determining a quality app. So let get down to the point of

How to search using Datagridview using textbox in c#:

Create a project in your visual studio and call it DatagridRec.

On your form1, drag a textbox (name= searchgud) and a datagridview(dataGridView1) control to your form. Note: let your textbox be untop of the datagridview control.

filter datagridview
filter datagridview

On your form1_load event or double click on your form1 to trigger load event and type this codes:

 

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.OleDb;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using System.Data.SqlClient;

using System.Xml.Serialization;

using System.IO;

using System.Web;

using System.Web.UI;

using System.Threading;

using System.Collections;

 

namespace DatagridRec

{

public partial class Form1 : Form

{

String db = “”;

static String PATH = “.\\bin\\”;

DataSet memoryDataset;

private OleDbConnection con = null;

private OleDbCommand command = null;

private OleDbDataAdapter dataAdapter = null;

public LoginForm()

{

InitializeComponent();

}

 

 

 

 

public void HandleFilter()

{

// Load The MemoryDataset while loading the Form

 

if (searchgud.Text != “”)

{

Query = “SELECT Item_ID,itemName,itemQty,perItem,profit,Category,datecreated FROM items WHERE itemName LIKE ‘%” + searchgud.Text + “%’ ORDER BY Item_ID”;

}

else

{

Query = “SELECT Item_ID,itemName,itemQty,perItem,profit,Category,datecreated FROM items WHERE active='” + act + “‘ ORDER BY Item_ID”;

}

 

//Reload MemoryDataset from The Query

memoryDataset = api.LoadDataSetByQuery(Query, “Data”, db);

 

try

{

//Count Row Result(s)

int rowCnt = memoryDataset.Tables[0].Rows.Count;

rowCount.Text = rowCnt + ” Record(s) Found for your search”;

 

 

//Bind the Dataset Loaded to the DataGridView

dataGridView1.DataSource = memoryDataset.Tables[0];

dataGridView1.Refresh();

}

catch (Exception e) { MessageBox.Show(e.Message); } // Just to Trap the Error

 

}

 

private void searchgud_TextChanged(object sender, EventArgs e)

{

if (searchgud.Text != “”)

{

HandleFilter();

}

}

}

}

Please check some of my articles on data base connection and just insert it to the sample code above. And you can email me to ask any further question or comment. Thanks

Recommended: How to create LAN login form in c#

 

data-matched-content-rows-num="2" data-matched-content-columns-num="2" data-matched-content-ui-type="image_stacked"

About Joseph Sam

Check Also

c# access database

How to Send email using c#

Emailing can be something very essential to some application maybe your client might be in …

Leave a Reply

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

Translate »