2016-05-15 57 views
0

對此做了一點搜索,但找不到足夠具體的東西。從組合框中獲得有價值的成員

我嘗試從我的組合框中獲取value成員,原因是所選的valuemember將構成填充datagridview的查詢的基礎。

以下代碼查看T-SQL數據庫上的dbo.Calendar。列month只是該月的數字,即1-12,並且MonthName顧名思義。

在下面的MsgBox命令中測試valuemember輸出,輸出只是給我「月」,而不是在用戶在組合框中選擇「可以」時說「5」。因此,當我傳遞「5」時,我傳遞字符串「Month」來嘗試填充我的datagridview。任何人都可以幫助我爲什麼沒有得到「5」?

Dim command As SqlCommand 
     Dim adapter As New SqlDataAdapter() 
     Dim ds As New DataSet() 
     Dim sql As String 
     sql = "select distinct month, MonthName from Calendar order by month asc" 
     Try 
      conn.Open() 
      command = New SqlCommand(sql, conn) 
      adapter.SelectCommand = command 
      adapter.Fill(ds) 
      adapter.Dispose() 
      Command.Dispose() 
      conn.Close() 
      MonthSearch.DataSource = ds.Tables(0) 
      MonthSearch.ValueMember = "month" 
      MonthSearch.DisplayMember = "MonthName" 
      MsgBox(MonthSearch.ValueMember) 
     Catch ex As Exception 
      MessageBox.Show("Cannot open connection! ") 
     End Try 

回答

2

ValueMember引用數據表中用於值的列的名稱。

要參考該物業是SelectedValue

Option Infer On 

Imports System.Data.SqlClient 

Public Class Form1 

    Sub SetUpMonthsCB() 
     Dim dt As New DataTable 

     Dim scsb As New SqlConnectionStringBuilder 
     scsb.DataSource = ".\SQLEXPRESS" 
     scsb.InitialCatalog = "testing" 
     scsb.IntegratedSecurity = True 

     Try 
      Using sqlConn As New SqlConnection(scsb.ConnectionString) 
       ' Here I used an existing table in my database, hence the different SQL. 
       Dim sql = "SELECT DISTINCT M AS month, DATENAME(month, dt) AS MonthName FROM Calendar ORDER BY month ASC" 
       Using da As New SqlDataAdapter(sql, sqlConn) 
        da.Fill(dt) 
       End Using 
      End Using 
      MonthSearch.DataSource = dt 
      MonthSearch.ValueMember = "month" 
      MonthSearch.DisplayMember = "MonthName" 

     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 

     ' add the handler after populating the ComboBox to avoid unwanted firing of the event... 
     AddHandler MonthSearch.SelectedIndexChanged, AddressOf MonthSearch_SelectedIndexChanged 

    End Sub 

    Private Sub MonthSearch_SelectedIndexChanged(sender As Object, e As EventArgs) 
     Dim cb = DirectCast(sender, ComboBox) 
     ' SelectedValue is an Object - you can get the name of its actual type with .SelectedValue.GetType().Name 
     Dim val = CInt(cb.SelectedValue) 
     MsgBox(val.ToString()) 

    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     SetUpMonthsCB() 

    End Sub 

End Class 

SelectedValue會返回一個對象,但你可以SelectedValue.GetType()找到它的實際類型 - 在這種情況下,它碰巧是Byte,它可以安全地轉換爲Integer

對於數據您不需要完整的DataSet - 僅需DataTable即可。

DataAdapter將爲您打開和關閉連接。

如果您使用Using,那麼將負責爲您處理物品。

如果您想讓事件處理程序對SelectedIndexChanged事件做出反應,最好在填充ComboBox之後添加它以避免在CB填充時觸發事件。

順便說一句,如果你只需要填充{1..12},{「一月」 ......「十二月」},你可以使用像

Dim dt = Enumerable.Range(1, 12).Select(Function(x) New With {.month = x, .MonthName = MonthName(x)}).ToList() 
MonthSearch.DataSource = dt 
MonthSearch.ValueMember = "month" 
MonthSearch.DisplayMember = "MonthName" 
+0

謝謝安德魯,這是一個頂級的和翔實的迴應。我特別喜歡你最後的非DB解決方案,看起來像我需要更多地閱讀的區域。 – TJB

1

我猜MsgBox(MonthSearch.SelectedValue.ToString)會做這項工作。

+0

感謝與迴應組合框正確的財產影子:) – TJB