Gridview
how can i search record in gridview.example suppose i add "v" in the textbox.then i can find all records starting from v(desktop application in c#)
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
nallyaRank #8,588 · 219 reputationPosted Sep 12, 2012, 4:08 AM EDT
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
SELECT column_name(s)FROM table_name
WHERE column_name LIKE pattern
ex:SELECT * FROM Persons
WHERE City LIKE 's%'
Source:http://www.w3schools.com/sql/sql_like.asp
Rewant ChoudharyRank #4,270 · 354 reputationPosted Sep 12, 2012, 4:09 AM EDT
connect = new SqlConnection(myConnection);
command = new SqlCommand();
connect.Open();
command.Connection = connect;
string komut = "SELECT * FROM table name WHERE authername='"+textbox1.Text+"'";
try
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand = connect.CreateCommand();
sqlCommand.CommandText = komut;
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connect);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
//Create a DataTable to hold the query results.
DataTable dTable = new DataTable();
//Fill the DataTable.
sda.Fill(dTable);
GridView1.DataSource = dTable;
GridView1.DataBind();
}
catch (SqlException)
{
//Console.WriteLine(e.StackTrace);
}
reader.Close();
connect.Close();
Abhimanyu K VatsaRank #14 · 51.7K reputationPosted Sep 12, 2012, 5:34 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 12, 2012, 6:18 AM EDT
Try this....
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 Search_datagridview
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void bindgrid()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select name from employee where name like '" + textBox1.Text + "%'";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataMember = "employee";
dataGridView1.DataSource = ds;
con.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
bindgrid();
}
}
}
Thanks
If this post helps you mark it as answer