简体   繁体   English

C#中的组合框我需要在组合框中填充两个值

[英]Combobox in C# i Need to fill two value in combo box

i have a problem with C# code for combo box my code is here 我的组合框的C#代码有问题,我的代码在这里

cmbProjectName.Items.Clear();
if (obds.Tables.Count > 0 && obds.Tables[0].Rows.Count > 0)
{
    for (int iCount = 0; iCount < obds.Tables[0].Rows.Count; iCount++)
    {
        cmbProjectName.Items.Add(obds.Tables[0].Rows[iCount]["Project_Name"].ToString()); 
    }
}

Value Add in Combo Box but i will to add the index of this item my self for further operation on other search please suggest me... 在组合框中有增值功能,但我将自己添加此商品的索引,以便在其他搜索中进行进一步操作,请建议我...

You can create a class 您可以创建一个类

private class Project
{
     public int ID {get; set;}
     public string Name {get; set;}

     public Project (int _id, string _name)
     {
          ID = _id;
          Name = _name
     }
}

Then, you transform the method in this way 然后,您以这种方式转换方法

List<Project> listPrj = new List<Project>();

 if (obds.Tables.Count > 0 && obds.Tables[0].Rows.Count > 0)
 {
        for (int iCount = 0; iCount < obds.Tables[0].Rows.Count; iCount++)
        {
            listPrj.Add(new Project(int.Parse(obds.Tables[0].Rows[iCount]["Project_ID"].ToString()), obds.Tables[0].Rows[iCount]["Project_Name"].ToString());
        }    
 }

cmbProjectName.DisplayMember = Name;
cmbProjectName.ValueMember = ID;
cmbProjectName.DataSource = listPrj;

JAEP's solution can also be applied in desktop apps. JAEP的解决方案也可以应用于桌面应用程序。 Please check this MSDN page about ValueMember . 请检查有关ValueMember MSDN页面

After you specify DisplayMember , ValueMember , and DataSource , you can get selected text and selected value with ComboBo.SelectedText and ComboBo.SelectedValue . 指定DisplayMemberValueMemberDataSource ,可以使用ComboBo.SelectedTextComboBo.SelectedValue获取选定的文本和选定的值。

Or you simply just want the index, you can use ComboBo.SelectedIndex . 或者,您只需要索引,就可以使用ComboBo.SelectedIndex

JAEP's solution using class will work. JAEP使用类的解决方案将起作用。 Binding to a dictionary is another way, here you don't have to create extra class. 绑定到字典是另一种方式,这里您不必创建额外的类。 Source SO question 来源SO问题

The code will be like below in your case. 根据您的情况,代码如下所示。 Replace iCount with the index you want. 将iCount替换为所需的索引。

cmbProjectName.Items.Clear();
Dictionary<int, string> projectsDictionary = new Dictionary<int, string>();
if (obds.Tables.Count > 0 && obds.Tables[0].Rows.Count > 0)
{
    for (int iCount = 0; iCount < obds.Tables[0].Rows.Count; iCount++)
    {
        projectsDictionary.Add(iCount, obds.Tables[0].Rows[iCount]["Project_Name"].ToString());
    }
}   
cmbProjectName.DataSource = new BindingSource(projectsDictionary, null);
cmbProjectName.DisplayMember = "Value";
cmbProjectName.ValueMember = "Key";

Code to retrieve key or value. 检索键或值的代码。

string value = ((KeyValuePair<int, string>)cmbProjectName.SelectedItem).Value;
int key =  ((KeyValuePair<int, string>)cmbProjectName.SelectedItem).Key;

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

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