从组合框中获取选定的值 [英] Get selected value from comboBox

查看:183
本文介绍了从组合框中获取选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#,winforms。

I'm using 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.

有2列name和keycode。我只使用以下名称显示名称:

There are 2 columns "name", and "keycode". I display the name only using:

accCollection.DisplayMember = "name";

然后我使用下面的命令来获取对应于该名称的键值。

Then I use the following to get the keycode value that corresponds to the name.

string acct = accCollection.SelectedValue.ToString();

我遇到的问题是keycode不匹配的名称。我虽然这可能是我的查询,所以我做的是显示查询结果在数据网格视图JUST在我填充组合框。数据网格视图显示正确的结果,让我相信我使用错误的代码!

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. 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遇到代码我忘了提及,因为你可以看到我已经分配了数据成员

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一些更多的信息。选择的值应为1557,即名称帐号。但我得到1855,这是一个不同的帐号。像我说的数据表是正确的....这就是为什么im sooooooo困惑!

UPDATE:::: Ok some more info. the selected value should be 1557 which is the names account number. but i get 1855, which is a different account number. like i said the datatable is correct....this is why im sooooooo confused!

UPDATE :: heres一些代码,所以你可以看到如何更新组合框信息!

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 ::只是为了显示数据!
>

UPDATE:: just to show the data i get!

ok所以经过一些帮助调试,ive使用下面的代码来获得这些结果。

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这是错误的

this = 1885 which is wrong

但是当我这样做。

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

它显示正确的数据melksham1557

its shows the correct data, "melksham" "1557"

为什么是这个?

推荐答案

您可以使用 SelectedValue SelectedItem 属性,但也必须检查返回的值是否为null。

You may use SelectedValue or SelectedItem property but also you have to check whether the returned value is null or not.

如果绑定DataTable,则SelectedItem属性返回DataRowView。

If you bind the DataTable then the SelectedItem property return DataRowView.

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

如果是SelectedValue,

In case of SelectedValue,

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

编辑:

Form_Load事件中的代码:

Code in Form_Load event:

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";
}

Button的Click处理程序中的代码:

Code in Click handler of Button:

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]);
}

这篇关于从组合框中获取选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆