2015-11-02 18 views
3

我在Winform應用程序中有一個文本框和列表框。當我在文本框中鍵入一個值(即字符串)我想列表框顯示數據表中的值,當我從列表框中選擇一個特定的值時,它將顯示在文本框中。如何在dataview中顯示列表框中的值

代碼:

private void txtIName_TextChanged(object sender, EventArgs e) 
     { 
      string constring = "Data Source=.;Initial Catalog=Test;User Id=sa;[email protected]"; 
      using (SqlConnection con = new SqlConnection(constring)) 
      { 
       using (SqlCommand cmd = new SqlCommand("SELECT distinct * FROM Item_Details", con)) 
       { 
        cmd.CommandType = CommandType.Text; 
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) 
        { 
         using (dt = new DataTable()) 
         { 
          sda.Fill(dt);        
          DataView dv = new DataView(dt); 
          dv.RowFilter = string.Format("IName Like '%{0}%'", txtIName.Text); 
          listBox1.Visible = true; 
          listBox1.DataSource = dv; 

         } 
        } 
       } 
      } 
     } 

,但我得到了在列表框輸出像 「的System.Data.DataRow」。通過調試,我可以在dataview'dv'中看到我想要的行。我在這裏錯過了什麼?謝謝。

+0

您必須在看到您的評論之前將'listBox1.DisplayMember'設置爲您希望看到的列名稱 –

+0

我通過Google搜索獲得了輸出。但我不知道如何在列表框中顯示文本框中的值。謝謝你的努力。 –

回答

2

每次啓動TextChanged事件時都不需要加載數據。在您的表單的Load事件中加載數據,然後在TextChanged中僅對數據進行過濾就足夠了。然後,使用一個事件像設置TextBox文本ListBoxDoubleClick的:

private DataTable dataTable = new DataTable(); 
private void Form1_Load(object sender, EventArgs e) 
{ 
    string constring = @"Data Source=.;Initial Catalog=Test;User Id=sa;[email protected]"; 
    using (SqlConnection con = new SqlConnection(constring)) 
    { 
     using (SqlDataAdapter sda = new SqlDataAdapter("SELECT distinct * FROM Item_Details", con)) 
     { 
      sda.Fill(dataTable); 
     } 
    } 
    this.listBox1.DataSource = new DataView(dataTable); 
    this.listBox1.DisplayMember = "IName"; 
    this.listBox1.Visible = false; 
} 
private void txtIName_TextChanged(object sender, EventArgs e) 
{ 
    var dv = (DataView)this.listBox1.DataSource; 
    dv.RowFilter = string.Format("IName Like '%{0}%'", txtIName.Text); 
    listBox1.Visible = true; 
} 
private void listBox1_DoubleClick(object sender, EventArgs e) 
{ 
    var item = (DataRowView)listBox1.SelectedItem; 
    if (item != null) 
     this.txtIName.Text = item["IName"].ToString(); 
    else 
     this.txtIName.Text = ""; 

    this.listBox1.Visible = false; 
} 

不要忘記Form1_LoadtxtIName_TextChangedlistBox1_DoubleClick處理程序附加到事件。

+0

請幫助我編碼,因爲我是新來的c#。不,我不希望用戶點擊按鈕輸入數據。我只是想讓用戶點擊輸入按鈕或鼠標點擊來選擇該值。 –

+0

@Rajaaar檢查更新 –

+0

您的代碼顯示錯誤爲「錯誤無法找到類型或名稱空間名稱'DataRowview'(缺少使用指令或程序集引用嗎?) 」 –

0

您必須指定您的DisplayMember,這一定是有些領域在你的數據視圖

listBox1.DisplayMember = "Your Field" 

那麼你可以suscribe到事件SelectedValueChanged:

listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged); 

視覺工作室爲您創建一個事件處理程序

void listBox1_SelectedValueChanged(object sender, EventArgs e) 
     { 
      // you get your selected item here 
     } 

希望它有幫助

+0

是的,它的工作。當我從中選擇時,我也想顯示文本框中的一個列表框值。我是新來的C#,我不知道如何顯示它的價值。謝謝。 –

+0

@Raja aar我編輯了我的答案 – Coding4Fun

+0

private void listBox1_SelectedValueChanged(object sender,EventArgs e) { txtIName.Text = listBox1.SelectedValue.ToString(); }我試過這段代碼,但得到了像「system.data.datarow」這樣的輸出。 –

相關問題