2014-11-03 76 views
0

我使用此代碼在vb.net:VB.net改變ComboBox中選定值,顯示

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") 

但這並不會改變選定的值。我需要將月份數值和月份顯示爲月份名稱

+0

你使用之前'ComboBox3。 SelectedValue = DateTime.Now.ToString(「MM」)',組合框中的可用值是多少?請在嘗試更改SelectedValue之前指定所有可用的鍵/值對。另外,當時'DateTime.Now.ToString(「MM」)'的值是多少? – Neolisk 2014-11-03 13:19:35

回答

0

希望comboBox已經有月份,您需要將當前月份設置爲選中狀態。那麼在這種情況下,你可以使用下面的代碼來選擇current month爲所選月份

ComboBox1.SelectedIndex = Now.Month 

如果你想顯示一個月份僅僅意味着你可以使用代碼:

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

combox盒子不顯示所有12個月,只有那些可用 – user2710234 2014-11-03 13:04:32

+0

查看更新 – 2014-11-03 13:09:20