• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Set value to DataGridViewComboBoxColumn

Hi

I need to set the value on  a DataGridViewComboBoxColumn.

I populate the combobox by:

 Public Sub InsertItemsintocmbTransSplit(ByVal cmb As DataGridViewComboBoxColumn, ByVal SQLDatabase As String)

        Dim objConn As System.Data.SqlClient.SqlConnection
        Dim OBJSQL As New System.Data.SqlClient.SqlCommand
        Dim objDR As System.Data.SqlClient.SqlDataReader


        'If Not Linq_Datatable Is Nothing Then
        '    Linq_Datatable = Nothing

        'End If

        'Linq_Datatable = New DataTable


        Try

            Dim strConnectionString As String = "Data Source=" + SQLServerName + ";Initial Catalog=" + SQLDatabase + ";User Id=" + SQLUser + ";Password=" + SQLPassword + ";"

            objConn = New System.Data.SqlClient.SqlConnection(strConnectionString)


            Dim Sql As String


            Sql = "Select TransSplitNo,TransactionSplit" _
                   & " from dbo.TRC_MaintainTransactionSplitFields" _
                   & " order by TransactionSplit DESC "


            Dim da As New SqlDataAdapter(Sql, _
                                objConn)
            Dim dt As New DataTable

            da.Fill(dt)


            objConn.Open()


            OBJSQL = New System.Data.SqlClient.SqlCommand(Sql, objConn)
            ' OBJSQL2 = New System.Data.SqlClient.SqlCommand(Sql2, objConn)

            objDR = OBJSQL.ExecuteReader
            'Dim dt As New DataTable

            cmb.DisplayMember = "TransactionSplit"
            cmb.ValueMember = "TransSplitNo"
            cmb.DataSource = dt

            'If objDR.HasRows Then
            '    While objDR.Read()
            '        cmb.Items.Add(objDR.Item("TransactionSplit").ToString)



            '    End While
            'End If

            '   cmb.Items.Add("None")


        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error-InsertItemsintocmbTransSplit()", MessageBoxButtons.OK, MessageBoxIcon.Error)


        Finally

            OBJSQL.Dispose()
            objDR.Close()
            objConn.Close()




        End Try



    End Sub

Open in new window


I query the SQL database to get the selected value by:

 Public Function GetTranssplitvalue(ByVal NomAccountID As String, ByVal CompanyID As String, ByVal URN As String, ByVal SQLDatabase As String) As String

        Dim objConn As System.Data.SqlClient.SqlConnection
        Dim OBJSQL As New System.Data.SqlClient.SqlCommand
        Dim Linq_Datatable As DataTable
        Dim objDR As System.Data.SqlClient.SqlDataReader




        Linq_Datatable = New DataTable


        Try

            Dim strConnectionString As String = "Data Source=" + SQLServerName + ";Initial Catalog=" + SQLDatabase + ";User Id=" + SQLUser + ";Password=" + SQLPassword + ";"

            objConn = New System.Data.SqlClient.SqlConnection(strConnectionString)


            Dim Sql As String


            Sql = "Select TransSplitID from dbo.TRC_TransactionSplitFieldsLines " _
          & " where NLNominalAccountID = " & "'" & NomAccountID & "'" _
          & " and CompanyID = " & "'" & CompanyID & "'" _
          & " and URN = " & "'" & URN & "'"






            objConn.Open()


            OBJSQL = New System.Data.SqlClient.SqlCommand(Sql, objConn)
            ' OBJSQL2 = New System.Data.SqlClient.SqlCommand(Sql2, objConn)

            objDR = OBJSQL.ExecuteReader

            'Load Linq_Datatable
            Linq_Datatable.Load(objDR)


            For Each row As DataRow In Linq_Datatable.Rows

                Return row("TransSplitID").ToString


            Next

            Return String.Empty

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error-GetTranssplitvalue()", MessageBoxButtons.OK, MessageBoxIcon.Error)


        Finally
            If Not Linq_Datatable Is Nothing Then
                Linq_Datatable = Nothing

            End If
            OBJSQL.Dispose()
            objDR.Close()
            objConn.Close()




        End Try



    End Function

Open in new window


Then set the value by:

   Dim NLNominalAccountID As String = String.Empty
        Dim URN As String = String.Empty
        NLNominalAccountID = NominalLookup1.NominalCode.PrimaryKey.Value
        Dim rowcount As String = dtViewMaintainTransSplit.RowCount


        If rowcount > 0 Then

            Dim dgv As DataGridView = dtViewMaintainTransSplit
            Dim cellValue As String
            Dim TransSplitID As String

            For r As Integer = 0 To dgv.RowCount - 1
                Dim rw As DataGridViewRow = dgv.Rows(r)
                For c As Integer = 0 To dgv.ColumnCount - 1

                    If c = 4 Then
                        cellValue = dgv.Rows(r).Cells(c).Value
                        URN = dgv.Rows(r).Cells(c).Value
                        TransSplitID = GetTranssplitvalue(NLNominalAccountID, CompanyID, URN, "CPS_ReportConsole")
                        TransSplitID = RTrim(TransSplitID)

                        If TransSplitID <> String.Empty Then
                            DirectCast(dtViewMaintainTransSplit(5, r), DataGridViewComboBoxCell).Value = TransSplitID

                        End If
                  



                    End If
                Next


            Next


        End If

Open in new window


But I get an error on Datagridview.

Please help.
0
Shezad Ahmed
Asked:
Shezad Ahmed
1 Solution
 
Shezad AhmedAuthor Commented:
never mind got it to work.

I had to set the value as integer not string.

  If TransSplitID <> String.Empty Then
                            Dim NumTransspiltID As Integer = TransSplitID
                            DirectCast(dtViewMaintainTransSplit(5, r), DataGridViewComboBoxCell).Value = NumTransspiltID

                        End If
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now