0

我是 VB.NET 的新手。请多多包涵。我有一个绑定到数据表的 datagridview。我能够将“SLID”列转换为组合框列。我有这个错误:“System.FormatException:DatagridViewComboboxCell 值无效”。我理论上知道该错误是由组合框的 DisplayMember 引起的。如何使 DataGridView1 接受组合框的 ValueMember?

我的整个代码:

Imports System.Data.SqlClient
Imports System.Configuration

Public Class frmCheckDisbursements

Dim da As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim col As DataColumn
Dim bs As BindingSource


Private Sub frmCheckDisbursements_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Load_DGV_Data()
    Load_SLCombobox()
End Sub

Private Sub Load_DGV_Data()

    dt = New DataTable("TempTable")

    col = New DataColumn("SLID", GetType(System.Int32))
    dt.Columns.Add(col)
    col = New DataColumn("Amount", GetType(System.Decimal))
    dt.Columns.Add(col)

    dgvEntries.AutoGenerateColumns = False
    dgvEntries.DataSource = dt

End Sub

Private Sub Load_SLCombobox()

    Dim CS As String = ConfigurationManager.ConnectionStrings("SimpleAccounting.My.MySettings.SimpleAcctgConnectionString").ConnectionString

    Using con As SqlConnection = New SqlConnection(CS)

        da = New SqlDataAdapter("sp_NET_tblSL_AccountID", con)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.Parameters.AddWithValue("@CompanyID", CInt(ConfigurationManager.AppSettings("CompanyID")))
        da.SelectCommand.Parameters.AddWithValue("@AccountID", 5)

        ds = New DataSet
        da.Fill(ds)
        ds.Tables(0).TableName = "SL"

        bs = New BindingSource
        bs.DataSource = ds.Tables("SL")
        Me.SLBindingSource.DataSource = bs

        Dim dgvc As New DataGridViewComboBoxColumn
        dgvc.DisplayIndex = 0
        dgvc = DirectCast(dgvEntries.Columns(0), DataGridViewComboBoxColumn)
        dgvc.DataSource = SLBindingSource
        dgvc.Name = "SLID"
        dgvc.HeaderText = "SL Name"
        dgvc.DataPropertyName = "SLID"
        dgvc.DisplayMember = "SLName"
        dgvc.ValueMember = "SLID"

    End Using

End Sub

End Class

解决方案:我从 jmcilhinney 发布的 vbforums 获得了这个解决方案。将 ComboBox 列添加到 DataGridView。这是链接

4

0 回答 0