从文本框进行搜索时,将数据检索到Datagridview表中. [英] Retrive data into Datagridview table,when there is a search from textbox.
问题描述
Hello Sirs/女士,
我创建了一个表单,在其中插入了DataGridView.而且我也有带有文本框和一个搜索按钮的"first
,middle
和last
"标签.所以我想要的是,如果我在"first
"标签的文本框中输入名称,然后单击搜索按钮,则该行将显示在DataGridView中.
因此,我必须在"DataGridView Task"中添加查询,或者必须在"GridView
"中编写查询.
这是我的代码:
Hello Sirs/Madams,
I had created a form in which, I had inserted a DataGridView. And aslo I am having the "first
, middle
and last
" labels with there TextBoxes and one search Button. So what I want is, if I enter the name in TextBox of "first
" label and hit a search button, its row as to be displayed in DataGridView.
So I have to add the query in "DataGridView Task", or I have to write query inside "GridView
".
This is my code:
namespace Config_admin
{
public partial class Form2 : Form
{
SqlConnection con;
SqlCommand com;
SqlDataReader rdr;
DataSet ds;
SqlDataAdapter da;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True");
con.Open();
com = new SqlCommand("select middle,last from Table2 where first= '"+ textBox1.Text.Trim()+"'",con);
rdr = com.ExecuteReader();
bool temp = false;
while (rdr.Read())
{
//textBox1.Text = rdr.GetString(0);
comboBox1.Text = rdr[0].ToString().Trim();//ToString();// GetString(1);
comboBox2.Text = rdr[1].ToString().Trim();
//textBox4.Text = rdr.GetString(3);
//textBox5.Text = rdr.GetString(4);
//textBox6.Text = rdr.GetString(5);
//textBox7.Text = rdr.GetString(6);
//textBox8.Text = rdr.GetString(7);
//textBox9.Text = rdr.GetString(8);
//textBox10.Text = rdr.GetString(9);
temp = true;
}
if (temp == false)
MessageBox.Show("not found");
con.Close();
con.Open();
ds = new DataSet();
da = new SqlDataAdapter("select * from Table2", con);
da.Fill(ds, "Table2");
con.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
请在这方面帮助我
谢谢
Pradeep CBZ
Please help me in this regard
Thanks
Pradeep CBZ
推荐答案
查看此链接
具有内置过滤器功能的DataGrid [ DataGridView过滤器弹出窗口 [具有过滤功能的Datagridview [具有集成过滤,排序和更新对SQL的更改的DataGridView [ ^ ]
See this links
DataGrid with built-in filter functionality[^]
DataGridView Filter Popup[^]
Datagridview with filtering capability[^]
DataGridView with integrated filtering, sorting, and updating changes to SQL[^]
这篇关于从文本框进行搜索时,将数据检索到Datagridview表中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!