如何 select 当前 selected 项目的 DisplayMember 或 DisplayValue?
How do I select the DisplayMember or DisplayValue of the currently selected item?
我有一个函数可以填充下拉列表并设置来自 BindingSource 的显示和值成员,但我不知道如何 select 仅 selected 项的值.
字典绑定源数量:
public Dictionary<int, string> Get_List_of_ManualSchemes()
{
Dictionary<int, string> comboSource = new Dictionary<int, string>();
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
string query = "SELECT [idGMRScheme],[SchemeName] FROM [DBA_Admin].[dbo].[GMR_Schemes]";
using (SqlCommand command = new SqlCommand(query, cnn))
using (SqlDataReader reader = command.ExecuteReader())
while (reader.Read())
{
comboSource.Add((int)reader["idGMRScheme"], (string)reader["SchemeName"]);
}
}
return comboSource;
}
用它填充组合:
public void Populate_ManualSchemes_Combolist(Dictionary<int, string> comboSource)
{
cb_SchemesManual.DataSource = new BindingSource(comboSource, null);
cb_SchemesManual.ValueMember = "value";
cb_SchemesManual.DisplayMember = "text";
}
我尝试这样做:
cb_ManualSchemes.SelectedValue
它 returns 显示的文本,而不是 id,当我尝试时:
cb_ManualSchemes.SelectedText
它returns没什么
因为您使用的字典是键值对的集合。显示成员和值成员必须匹配字典中项目的属性
尝试以下方法
cb_SchemesManual.ValueMember = "Key";
cb_SchemesManual.DisplayMember = "Value";
尝试为 comboSource 使用 DataTable 而不是 Dictionary。
public DataTable Get_List_of_ManualSchemes()
{
DataTable comboSource = new DataTable();
comboSource.Columns.Add("value", typeof(Int32));
comboSource.Columns.Add("text");
DataRow newRow;
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
string query = "SELECT [idGMRScheme],[SchemeName] FROM [DBA_Admin].[dbo].[GMR_Schemes]";
using (SqlCommand command = new SqlCommand(query, cnn))
using (SqlDataReader reader = command.ExecuteReader())
while (reader.Read())
{
newRow = comboSource.NewRow();
newRow["value"] = (Int32)reader["idGMRScheme"];
newRow["text"] = reader["SchemeName"].ToString();
comboSource.Rows.Add(newRow);
}
}
return comboSource;
}
public void Populate_ManualSchemes_Combolist(DataTable comboSource)
{
cb_SchemesManual.DataSource = comboSource;
cb_SchemesManual.ValueMember = "value";
cb_SchemesManual.DisplayMember = "text";
}
使用以下代码绑定您的组合,以便您可以使用 cb_ManualSchemes.SelectedValue
访问该值
public void Populate_ManualSchemes_Combolist(Dictionary<int, string> comboSource)
{
cb_SchemesManual.DataSource=comboSource;
cb_SchemesManual.ValueMember = "Key";
cb_SchemesManual.DisplayMember = "Value";
}
我有一个函数可以填充下拉列表并设置来自 BindingSource 的显示和值成员,但我不知道如何 select 仅 selected 项的值.
字典绑定源数量:
public Dictionary<int, string> Get_List_of_ManualSchemes()
{
Dictionary<int, string> comboSource = new Dictionary<int, string>();
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
string query = "SELECT [idGMRScheme],[SchemeName] FROM [DBA_Admin].[dbo].[GMR_Schemes]";
using (SqlCommand command = new SqlCommand(query, cnn))
using (SqlDataReader reader = command.ExecuteReader())
while (reader.Read())
{
comboSource.Add((int)reader["idGMRScheme"], (string)reader["SchemeName"]);
}
}
return comboSource;
}
用它填充组合:
public void Populate_ManualSchemes_Combolist(Dictionary<int, string> comboSource)
{
cb_SchemesManual.DataSource = new BindingSource(comboSource, null);
cb_SchemesManual.ValueMember = "value";
cb_SchemesManual.DisplayMember = "text";
}
我尝试这样做:
cb_ManualSchemes.SelectedValue
它 returns 显示的文本,而不是 id,当我尝试时:
cb_ManualSchemes.SelectedText
它returns没什么
因为您使用的字典是键值对的集合。显示成员和值成员必须匹配字典中项目的属性
尝试以下方法
cb_SchemesManual.ValueMember = "Key";
cb_SchemesManual.DisplayMember = "Value";
尝试为 comboSource 使用 DataTable 而不是 Dictionary。
public DataTable Get_List_of_ManualSchemes()
{
DataTable comboSource = new DataTable();
comboSource.Columns.Add("value", typeof(Int32));
comboSource.Columns.Add("text");
DataRow newRow;
using (SqlConnection cnn = new SqlConnection(connectionString))
{
cnn.Open();
string query = "SELECT [idGMRScheme],[SchemeName] FROM [DBA_Admin].[dbo].[GMR_Schemes]";
using (SqlCommand command = new SqlCommand(query, cnn))
using (SqlDataReader reader = command.ExecuteReader())
while (reader.Read())
{
newRow = comboSource.NewRow();
newRow["value"] = (Int32)reader["idGMRScheme"];
newRow["text"] = reader["SchemeName"].ToString();
comboSource.Rows.Add(newRow);
}
}
return comboSource;
}
public void Populate_ManualSchemes_Combolist(DataTable comboSource)
{
cb_SchemesManual.DataSource = comboSource;
cb_SchemesManual.ValueMember = "value";
cb_SchemesManual.DisplayMember = "text";
}
使用以下代码绑定您的组合,以便您可以使用 cb_ManualSchemes.SelectedValue
public void Populate_ManualSchemes_Combolist(Dictionary<int, string> comboSource)
{
cb_SchemesManual.DataSource=comboSource;
cb_SchemesManual.ValueMember = "Key";
cb_SchemesManual.DisplayMember = "Value";
}