I'm new to VB.NET. Please bear with me. I have a datagridview that is bound to a datatable. I was able to convert the "SLID" column into a combobox column. I'm have this error: "System.FormatException:DatagridViewComboboxCell value is not valid". I know in theory that the error is caused by DisplayMember of the combobox. How can I make the DataGridView1 accept the ValueMember of the combobox?
my whole code:
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
Solution: I got this solution from vbforums posted by jmcilhinney. Adding a ComboBox Column to a DataGridView. This is the link.