VB.net更改ComboBox选择的值并显示

时间:2014-11-03 11:44:59

标签: vb.net

我在vb.net中使用此代码:

Function GetBillingMonthsGrouped() As List(Of BillingMonthsGrouped)
        Dim conn = New MySqlConnection()
        Dim conn2 = New MySqlConnection()
        Dim myCommand, myCommand2 As New MySqlCommand
        Dim reader, reader2 As MySqlDataReader
        Dim SQL As String

        conn.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "
        conn2.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "

        Dim billingMonthsGrouped = New List(Of BillingMonthsGrouped)

        'customers
        conn.Open()
        SQL = "select MONTH(timestamp) from billing group by MONTH(timestamp) order by MONTH(timestamp) ASC "
        myCommand.Connection = conn
        myCommand.CommandText = SQL
        reader = myCommand.ExecuteReader
        While reader.Read()
            billingMonthsGrouped.Add(New BillingMonthsGrouped(reader.GetString(0), MonthName(reader.GetString(0))))
        End While
        conn.Close()

        Return billingMonthsGrouped
    End Function
    Public Class BillingMonthsGrouped
        Public Sub New(ByVal id As String, ByVal name As String)
            mID = id
            mName = name
        End Sub

        Private mID As String
        Public Property ID() As String
            Get
                Return mID
            End Get
            Set(ByVal value As String)
                mID = value
            End Set
        End Property

        Private mName As String
        Public Property Name() As String
            Get
                Return mName
            End Get
            Set(ByVal value As String)
                mName = value
            End Set
        End Property
    End Class

然后这部分:

ComboBox3.DataSource = GetBillingMonthsGrouped()
ComboBox3.DisplayMember = "Name"
ComboBox3.ValueMember = "ID"
ComboBox3.SelectedIndex = 0

我需要使用哪些代码将ComboBox3的选定值更改为当月?

我尝试使用:

ComboBox3.SelectedValue = DateTime.Now.ToString("MM")

但不会更改所选值。我需要将值作为月份编号,将显示作为月份名称

1 个答案:

答案 0 :(得分:0)

希望comboBox已经拥有月份,您需要将当前月份设置为已选中。然后在这种情况下,您可以使用以下代码选择current month作为选定月份

  ComboBox1.SelectedIndex = Now.Month

如果您只想显示一个月,则表示您可以使用以下代码:

  ComboBox1.SelectedText = DateTime.Today.ToString("MMMM")