Shezad Ahmed
asked on
Set value to DataGridViewComboBoxColumn
Hi
I need to set the value on a DataGridViewComboBoxColumn .
I populate the combobox by:
I query the SQL database to get the selected value by:
Then set the value by:
But I get an error on Datagridview.
Please help.
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
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
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
But I get an error on Datagridview.
Please help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.