如何在listview中搜索

时间:2012-01-09 13:41:45

标签: c# listview

如何在(listview)中搜索显示数据所以当他发现项目由(listview)中的蓝色表示时

 private void butsearch_Click(object sender, EventArgs e)
 {
      SqlConnection cn = new SqlConnection(Class1.x);

      DataSet mydata = new DataSet();
      mydata.Clear();
      SqlDataAdapter db = new SqlDataAdapter("SELECT * FROM okk WHERE username LIKE '%" + textBox4.Text + "%'", cn);
      db.Fill(mydata, "uspas");
      textBox4.DataBindings.Clear();
      textBox4.DataBindings.Add("text", mydata, "uspas.okk");
      listView1.SelectedItems.data....// I could not be completed because I do not know
 }

1 个答案:

答案 0 :(得分:1)

看看这个例子,这是你可以向ListView添加新元素的方式,我看到button3的操作是搜索和添加,所以这一定是你的问题,如果不是你想要的做。

private void LoadList()
{
    // Get the table from the data set
    DataTable dtable = _DataSet.Tables["Titles"];

    // Clear the ListView control
    listView1.Items.Clear();

    // Display items in the ListView control
    for (int i = 0; i < dtable.Rows.Count; i++)
    {
        DataRow drow = dtable.Rows[i];

        // Only row that have not been deleted
        if (drow.RowState != DataRowState.Deleted)
        {
            // Define the list items
            ListViewItem lvi = new ListViewItem(drow["title"].ToString());
            lvi.SubItems.Add (drow["title_id"].ToString());
            lvi.SubItems.Add (drow["price"].ToString());
            lvi.SubItems.Add (drow["pubdate"].ToString());

            // Add the list items to the ListView
            listView1.Items.Add(lvi);
        }
    }
} 

如果要删除所选项目,可以这样做:

foreach(System.Windows.Forms.ListViewItem eachItem in
this.listView1.SelectedItems)
{

}