0

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.

cool water
  • 25
  • 2
  • 7
  • I am not sure if I understand you correctly: you want to populate a DGV from a given DataSource; then you want to convert the type of one of the columns into combobox-type, don't you? What values should contain this combobox? – varocarbas Oct 26 '13 at 13:59
  • The combobox will contain a list items of subsidiary ledgers. 100+ rows. My objective for using a datatable is to store the user inputs in memory and check the total amount before inserting all the data in my database. (I don't know if this is a good idea). I'm just learning as I go along. – cool water Oct 26 '13 at 14:06
  • The main reason for relying on a datasource is using what is in the datasource without changing it too much (and affecting both the DGV and the DataSource at the same time more or less easily). If you want to perform a relevant change (converting a column into Combobox), you might better add the records directly instead via DataSource. You can use the DataTable to perform all the corrections you wish, but don't use as a DataSource. Shall I go ahead and write a simple code showing you how to do this? – varocarbas Oct 26 '13 at 14:11
  • yes your help is very much appreciated. – cool water Oct 26 '13 at 14:13
  • Actually my code above is just an experiment. I would like to learn how to insert data directly to a datagridview with a combobox column for lookups just like in MS ACCESS. – cool water Oct 26 '13 at 14:18
  • Just to make clear the point: I deleted my answer because of not liking your attitude (the code I wrote delivered what you were looking for). PS: don't expect help here from me (or people thinking like me) with this attitude. – varocarbas Oct 27 '13 at 17:35
  • Sorry for hurting your feelings that was not my intention. As I've said I'm new to vb.net I was not able to understand the code you suggested. I found a step by step solution easier to understand by beginners like me and posted the link. I don't mean disrespect. – cool water Nov 04 '13 at 14:54

0 Answers0