C# - How To Use TextChanged To Search And Display Data In DatagridView
In This C# Tutorial We Will See How To Search Data In MySQL On The TextChanged Event And Show This Data On Datagridview Using CSharp Programming Language And MySQL Database.
-
-
Source Code :
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_And_MySQL
{
public partial class TextChanged_Search : Form
{
public TextChanged_Search()
{
InitializeComponent();
}
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='test_db';username=root;password=");
private void TextChanged_Search_Load(object sender, EventArgs e)
{
searchData("");
}
public void searchData(string valueToFind)
{
string searchQuery = "SELECT * FROM users WHERE CONCAT(fname,lname) LIKE '%"+valueToFind+"%'";
MySqlDataAdapter adapter = new MySqlDataAdapter(searchQuery, connection);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView_Data.DataSource = table;
}
private void textBox1_Search_TextChanged(object sender, EventArgs e)
{
searchData(textBox1_Search.Text);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_And_MySQL
{
public partial class TextChanged_Search : Form
{
public TextChanged_Search()
{
InitializeComponent();
}
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='test_db';username=root;password=");
private void TextChanged_Search_Load(object sender, EventArgs e)
{
searchData("");
}
public void searchData(string valueToFind)
{
string searchQuery = "SELECT * FROM users WHERE CONCAT(fname,lname) LIKE '%"+valueToFind+"%'";
MySqlDataAdapter adapter = new MySqlDataAdapter(searchQuery, connection);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView_Data.DataSource = table;
}
private void textBox1_Search_TextChanged(object sender, EventArgs e)
{
searchData(textBox1_Search.Text);
}
}
}
1 comments:
commentsHow do you use this with more than one table?
Reply