2012-01-09 95 views
1

內我怎樣才能內(列表視圖)顯示的資料,因此,當他發現該項目是由藍色指示的(列表視圖)內如何搜索列表視圖

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 
} 
+6

不要使用SQL查詢這個樣子。這是非常不安全和不好的做法。使用SQL命令,而不是http://www.codeproject.com/KB/database/sql_in_csharp.aspx – 2012-01-09 13:45:26

回答

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) 
{ 

} 
+0

謝謝> :)你有沒有使用應用程序,而不是asp的例子? – 2012-01-09 14:17:52

+0

請幫我解決這個問題... :( – 2012-01-09 14:46:57

+0

我需要更多的代碼和更詳細的描述嘗試將代碼粘貼到pastebin.com – netadictos 2012-01-09 17:20:01