如何使用 DataTable 作为数据源从 ComboBox 获取整数值

发布于 2024-09-07 07:32:43 字数 835 浏览 3 评论 0原文

我正在使用与此类似的代码来使用数据库中的项目填充组合框。显示工作正常,但是当我尝试获取 combobox.SelectedValue 时,它返回一个 DataRowView,其中我需要一个整数。显然这是因为我没有将值转换为整数,但是函数 CInt(cboPosition.SelectedValue) 抛出了 InvalidCastException。有什么方法可以让 ValueMember 的类型成为 Integer 吗?

Dim cn As New SqlConnection(CreditDisputesLogConn)
    Dim cmd As New SqlCommand("CustomersLookup", cn)
    Dim da As New SqlDataAdapter(cmd)
    cmd.CommandType = CommandType.StoredProcedure
    Try
        Dim dt As New DataTable
        da.Fill(dt)
        uxCustomerName.DataSource = dt
        uxCustomerName.DisplayMember = "CustomerName"
        uxCustomerName.ValueMember = "CustomerID"
        uxCustomerName.SelectedIndex = -1
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        cn.Close()
        cn.Dispose()
    End Try

I am using code similar to this to populate a combobox with items from a database. The display works fine, but when I try to get the combobox.SelectedValue it is returning a DataRowView, where I need an integer. Obviously this is becuase I haven't casted the value to an integer, but the function, CInt(cboPosition.SelectedValue) is throwing an InvalidCastException. Is there any way that I can get the ValueMember's type to be an Integer?

Dim cn As New SqlConnection(CreditDisputesLogConn)
    Dim cmd As New SqlCommand("CustomersLookup", cn)
    Dim da As New SqlDataAdapter(cmd)
    cmd.CommandType = CommandType.StoredProcedure
    Try
        Dim dt As New DataTable
        da.Fill(dt)
        uxCustomerName.DataSource = dt
        uxCustomerName.DisplayMember = "CustomerName"
        uxCustomerName.ValueMember = "CustomerID"
        uxCustomerName.SelectedIndex = -1
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        cn.Close()
        cn.Dispose()
    End Try

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2024-09-14 07:32:43

您需要在 SQL 中选择 IDText 字段。

如果您确实选择了ID,则可以使用MsgBox TypeName(cboPosition.SelectedValue)来确定所选值的类型。这可能会帮助您进行调试。


编辑:如果 SelectedValue 返回 DataRowView,您可以像这样访问 ID 字段:

Dim myDataRowView As DataRowView = DirectCast(cboPosition.SelectedValue, DataRowView)
Dim myId as Integer = CInt(myDataRowView("CustomerID"))

至于为什么 SelectedValue 返回DataRowView 虽然设置了正确的 ValueMember,但我不知道...


EDIT2:我找到了 SelectedValue 返回整行的原因仅 ValueMember:您必须在“right”中设置属性" 顺序,即:(

    uxCustomerName.DisplayMember = "CustomerName"
    uxCustomerName.ValueMember = "CustomerID"
    uxCustomerName.DataSource = dt

首先是 DisplayMemberValueMember然后 DataSource。)

You need to select both fields ID and Text in your SQL.

If you did select ID, you can use MsgBox TypeName(cboPosition.SelectedValue) to determine the type of the selected value. This might help you with debugging.


EDIT: If SelectedValue returns a DataRowView, you can access the ID field like this:

Dim myDataRowView As DataRowView = DirectCast(cboPosition.SelectedValue, DataRowView)
Dim myId as Integer = CInt(myDataRowView("CustomerID"))

As to why SelectedValue returns the DataRowView although a correct ValueMember is set, I have no idea...


EDIT2: I found the reason why SelectedValue returns the whole row instead of just the ValueMember: You have to set the properties in the "right" order, i.e.:

    uxCustomerName.DisplayMember = "CustomerName"
    uxCustomerName.ValueMember = "CustomerID"
    uxCustomerName.DataSource = dt

(First DisplayMember and ValueMember, then DataSource.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文