Can anyone help me on how can I do auto suggest on gridview when typing on textbox. Almost 1 week I seek for the solution. I already check my select statement, it connect to my database. And now I do not know why that coding is not function. Please somebody help me. This is my current 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;
using System.Drawing.Imaging;
using System.IO;
namespace EmployeeVerification
{
public partial class Form1 : Form
{
//Connection String
string cs = "Server=..;User Id=sa;Password=..;Database=..";
SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//table to show in gridview
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("select [name], [empno], [workno] from [GMT].[dbo].[m_employee] where not [recsts] = 'R' order by empno", con);
dt = new DataTable();
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
//make the textbox read only
textBoxICPass.ReadOnly = true;
textBoxPassport.ReadOnly = true;
textBoxDept.ReadOnly = true;
textBoxSection.ReadOnly = true;
pictureBox1.Visible = false;
dataGridView1.Visible = false;
textBoxEmplNo.CharacterCasing = CharacterCasing.Upper;
textBoxWorkNo.CharacterCasing = CharacterCasing.Upper;
textBoxName.CharacterCasing = CharacterCasing.Upper;
DataGridViewColumn column = dataGridView1.Columns[0];
column.Width = 300;
}
//auto suggest on gridview when typing on textbox
private void textBoxName_TextChanged(object sender, EventArgs e)
{
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("select [name], [empno], [workno] from m_employee where name like '%" + textBoxName.Text + "%' and not [recsts] = 'R' order by empno", con);
dt = new DataTable();
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
if (textBoxName.Text != null)
{
dataGridView1.Visible = true;
}
}
private void labelEmplNo_Click(object sender, EventArgs e)
{
}
private void textBoxEmplNo_TextChanged(object sender, EventArgs e)
{
}
private void textBoxWorkNo_TextChanged(object sender, EventArgs e)
{
}
private void labelTitle_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void buttonSelect_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void textBoxICPass_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
}
//show the row value in textbox
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
textBoxName.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBoxEmplNo.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBoxWorkNo.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBoxICPass.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
textBoxPassport.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBoxDept.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBoxSection.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
dataGridView1.Visible = false;
}
//clear all the textbox fields after click
private void buttonClear_Click(object sender, EventArgs e)
{
foreach (Control crt in this.Controls)
{
if (crt.GetType() == typeof(TextBox))
crt.Text = "";
pictureBox1.Visible = false;
}
dataGridView1.Visible = false;
}
private void labelICPass_Click(object sender, EventArgs e)
{
}
private void textBoxWorkNo_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBoxWorkNo.Text != "")
{
//Do something
string selectSql = "select name, empno, icnum, passport, deptno, section from m_employee where workno=@workno";
SqlCommand cmd = new SqlCommand(selectSql, con);
cmd.Parameters.AddWithValue("@workno", textBoxWorkNo.Text);
bool isDataFound = false;
try
{
con.Open();
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
isDataFound = true;
textBoxEmplNo.Text = (read["empno"].ToString());
textBoxName.Text = (read["name"].ToString());
textBoxICPass.Text = (read["icnum"].ToString());
textBoxPassport.Text = (read["passport"].ToString());
textBoxDept.Text = (read["deptno"].ToString());
textBoxSection.Text = (read["section"].ToString());
}
}
if(!isDataFound)
{
textBoxEmplNo.Text = "";
textBoxWorkNo.Text = "";
textBoxName.Text = "";
// Display message here that no values found
MessageBox.Show("No Result Found");
}
}
finally
{
con.Close();
}
}
else
{
textBoxEmplNo.Text = "";
textBoxName.Text = "";
}
string imgFilePath = @"C:\Users\hamizah\Documents\Visual Studio 2013\WebSites\EV\photo\" + textBoxWorkNo.Text + ".jpg";
if (File.Exists(imgFilePath))
{
pictureBox1.Visible = true;
pictureBox1.Image = Image.FromFile(imgFilePath);
}
else
{
// Display message that No such image found
// MessageBox.Show("No Image Found");
pictureBox1.Visible = true;
pictureBox1.Image = Image.FromFile(@"C:\Users\hamizah\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
}
}
}
private void textBoxEmplNo_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBoxEmplNo.Text != "")
{
string selectSql = "select name, workno, icnum, passport, deptno, section from m_employee where empno=@empno";
SqlCommand cmd = new SqlCommand(selectSql, con);
cmd.Parameters.AddWithValue("@empno", textBoxEmplNo.Text);
bool isDataFound = false;
try
{
con.Open();
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
isDataFound = true;
textBoxWorkNo.Text = (read["workno"].ToString());
textBoxName.Text = (read["name"].ToString());
textBoxICPass.Text = (read["icnum"].ToString());
textBoxPassport.Text = (read["passport"].ToString());
textBoxDept.Text = (read["deptno"].ToString());
textBoxSection.Text = (read["section"].ToString());
}
}
if(!isDataFound)
{
textBoxEmplNo.Text = "";
textBoxWorkNo.Text = "";
textBoxName.Text = "";
// Display message here that no values found
MessageBox.Show("No Result Found");
}
}
finally
{
con.Close();
}
}
else
{
textBoxWorkNo.Text = "";
textBoxName.Text = "";
}
string imgFilePath = @"C:\Users\hamizah\Documents\Visual Studio 2013\WebSites\EV\photo\" + textBoxEmplNo.Text + ".jpg";
if(File.Exists(imgFilePath))
{
pictureBox1.Visible = true;
pictureBox1.Image = Image.FromFile(imgFilePath);
}
else
{
// Display message that No such image found
MessageBox.Show("No Image Found");
}
}
}
}
}
Make these changes within your code:
dataGridView1.Visible = false;
at various places within your code, remove all of them for instance, just for the sake of testing, you can add them later based on your requirements. where not [recsts] = 'R' order by empno"
because for now it is useless and I don't understand why you are using this, you can apply filters later and also select ... from m_employee
instead of [dbo]... etc.. dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
get data from selected row like this: textBoxName.Text = dataGridView1.Rows[e.RowIndex].Cells["name"].Value.ToString();
Because in future, if you increase/decrease the number of columns or re-arrange the order of getting data, it will not effect. I've tested your code with these modifications and it works perfectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.