Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net, datagridview, combobox

Posted on 2011-03-21
4
Medium Priority
?
876 Views
Last Modified: 2012-05-11
I have a datagridview with multiple columns.  Two of the columns are comboboxes.  The entries in ComboBox2 is based on the entry in ComboBox1.  For example, ComboBox1 contains values of 10, 20, 30.  Keeping in mind that the datagridview can have multiple rows, when ComboBox1 is set to 10, ComboBox2 should have values of A1, A2, and A3, when ComboBox1 is set to 20, ComboBox2 should have values of B1, B2, and B3, etc.  I don't believe I can accomplish this.  Therefore, I have two questions.
1. Am I correct?
2. Does anyone have a suggestion of how to accomplish this function?
Thanks.
0
Comment
Question by:StoneCutter3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35186019
declare to lists of string with your values and set the combobox values' datasource to the lists

then you wil have to handle the selectedvaluechanged or selectedindexchanged of combobox1 to populate combobox2.

This is straight foward with mere comboboxes, however with the datagridviewcomboboxes, you will have to handle these events and filter for the grid column index etc

I have some code for you below. It may need some sprucing up bit it does what you asked for. Start a new project and add a datagridview with two comboboxcolumns, then copy the code and run

Public Class Form1
    Dim List1 As New List(Of Integer) From {10, 20, 30}
    Dim List2 As New List(Of String) From {"A1", "A2", "A3"}
    Dim List3 As New List(Of String) From {"B1", "B2", "B3"}
    Dim List4 As New List(Of String) From {"C1", "C2", "C3"}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim c1 = DirectCast(Me.DataGridView1.Columns(0), DataGridViewComboBoxColumn)
        c1.DataSource = List1


    End Sub

    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        Try
            If e.ColumnIndex = 0 Then
                Dim c2 = DirectCast(Me.DataGridView1.Item(1, e.RowIndex), DataGridViewComboBoxCell)
                c2.Items.Clear()
                Select Case Me.DataGridView1.Item(0, e.RowIndex).Value.ToString
                    Case "10"
                        c2.DataSource = List2
                    Case "20"
                        c2.DataSource = List3
                    Case "30"
                        c2.DataSource = List4
                End Select
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError

    End Sub
End Class

Open in new window


You need the last handler (data error) because when you start the form the gridview isnot populated and this generates a data error.


DG.png
0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 2000 total points
ID: 35186032
You can, try the below code...

For the sample code below to work create datagridview with col1 and col2.

cheers
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If TypeOf (e.Control) Is ComboBox Then
            Dim cmbox As ComboBox = DirectCast(e.Control, ComboBox)
            If Not cmbox Is Nothing Then
                RemoveHandler cmbox.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged
                AddHandler cmbox.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged
            End If
        End If
    End Sub

    Public Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim cmbox As ComboBox = DirectCast(sender, ComboBox)
        col2.Items.Clear()
        If cmbox.SelectedIndex <> -1 Then
            If cmbox.SelectedItem.ToString() = "10" Then
                col2.Items.Add("A1")
                col2.Items.Add("A2")
                col2.Items.Add("A3")
            ElseIf cmbox.SelectedItem.ToString() = "20" Then
                col2.Items.Add("B1")
                col2.Items.Add("B2")
                col2.Items.Add("B3")
            End If
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        col1.Items.Clear()
        col1.Items.Add("10")
        col1.Items.Add("20")
        col1.Items.Add("30")
    End Sub

Open in new window

0
 

Author Comment

by:StoneCutter3
ID: 35186062
Keep in mind that the user will populate the values in ComboBox1 and ComboBox2, i.e., there could be three values in ComboBox1 or 20 values.  the same for ComboBox2.  Are these examples handling this?  Thanks.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35186100
pago's suggestion works better, so I suggest you look at it rather than mine.
On whether the user has to input more values, well you never asked for that, but yes, you can incorporate that too!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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