我是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。这是link。