设置组合框指数 [英] set combobox indices

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

问题描述

我的数据库中有Table。我的表有2列:ID和名称

i希望我的组合框项目填写(名称列值)

和组合框索引开始于(id列。)

例如在表格中

__________________

ID |姓名|

------------------

1 |约翰|

3 |玛丽|

5 |尼克|

------------------

我想组合框第一项=约翰,它索引= 1)和其他这样的行

我可以通过以下代码填写组合框项目:



 datatable dt; 
int arr [] = new int [dt.rows.count];
for int i = 0 ; i < dt.rows.count; i ++)
{
combobox.items。 add (dt.rows [ 0 ] [ ]);
}



------------------------------ ---------------------

但是我不能将id等于组合框项目的索引

help我

解决方案

索引总是从0开始。你可以做一件事。只需在循环之前在 ComboBox 中添加一个默认项(如 - 选择 - ),以便它将在索引0处插入。



然后循环将自动从索引1开始插入。但是您无法完全按照您所写的方式匹配索引。那是因为一个索引不能留空。



但是,您可以将 ID 存储为 ComboBox 项目


组合框中的项目与任何其他集合中的项目类似;它们具有自动递增的从零开始的索引。为了完成您的要求,您需要为表中没有的索引插入虚拟条目。在您的示例中,这将用于索引0,2和4.



您真正想要做的是将Key-Value对对象添加到项集合中。根据您使用的是Forms,WPF还是Web控件,有不同的方法可以执行此操作。由于您使用术语'ComboBox',我将假设您正在使用表单。在这种情况下,最简单的方法是将KeyValuePair对象添加到项集合中,如下所示:



 comboBox.DisplayMember =   Value; 
foreach (DataRow row in dt.Rows)
{
comboBox.Items.Add(
new KeyValuePair< int,string>(
Convert.ToInt32(row [ 0 ]),
Convert.ToString(row [ 1 ])));
}





第一行设置您想要在组合框中显示为文本的成员。默认情况下,调用toString方法,这将为您留下类似[1,john]的内容。将DisplayMember设置为Value将仅显示john。然后,您可以按如下方式访问所选项目的值:



  if (comboBox.SelectedItem   KeyValuePair< int,string>)
{
int selectedKey =((KeyValuePair< int,string>)comboBox.SelectedItem).Key; // 1
string selectedValue =((KeyValuePair< int,string>)comboBox.SelectedItem).Value; // john
}
否则
{
// 未选择任何内容
}


hi, i have Table in my database .my table have 2 Columns: ID and Name
i want to my combobox items fill by (Name Column values)
and combobox Index start by (id columns. )
for example in table
__________________
ID | Name |
------------------
1 | john |
3 | mary |
5 | nick |
------------------
I want to combobox first item = john and it index =1 ) and other rows like this
I can fill combobox items by this code:

datatable dt;
int arr[]=new int [dt.rows.count];
for (int i=0; i < dt.rows.count ;i++)
{
   combobox.items.add(dt.rows[0]["name"]);
}


---------------------------------------------------
But I can't equal id to index of combobox items
help me

解决方案

Index will always start from 0. You can do one thing. Just add one default item (like "--Select--") in ComboBox before the loop, so that it will be inserted at index 0.

Then the loop will automatically start inserting from index 1. But you cannot match the index exactly as you have written. That is because one index cannot be left blank.

However, you can store that ID as the ComboBox Item Value.


The items in a combo-box are like items in any other collection; they have an auto-incrementing zero-based index. In order to accomplish what you are asking, you would need to insert 'dummy' entries for the indexes that you do not have in the table. In your example, this would for indexes 0, 2, and 4.

What you really want to do, is add Key-Value pair objects into your item collection. There are varying ways to do this depending on whether you're using Forms, WPF, or Web controls. Since you use the term 'ComboBox' I'm going to assume you're using Forms. In this case the simplest thing to do is add KeyValuePair objects to your item collection as follows:

comboBox.DisplayMember = "Value";
foreach (DataRow row in dt.Rows)
{
    comboBox.Items.Add(
        new KeyValuePair<int, string>(
            Convert.ToInt32(row[0]),
            Convert.ToString(row[1])));
}



The first line sets the member you want displayed as text in your combo-box. By default the toString method is called, which will leave you with something like "[1, john]". Setting DisplayMember to "Value" will cause only "john" to be displayed. You can then access the values of the selected item as follows:

if (comboBox.SelectedItem is KeyValuePair<int, string>)
{
    int selectedKey = ((KeyValuePair<int, string>)comboBox.SelectedItem).Key; //1
    string selectedValue = ((KeyValuePair<int, string>)comboBox.SelectedItem).Value; //john
}
else
{
    //Nothing selected
}


这篇关于设置组合框指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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