简体   繁体   English

从comboBox中获取选定的值

[英]Get selected value from comboBox

I'm using C#, winforms. 我正在使用C#,winforms。

Got a small problem. 遇到了一个小问题。 I've debugged as much as possible that leads me to believe the code I'm using is causing the problem. 我尽可能地调试,这让我相信我正在使用的代码导致问题。 Ok so I have a combo box that is filled with data from a query. 好的,我有一个充满查询数据的组合框。

There are 2 columns "name", and "keycode". 有2列“名称”和“键码”。 I display the name only using: 我只使用以下方式显示名称:

accCollection.DisplayMember = "name";

Then I use the following to get the keycode value that corresponds to the name. 然后我使用以下内容来获取与名称对应的keycode值。

string acct = accCollection.SelectedValue.ToString();

The problem I have is the keycode does NOT match the name. 我遇到的问题是键码与名称不匹配。 I though this may be my query so what I did was to display the query results in a data grid view JUST before I fill the comboBox. 我虽然这可能是我的查询,所以我做的是在填充comboBox之前在数据网格视图中显示查询结果。 The data grid view displays the correct results which leads me to believe that I'm using the wrong code! 数据网格视图显示正确的结果,这使我相信我使用了错误的代码!

Hopefully it's a silly one line problem, if you guys want any more code let me know and I will edit this post. 希望这是一个愚蠢的一线问题,如果你们想要更多的代码让我知道,我将编辑这篇文章。

UPDATE heres the code i forgot to mention, as you can see i already have assigned the data member 更新继承了我忘记提及的代码,因为你可以看到我已经分配了数据成员

accCollection.DataSource = myTable;
accCollection.DisplayMember = "name";
accCollection.ValueMember = "keycode";

UPDATE:::: Ok some more info. 更新::::还有更多信息。 the selected value should be 1557 which is the names account number. 所选值应为1557,即名称帐号。 but i get 1855, which is a different account number. 但我得到1855,这是一个不同的帐号。 like i said the datatable is correct....this is why im sooooooo confused! 就像我说数据表是正确的....这就是为什么我sooooooo困惑!

UPDATE:: heres some code so you can see how i update the combo box with the info! 更新::下载一些代码,以便您可以看到我如何使用信息更新组合框!

SqlCommand accountFill = new SqlCommand("SELECT name, keycode FROM dbo.Customer", conn1);
SqlDataAdapter readacc = new SqlDataAdapter(accountFill);
DataTable dt = new DataTable();


readacc.Fill(dt);
dataGridView3.DataSource = dt;
conn1.Close();
accCollection.DataSource = dt;
accCollection.DisplayMember = "name";
accCollection.ValueMember = "keycode";

then i pass the following into my task that calls my other query. 然后我将以下内容传递给我调用其他查询的任务。

private void button1_Click_1(object sender, EventArgs e)
{
    checkBox1.Checked = true;
    string acct = accCollection.SelectedValue.ToString();

    Task t = new Task(() => GetsalesFigures(acct));
    t.Start();
}

UPDATE:: just to show the data i get! 更新::只是为了显示我得到的数据! 在此输入图像描述

ok so after some help with debugging, ive used the follwing code to get these resuults. 好的,所以在调试一些帮助之后,我已经使用了以下代码来获得这些结果。

var result = accCollection.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString());
}

this = 1885 which is wrong 这= 1885这是错的

BUT when i do this. 但是,当我这样做。

DataRowView row = (DataRowView)accCollection.SelectedItem;
if (row != null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

its shows the correct data, "melksham" "1557" 它显示了正确的数据,“melksham”“1557”

why is this? 为什么是这样?

You may use SelectedValue or SelectedItem property but also you have to check whether the returned value is null or not. 您可以使用SelectedValueSelectedItem属性,但您还必须检查返回的值是否为null。

If you bind the DataTable then the SelectedItem property return DataRowView. 如果绑定DataTable,则SelectedItem属性将返回DataRowView。

DataRowView row = (DataRowView)accCollection.SelectedItem;
if(row!=null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

In case of SelectedValue, 如果是SelectedValue,

var result = accCollection.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString()); 
}          

EDIT: 编辑:

Code in Form_Load event: Form_Load事件中的代码:

private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("A1", typeof(int));
    dt.Columns.Add("A2");
    dt.Rows.Add(1, "A");
    dt.Rows.Add(2, "B");

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "A2";
    comboBox1.ValueMember = "A1";
}

Code in Click handler of Button: Button的Click处理程序中的代码:

var result = comboBox1.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString());
}


DataRowView row = (DataRowView)comboBox1.SelectedItem;
if (row != null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

Several Things: 好几件事:

  1. DisplayMember property should be the name of the column to be displayed inside the combobox. DisplayMember属性应该是要在组合框内显示的列的名称。
  2. ValueMember property should be the name of the column which is the value of the item. ValueMember属性应该是列的名称,该列是项的值。

     accCollection.DisplayMember = "name"; accCollection.ValueMember = "key"; 

    If you want the value of the selected item you should use: 如果您想要所选项目的值,您应该使用:

     string acct = accCollection.SelectedValue.ToString(); 

    Get the Display text as : 获取显示文本为:

     string acct = accCollection.SelectedText; 

See this for details . 请参阅了解详情。

I have created a DataTable & i used to bind the dataTable to the combobox to display the title types and i wanted to get the title id which is in the datatable to a variable. 我创建了一个DataTable,我用来将dataTable绑定到组合框以显示标题类型,我想获得数据表中的标题id到变量。

      cmbTitle.DataSource=dataTable;
      cmbTitle.DisplayMember="title_type";
      cmbTitle.ValueMember="id";

then on the selection changed event of cmbtitle, i got the selected value member to a string and parsed in into an integer to save it back for the database as it is a foreign key. 然后在cmbtitle的选择更改事件中,我将选定的值成员转换为字符串并解析为整数以将其保存回数据库,因为它是外键。

  private void cmb_title_SelectedIndexChanged(object sender, EventArgs e)
  {
       string userTitle = cmb_title.SelectedValue.ToString();            
       int.TryParse(userTitle, out this.userTitle);
       MessageBox.Show(this.userTitle.ToString());            
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM