从组合框中获取值成员

时间:2016-05-15 10:31:06

标签: vb.net datagridview combobox

对此进行了一些搜索,但找不到足够具体的内容。

我尝试从我的组合框中获取值成员,原因是选定的valuemember将构成填充数据网格视图的查询基础。

以下代码查看T-SQL数据库上的dbo.Calendar。列month只是月份的数字,即1-12,MonthName就像名称所示。

在下面的valuemember命令中测试MsgBox输出,输出只给了我"月"而不是说" 5"如果用户选择" May"在组合框中。因此,我正在传递字符串"月"当我应该传递" 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

3 个答案:

答案 0 :(得分: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,可以安全地转换为{ {1}}。

您不需要完整的Integer数据 - DataSet就足够了。

DataAdapter将为您打开和关闭连接。

如果您使用DataTable,那将为您处理对象。

如果你想让一个处理程序对Using事件作出反应,最好在填充ComboBox之后添加它,以避免在填充CB时触发事件。

顺便提一下,如果您只需要使用{1..12},{“January”...“12月”}填充ComboBox,您可以使用类似

的内容
SelectedIndexChanged

答案 1 :(得分:1)

我想MsgBox(MonthSearch.SelectedValue.ToString)可以完成这项工作。

答案 2 :(得分:0)

您尝试简单的解决方案:

创建公共子

Public Sub ListProfiles(cbo As ComboBox)
    Try
        Conn.Open()
        Dim cmd As New SqlCommand("List_JobProfiles", Conn)
        cmd.CommandType = CommandType.StoredProcedure
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.SelectCommand = cmd
        ds.Clear()
        da.Fill(ds, 0)
        With cbo
            .DisplayMember = "ProName"
            .ValueMember = "ProID"
            .DataSource = ds.Tables(0)
        End With
        Conn.Close()
PROC_DONE:
    Catch ex As Exception
        Conn.Close()
        GoTo PROC_DONE
    End Try
End Sub

事件SelectedIndexChanged:

Private Sub cbJobProfiles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbJobProfiles.SelectedIndexChanged
    Dim cb = DirectCast(sender, ComboBox)
    Dim JobProfileID As Integer
    Dim i As Integer
    If Integer.TryParse(cb.SelectedValue.ToString, i) Then
        JobProfileID = cb.SelectedValue
    End If
    ListJobs(dgvJobs, JobProfileID)
End Sub

在Proc SQL Server中:

CREATE PROCEDURE List_JobProfiles 
    AS
        BEGIN
            SELECT ProID, ProName, ProStatus FROM tblJobProfiles ORDER BY ProID ASC
        END
GO

在VS 2019上测试并为我工作 希望对您有帮助