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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Sep 12, 2012, 10:18 AM
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
Abhimanyu K VatsaPosted Sep 12, 2012, 9:34 AM
Rewant ChoudharyPosted Sep 12, 2012, 8:09 AM
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();
nallyaPosted Sep 12, 2012, 8:08 AM
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