首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >平面多列组合框-使用DB表填充列

平面多列组合框-使用DB表填充列
EN

Stack Overflow用户
提问于 2016-08-10 16:40:13
回答 2查看 1.4K关注 0票数 0

我找到了一个非常好的免费多列组合框,但不能用它填充第二列,到目前为止我只能显示1列。有没有人有通过Datable做这件事的经验--它必须这样做,至少控制的作者声称是这样的。下面是我的代码:

编辑:

代码语言:javascript
复制
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       Dim SQL As String = "SELECT Name,Surname from MyTable"

       Dim dtb As New DataTable
       dtb.Columns.Add("Name", System.Type.GetType("System.String"))
       dtb.Columns.Add("Surname, System.Type.GetType("System.String"))

       Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")

                Try

                    con.Open()

                    Using dad As New OracleDataAdapter(SQL, con)
                        dad.Fill(dtb)

                    End Using

                    MtgcComboBox1.ColumnNum = 2
                    MtgcComboBox1.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
                    MtgcComboBox1.SourceDataString = {"Name", "Surname"}
                    MtgcComboBox1.SourceDataTable = dtb

                    con.Close()

                Catch ex As Exception
                    'MessageBox.Show(ex.Message)
                Finally
                    con.Dispose()
                End Try

        End Using

下面是控制链接--还有一些指令:http://www.codeproject.com/Articles/8619/Flat-MultiColumn-Combobox-with-Autocomplete

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-10 20:32:22

这是Any way for a combo box with 2 values per line?中答案的简化版本

代码语言:javascript
复制
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dtb As New DataTable
    Using dad As New OracleDataAdapter("SELECT Name,Surname from MyTable", "Data Source=MyDB;User Id=Lucky;Password=MyPassword;")
        dad.Fill(dtb) ' this should add the columns
    End Using

    Dim items = From r In dtb.Rows.Cast(Of DataRow) r(0).ToString & vbNullChar & r(1).ToString
    ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
    ComboBox1.DataSource = items.ToList
    'ComboBox1.DisplayMember = "Name"
    'ComboBox1.ValueMember = "Surname"
End Sub

Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
    e.DrawBackground() ' Fill the background.
    Dim items = ComboBox1.Items(e.Index).ToString.Split(ControlChars.NullChar) ' Extract the Record object corresponding to the combobox item to be drawn.
    Dim loc = e.Bounds.Location, xMid = (loc.X + e.Bounds.Width - loc.X) \ 2 ' Calculate important positions based on the area of the drop-down box.

    TextRenderer.DrawText(e.Graphics, items(0), e.Font, loc, e.ForeColor) ' Draw the first (Unique ID) string in the first half.
    TextRenderer.DrawText(e.Graphics, items(1), e.Font, New Point(xMid + 5, loc.Y), e.ForeColor) ' Draw the second (Name) string in the second half, adding a bit of padding.

    e.Graphics.DrawLine(SystemPens.ButtonFace, xMid, loc.Y, xMid, loc.Y + e.Bounds.Height) ' optional Draw the column separator line right down the middle.
    e.DrawFocusRectangle()        ' Finally, draw the focus rectangle.
End Sub
票数 1
EN

Stack Overflow用户

发布于 2016-08-10 18:29:20

我想您只是错过了显示列的说明。在示例中:

代码语言:javascript
复制
 MtgcComboBox1.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
 MtgcComboBox1.SourceDataString =  {"Name", "SurName"}
 MtgcComboBox1.ColumnWidth = "100;100"
 MtgcComboBox1.SourceDataTable = dtb
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38868261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档