我有一個綁定到數據表的組合框。我希望能夠獲得所選項目中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();
}
}
做。如果是這樣,你可以在選擇改變事件'private void CboItemId_SelectionChanged(Object sender,SelectionChangedEventArgs e){string} = CboItemId.SelectedItem.ToString();字符串val = CboItemId.SelectedValue.ToString(); }' – prasy