Solved

How to pass Datagridview Combobox cell as a parameter to a function

Posted on 2009-04-06
6
766 Views
Last Modified: 2012-05-06
I have a datagridview that contains many combobox that will be bound to different sources.  I want to pass the combobox object of a cell in the datagrid to a function as a parameter.  What is the syntax to do so?
0
Comment
Question by:KentDBerry
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 24078887
Kent,

Just for clarification, are you looking to pass the "Value" of the combo box to a function?

Regards,

Keith
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24078927
If you have a datasource for that comboboxes you can use that.
Maybe you can use a DataView for that ...
 


Dim dgvCombo As DataGridViewComboBoxCell = DirectCast(Me.DataGridView1(<Column Name or Index>, <Row Number>), DataGridViewComboBoxCell) 
Dim dv As DataView = DirectCast(dgvCombo.DataSource, DataView) 

Open in new window

0
 

Author Comment

by:KentDBerry
ID: 24078963
No.  I want to pass the object.   I have an existing function that I use to populate combo boxes in general and I am trying to tap into it as shown below.
            For Each row As DataGridViewRow In .Rows
                For i As Integer = 1 To 10
                    Bind_Object_To_DataSource(<reference combobox object here>, .Item("AnswerTypeID", row.Index).Value, IIf(.Item(Trim(Str(i)), row.Index).Value Is DBNull.Value, 0, .Item(Trim(Str(i)), row.Index)))
                    '                    Bind_Object_To_DataSource(.Item(i, row.Index), .Item("AnswerTypeID", row.Index).Value, .Item("A" & Trim(Str(i)) & "_AnswerID", row.Index).Value)
                Next
            Next
 
 
    Public Sub Bind_Object_To_DataSource(ByVal cb As Object, ByVal AnswerTypeID As Integer, ByVal AnswerID As Integer)
        Dim conn As New SqlConnection(My.Settings.ConnectionString)
        Dim sql As String = ""
        Try
            Dim sYear As String = ""
            If cb.Name <> "cbYear" Then
                If Me.cbYear.Text = "" Then
                    sYear = Year(Now()).ToString 'Trim(Me.cbYear.SelectedValue.ToString)
                    Me.cbYear.SelectedText = sYear
                Else
                    Try
                        sYear = Trim(Me.cbYear.SelectedValue.ToString)
                    Catch ex As Exception
                        sYear = ""
                    End Try
                End If
 
            End If
 
            Select Case cb.Name
                Case "cbYear"
                    sql = "SELECT YR as Code, rtrim(str(YR)) as Description FROM BRDWorkslates.dbo.tblEFF_NAFTA_WORKSLATE_REPORT GROUP BY YR HAVING (YR is not Null) and (YR >= 2008) and (YR < 2090) Order by YR DESC"
                Case "cbScientist"
                    sql = "SELECT Researcher as Code, Researcher as Description FROM BRDWorkslates.dbo.tblEFF_NAFTA_WORKSLATE_REPORT Where YR = '" & sYear & "' GROUP BY Researcher HAVING Researcher is not null Order by Researcher ASC"
                Case "cbTrialNumber"
                    sql = "SELECT Trial_NO as Code, Trial_NO as Description FROM BRDWorkslates.dbo.tblEFF_NAFTA_WORKSLATE_REPORT Where YR = '" & sYear & "' and Researcher = '" & Trim(Me.cbScientist.SelectedValue) & "' GROUP BY Trial_NO HAVING Trial_NO is not null Order by Trial_NO ASC"
                Case Else ' combo boxes for data entry
                    sql = "Select AnswerID as Code, Option as Description FROM tblAnswerTypesAndOptions where AnswerTypeID = " & Str(AnswerTypeID)
            End Select
 
            Dim DA As New SqlDataAdapter(sql, conn)
            Dim DS As New DataSet
            DA.Fill(DS, "Codes")
 
            Dim dt As New DataTable
            dt.Columns.Add("Description", GetType(System.String))
            dt.Columns.Add("Code", GetType(System.String))
 
            Dim drDSRow As DataRow
            Dim drNewRow As DataRow
 
            For Each drDSRow In DS.Tables("Codes").Rows
                drNewRow = dt.NewRow
                drNewRow("Description") = drDSRow("Description")
                drNewRow("Code") = drDSRow("Code")
                dt.Rows.Add(drNewRow)
            Next
 
 
            With cb
                .DataSource = dt
                .DisplayMember = "Description"
                .ValueMember = "Code"
 
                .AutoCompleteMode = AutoCompleteMode.Suggest
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .DropDownStyle = ComboBoxStyle.DropDown
                '                .DataBindings.Add(New Binding("SelectedValue", dt, "Code"))
 
                Select Case cb.Name
                    Case "cbYear"
                        cb.SelectedValue = My.Settings.Year
                    Case "cbScientist"
                        cb.SelectedValue = My.Settings.Scientist
                    Case "cbTrialNumber"
                        cb.SelectedValue = My.Settings.TrialNumber
                    Case Else
                        cb.SelectedValue = AnswerID
 
                End Select
            End With
        Catch ex As Exception
 
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window

0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 48

Expert Comment

by:jpaulino
ID: 24079002
Then you can use the dgvCombo from the example I have showed you above!
0
 
LVL 10

Expert Comment

by:kdwood
ID: 24079023
Yes, jpaulino's solution should work just fine.
0
 

Author Closing Comment

by:KentDBerry
ID: 31567103
Thank You.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question