2015-05-08 32 views
0

我有一個綁定到數據表的組合框。我希望能夠獲得所選項目中DisplayMemberPath和SelectedValuePath項目的值。下面是我有:從組合框獲取綁定到數據的值表

private void CboCustomerList_DoWork(object sender, DoWorkEventArgs e) 
{ 

    string connectionString = Settings.Default.ProdConnectionString; 
    SqlConnection connection = new SqlConnection(connectionString); 

    SqlCommand SqlCmd = new SqlCommand(); 
    SqlCmd.CommandType = CommandType.StoredProcedure; 
    SqlCmd.CommandText = "sp_GetItemIds"; 
    SqlCmd.Parameters.Add("@customer", SqlDbType.NVarChar).Value = CboCustomerList.SelectedValue.ToString().Trim(); 
    SqlCmd.Connection = connection; 
    SqlDataAdapter sqlDa = new SqlDataAdapter(); 
    sqlDa.SelectCommand = SqlCmd; 
    DataSet ds = new DataSet(); 


    try 
    { 
     sqlDa.Fill(ds, "ITEMS"); 
     DataRow nRow = ds.Tables["ITEMS"].NewRow(); 
     nRow["EXTITEM"] = string.Empty; 
     nRow["ITEMID"] = "0"; 
     ds.Tables["ITEMS"].Rows.InsertAt(nRow, 0); 

     //Binding the data to the combobox. 
     CboItemId.DataContext = ds.Tables["ITEMS"].DefaultView; 
     connection.Close(); 

     CboItemId.DisplayMemberPath = 
      ds.Tables["ITEMS"].Columns["EXTITEM"].ToString(); 

     CboItemId.SelectedValuePath = 
      ds.Tables["ITEMS"].Columns["ITEMID"].ToString(); 


    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    finally 
    { 
     connection.Close(); 
     SqlCmd.Dispose(); 
    } 

} 
+0

做。如果是這樣,你可以在選擇改變事件'private void CboItemId_SelectionChanged(Object sender,SelectionChangedEventArgs e){string} = CboItemId.SelectedItem.ToString();字符串val = CboItemId.SelectedValue.ToString(); }' – prasy

回答

0

你可以得到它的選擇改變的情況下,您希望得到基於在組合框中選擇所選擇的價值和選擇的項目值

private void CboItemId_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     string item = CboItemId.SelectedItem.ToString(); 
     string val = CboItemId.SelectedValue.ToString(); 
    } 
+0

這將返回「System.Data.DataRowView」 – rigamonk

+0

@rigamonk:您的組合框是否顯示綁定數據 – prasy

+0

是的。一切都顯示正常 – rigamonk

相關問題