我有从sql数据库获取数据的listview ..我想按文本框中输入的文本搜索并在单击按钮后显示结果并隐藏不匹配的记录
这是我的方法
plz help
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;
namespace Inventory_Manager_Pro
{
public partial class Modify : Form
{
public SqlConnection cn = new SqlConnection("Data Source=10.0.0.13;Initial Catalog=INVENTDB;Persist Security Info=True;User ID=sa;Password=farespila010A@;Encrypt=False");
public Modify()
{
InitializeComponent();
}
private void populate()
{
listView1.Items.Clear();
SqlCommand cm = new SqlCommand("SELECT * FROM lapdev", cn);
try
{
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem it = new ListViewItem(dr["fillingcode"].ToString());
it.SubItems.Add(dr["username"].ToString());
it.SubItems.Add(dr["branch"].ToString());
it.SubItems.Add(dr["department"].ToString());
it.SubItems.Add(dr["agency"].ToString());
it.SubItems.Add(dr["computername"].ToString());
it.SubItems.Add(dr["lapmodel"].ToString());
it.SubItems.Add(dr["lapserial"].ToString());
it.SubItems.Add(dr["assetnumber"].ToString());
it.SubItems.Add(dr["os"].ToString());
it.SubItems.Add(dr["winlicense"].ToString());
it.SubItems.Add(dr["office"].ToString());
it.SubItems.Add(dr["officelicense"].ToString());
it.SubItems.Add(dr["hddsize"].ToString());
it.SubItems.Add(dr["processor"].ToString());
it.SubItems.Add(dr["ram"].ToString());
it.SubItems.Add(dr["macadress"].ToString());
it.SubItems.Add(dr["ipadress"].ToString());
listView1.Items.Add(it);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Modify_Shown(object sender, EventArgs e)
{
try
{
cn.Open();
populate();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.ExitThread();
}
}
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear(); // clear list items before adding
listView1.Items.AddRange(Items.Where(i=>string.IsNullOrEmpty(textBox1.Text)||i.Name.StartsWith(textBox1.Text))
.Select(c => new ListViewItem(c.Name)).ToArray());
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:2)
在你的pupolate方法中这样做:
private void populate()
{
listView1.Items.Clear();
SqlCommand cm;
if(textBox1.Text == "")
cm = new SqlCommand("SELECT * FROM lapdev", cn);
else
cm = new SqlCommand("SELECT * FROM lapdev WHERE YourField='" + textBox1.Text + "'", cn);
try
{
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem it = new ListViewItem(dr["fillingcode"].ToString());
it.SubItems.Add(dr["username"].ToString());
it.SubItems.Add(dr["branch"].ToString());
it.SubItems.Add(dr["department"].ToString());
it.SubItems.Add(dr["agency"].ToString());
it.SubItems.Add(dr["computername"].ToString());
it.SubItems.Add(dr["lapmodel"].ToString());
it.SubItems.Add(dr["lapserial"].ToString());
it.SubItems.Add(dr["assetnumber"].ToString());
it.SubItems.Add(dr["os"].ToString());
it.SubItems.Add(dr["winlicense"].ToString());
it.SubItems.Add(dr["office"].ToString());
it.SubItems.Add(dr["officelicense"].ToString());
it.SubItems.Add(dr["hddsize"].ToString());
it.SubItems.Add(dr["processor"].ToString());
it.SubItems.Add(dr["ram"].ToString());
it.SubItems.Add(dr["macadress"].ToString());
it.SubItems.Add(dr["ipadress"].ToString());
listView1.Items.Add(it);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
并在按钮点击事件中:
private void button1_Click(object sender, EventArgs e)
{
//listView1.Items.Clear(); // clear list items before adding
populate();
//you type what you want in textbox then click button.
}
你甚至不需要在按钮点击时调用clear,因为populate方法已经处理了这个问题。