2016-05-15 40 views
0

これについてかなりの量の検索を行いましたが、十分な具体的なものは見つかりませんでした。コンボボックスから値のメンバーを取得

私は、選択したvaluememberがdatagridviewにデータを読み込むための基礎を形成するという理由から、私のコンボボックスからバリューメンバーを取得しようとしています。

以下のコードは、T-SQLデータベースのdbo.Calendarを参照しています。列monthは、その月の数字(つまり、1〜12)で、MonthNameはその名前のとおりです。

MsgBoxコマンドの出力をテストすると、ユーザーがコンボボックスで「5月」を選択した場合、「5」というよりも出力が「月」になります。その結果、私は "5"を渡す必要があるときに私の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イベントに反応させたい場合は、コンボボックスをポピュレートした後に追加して、CBのポピュレート時にイベントが発生しないようにしてください。あなたが唯一の{1..12}、{「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

関連する問題