?
Solved

select item in combibox contained within a datagridview in vb.net 2005

Posted on 2007-07-26
9
Medium Priority
?
274 Views
Last Modified: 2013-11-26
Hi,

I have the following code: -

Public Class Form1

    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 cbo As ComboBox = CType(e.Control, ComboBox)
            cbo.Items.Clear()
            Select Case Me.DataGridView1.CurrentRow.Index
                Case 0
                    cbo.Items.Add("1.1")
                    cbo.Items.Add("1.2")
                    cbo.SelectedItem = 1
                Case 1
                    cbo.Items.Add("2.1")
                    cbo.Items.Add("2.2")
                    cbo.Items.Add("2.3")

                Case 2
                    cbo.Items.Add("3.1")

            End Select
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Add("Test")
    End Sub
End Class


What it does is populate a datagridview with a straing column and anothe column with a combibox that is populated individually. When the combibox is populated it does not show the first piece of text i,e, if the combibox has : -

1.1
1.2
1.3

it shows the combibox as being blank until you clikc the down arrow. Usually with standard from combiboxes I use the code: -

cmbtest.selecteditem = 1

but this does not seem to work with combiboxes that are contained within a datagridview.

I would appreciate your help.

Many Thanks
Lee
0
Comment
Question by:ljhodgett
  • 5
  • 4
9 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19574615
Lee,

I did some thorough testing, and I don't know if this is what you want, but here it goes:

    ' Set the number of rows.
    Me.DataGridView1.RowCount = 10

    ' Fill the ComboBox column with all the valid values for the complete list, to
    ' avoid any data errors.
    For rowIndex As Integer = 0 To 9
      For index As Integer = 1 To 5
        Dim text As String = (rowIndex + 1) & "." & index
       
        Me.Column1.Items.Add(text)
      Next
    Next

    ' Set the initial row values.
    For Each row As DataGridViewRow In Me.DataGridView1.Rows

      Dim cell As DataGridViewCell = row.Cells(Me.Column1.Index)

      cell.Value = (row.Index + 1) & ".1"
    Next
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19574630
 Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    Dim cbo As ComboBox = CType(e.Control, ComboBox)
    cbo.Items.Clear()

    Dim rowIndex As Integer = Me.DataGridView1.CurrentRow.Index

    ' Set the items for the current row.
    For index As Integer = 1 To 5
      Dim text As String = (rowIndex + 1) & "." & index
      cbo.Items.Add(text)
    Next

    ' Highlight the current text from the cell in the ComboBox.
    Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRow.Cells(Me.Column1.Index)

    cbo.SelectedIndex = cbo.FindString(cell.Value)

  End Sub

Bob
0
 

Author Comment

by:ljhodgett
ID: 19574963
Hi Bob,

I've tried the following code: -

Public Class Form1

    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 cbo As ComboBox = CType(e.Control, ComboBox)
            cbo.Items.Clear()
            Select Case Me.DataGridView1.CurrentRow.Index
                Case 0
                    cbo.Items.Add("1.1")
                    cbo.Items.Add("1.2")
                    'Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRow.Cells(Me.Column1.Index)

                    'cbo.SelectedIndex = cbo.FindString(cell.Value)
                Case 1
                    cbo.Items.Add("2.1")
                    cbo.Items.Add("2.2")
                    cbo.Items.Add("2.3")

                Case 2
                    cbo.Items.Add("3.1")

            End Select
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Add("Test")
    End Sub
End Class

When I click the button it usually runs the edit control procedure but doesn't seem to in this case.

Best Regards
Lee
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 19575089
Lee,

You have to keep something in mind with the DataGridView and combo box columns:

1) The cell value needs to be found in the Items collection

2) The Items collection for the editing ComboBox can be changed in the EditingControlShowing event handler

3) You need to set the SelectedIndex for the editing ComboBox, or it will display a blank in the text portion when you click on the dropdown button.

4) Create a new test form, and try that sample code out, and see how it works.

Bob
0
 

Author Comment

by:ljhodgett
ID: 19575190
Sorry Bob,

Pasted the last code wrong. The code I tried was: -

Public Class Form1

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim cbo As ComboBox = CType(e.Control, ComboBox)
        cbo.Items.Clear()

        Dim rowIndex As Integer = Me.DataGridView1.CurrentRow.Index

        ' Set the items for the current row.
        For index As Integer = 1 To 5
            Dim text As String = (rowIndex + 1) & "." & index
            cbo.Items.Add(text)
        Next

        ' Highlight the current text from the cell in the ComboBox.
        Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRow.Cells(Me.Column1.Index)

        cbo.SelectedIndex = cbo.FindString(cell.Value)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Add("Test")
    End Sub
End Class

Best Regards
Lee
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19575841
I set up a test DataGridView with only 1 combobox column, and used the first block of sample code in the Form_Load event.

Bob
0
 

Author Comment

by:ljhodgett
ID: 19580861
Hi Bob,

Can you please paste the code as I can get an idea as I'm struggling a bit.

Many Thanks
Lee
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19581255
Lee,
I pasted the code above.  The only thing is that I didn't include the .designer, but it is only a simple DataGridView with 1 combobox column, and AutoGenerateColumns = false.

Bob
0
 

Author Comment

by:ljhodgett
ID: 19581313
Hi Bob,

I've got it now thank you. When you said block one I thought you were referring to the first procedure in the last thread. I realise now you meant the first thread as block. It's working now so I can start finding out how to use the datagridview in it's entirety.

Many Thanks
Lee
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

809 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