简体   繁体   English

VB.NET->组合框随机索引

[英]VB.NET -> Combobox random index

I have a question regarding comboboxes in VB.net 2010. 我对VB.net 2010中的组合框有疑问。

In my database I have 4 fields: eg: 在我的数据库中,我有4个字段:例如:

idDetails | DetailsShortCode | Details_Explain | DetailsSortOrder
{autonum}1| DOA              | Death on Arrival| 5
{autonum}2| NDI              | No Display      | 10
{autonum}3| QQA              | In Research     | 4

etc.. 等等..

These values I pull out of the dbase (mySQL) and insert into a CheckedListBox . 我将这些值从dbase(mySQL)中拉出,然后插入CheckedListBox I display the values as DetailsShortCode & " - " & Details_explain. 我将值显示为DetailsS​​hortCode和“-”&Details_explain。 I use a [for loop] to create index numbers, because the sort order is based on the Details Sort order. 我使用[for循环]创建索引号,因为排序顺序基于“详细信息”排序顺序。 Which means that VB.net gets 'fed' with the results in the following order: 这意味着VB.net按照以下顺序“喂”结果:

idDetails | DetailsShortCode | Details_Explain | DetailsSortOrder
3         | QQA              | " ... "         | 4
1         | DOA              | "...."          | 5
2         | NDI              | " ... "         | 10

If i put this in the listbox, the error I shall receive is '3 is an incorrect value for 'index'" Due to the fact that VB.net expects that the CheckedListBox (and also ComboBox ) index always is in a sequential order, as in 0,1,2,3,4..etc.. 如果我将其放在列表框中,我将收到的错误是“ 3是'index'的不正确值”“由于VB.net期望CheckedListBox (以及ComboBox )索引始终按顺序排列,如0、1、2、3、4等。

The problem is the fact that orders in the database can change, items can change, and I have a field in another table containing a comma separated list of the details selected (eg 1;10;14;12;) This means that 1 always must be the item with PrimaryKey 1, and that the displayed item on that index must always be the same... 问题在于数据库中的订单可以更改,项目可以更改,并且我在另一个表中有一个字段,其中包含一个逗号分隔的所选详细信息列表(例如1; 10; 14; 12;)。这意味着始终为1必须是具有PrimaryKey 1的项目,并且该索引上显示的项目必须始终相同...

so what I need, is to know how I can use the Primary Key as an Index Number, and let VB.Net not throw an error when the Index is in a random order.., or give the items a hidden value (like in HTML and PHP), in which I can just use the [for loop] indexes.. 因此,我需要知道如何使用主键作为索引号,并让VB.Net在索引处于随机顺序时不引发错误..,或为项目提供隐藏值(例如HTML和PHP),我可以在其中使用[for循环]索引。


This is the code I use to insert items to the Details CheckedListBox 这是我用来将项目插入到Details CheckedListBox中的代码

Function LoadComboBoxes(ByVal CB As String)
    Dim SQLtext = ""
    Select Case CB
        Case "Details"
            SQLtext = "Select " & _
                      "idDetails, " & _
                      "DetailsCode, " & _
                      "DetailsExplain, " & _
                      "DetailsSortOrder " & _
                      "FROM Details order by DetailsSortOrder"
Dim i = -1
    Dim dr As MySqlDataReader

    Dim cmd As New MySqlCommand(SQLtext, dbconn)
    connect()
    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

    CLBDetails.Items.Clear()
    While dr.Read
       i += 1
       CLBDetails.Items.Insert(i, .GetString(1) & " - " & dr.GetString(2))
     End While

end select
end function

The easiest way might be to use a DataGridView which should have all the functionality of the CheckedComboBox plus the ability to keep the data intact as column data and allow you to reference the ID column as an ID Column rather than an ID aliased as an Index. 最简单的方法可能是使用DataGridView,它应该具有CheckedComboBox的所有功能以及使数据完整保留为列数据的功能,并允许您将ID列作为ID列而不是作为索引别名的ID引用。

Depending on what a CheckedComboBox is (there are lots of these around) and what it inherits from, you should be able to store objects there: 取决于CheckedComboBox是什么(周围有很多)及其继承自什么,您应该能够在其中存储对象:

Class DisplayItem
     Friend ID as Long

     DetailsShortCode As String

     Details_Explain As String
     DetailsSortOrder As String

   Public Function overrides ToString As String
       ' depending on the CheckedComboBox add column seperators?
       return DetailsShortCode & Details_Explain & DetailsSortOrder 
   End Function
End Class

Adding an object such as this to the control lets you retain the PK ref: 将这样的对象添加到控件中,可以保留PK ref:

   ccb.Items(N).ID

While the ToString function lets you format the output as desired. 使用ToString函数可以根据需要设置输出的格式。 Further, you could create a List(Of DisplayItem) or BindingList(Of DisplayItem) and drive the CheckedComboBox by binding the datasource to the List. 此外,您可以创建一个List(Of DisplayItem)或BindingList(Of DisplayItem),并通过将数据源绑定到List来驱动CheckedComboBox。 A DGV is still easier... DGV仍然更容易...

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

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