Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGridViewComboBoxCell default value

Posted on 2011-03-15
10
Medium Priority
?
2,084 Views
Last Modified: 2012-05-11
I have a datagridview with a combobox cell that is created at runtime using the following method

(The combobox is in cell 5 of the datagridview)

 Dim cb As New DataGridViewComboBoxCell
        cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing

        Dim sb As New System.Text.StringBuilder
        Dim dt As DataTable
        'this will creat a combo box with the active breeds and the breed of this PID (in case it is inactive)
        sb.Append('sql code here')

        dt = DataFuncs.ReturnDataTable(sb.ToString)
        With cb
            .DataSource = dt
            .ValueMember = "Breed_ID" 'dt.Columns(0).ColumnName
            .DisplayMember = "descr" 'dt.Columns(1).ColumnName
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
        End With

        Try
            dgPatients.Rows(ROWNUM).Cells(5) = cb
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, "ERROR", MessageBoxButtons.OK)
        End Try
        sb = Nothing

This fires when the user leaves cell 4, so that the combobox can be repopulated with data that is filtered based on the contents of cell 4.

This works fine. however, how can I set the combobox to a specific value after if populates?  I've tried setting the .value property but that doesn't work.
0
Comment
Question by:vollmesg
[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
10 Comments
 
LVL 10

Expert Comment

by:Mez4343
ID: 35141835
Did you try setting the Value property to an Item within the combobox?

  Try
         dgPatients.Rows(ROWNUM).Cells(5) = cb
         cb.Value = cb.Items(0)
  Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, "ERROR", MessageBoxButtons.OK
  End Try


0
 

Author Comment

by:vollmesg
ID: 35141899
That throws this exception:

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143420
...."(The combobox is in cell 5 of the datagridview)"....
just a single cell?or you mean Column with ColumnIndex 5?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vollmesg
ID: 35148320
the combobox is in column 5.  The datasource is a 2 column datatable.
I replace the combox in the column 5 of the active row when the user moves out of column 4 using the code displayed at the top of the post.

Hope that makes sense.
0
 
LVL 7

Expert Comment

by:foobarr
ID: 35150763
before you bind the combobox with the datasource, try adding the dropdown item there
0
 

Author Comment

by:vollmesg
ID: 35150830
I tried setting the .value of the combobox before I bound it, but not luck.  It is still blank.

Here is the current code

Private Sub CreateBreedBox(ByVal ROWNUM As Integer)
        Dim cb As New DataGridViewComboBoxCell
        Dim sCell As String = dgPatients.Rows(ROWNUM).Cells(5).Value
        cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing

        Dim sb As New System.Text.StringBuilder
        Dim dt As DataTable
        'this will creat a combo box with the active breeds and the breed of this PID (in case it is inactive)
        sb.Append(" SELECT Breed_id,descr FROM  v_EV_Species_breed  where (species_id='@ID') and (active='Y') union ")
        sb.Append(" Select Breed_ID ,breed from v_EV_PatientInfo where (ACCID=@A) and (species_ID='@ID') order by descr")
        sb.Replace("@A", gACCID.ToString)
        sb.Replace("@ID", dgPatients.Rows(ROWNUM).Cells(4).Value.ToString)

        dt = DataFuncs.ReturnDataTable(sb.ToString)
        With cb
           .Value = sCell
            .DataSource = dt
            .ValueMember = "Breed_ID" 'dt.Columns(0).ColumnName
            .DisplayMember = "descr" 'dt.Columns(1).ColumnName
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
        End With

        Try
            dgPatients.Rows(ROWNUM).Cells(5) = cb

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, "ERROR", MessageBoxButtons.OK)
        End Try
        sb = Nothing


    End Sub
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35150933
Hi...You may use the defaultValues needed event..
(I have read lot of times your question ...i think i did not understand well....)
Insdide the event use:
e.Row.Cells(5).value=What ever Value

Open in new window



or just try to use FormattedValue instead...
0
 

Author Comment

by:vollmesg
ID: 35150976
tried that.
here is the code that calls the function listed in the previous post.
I've bolded the lines that call the function

  Private Sub dgPatients_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgPatients.CellLeave

   

        'these lines of code commit the current cell so that it can
        'be read to create the next combobox
        Try
            dgPatients.CommitEdit(DataGridViewDataErrorContexts.Commit)
            dgPatients.EndEdit()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        '0:PatientID, 1:PID, 2:PATIENT_NAME, 3: PremID, 4:SPECIES, 5:BREED, 6:Sex, 7:Age, 8:Age_UM
        Select Case e.ColumnIndex

            Case 8
                If gIsReturn = False Then
                    Me.cbBillTo.Focus()
                    Me.dgPatients.StandardTab = False
                End If

            Case 4
               Dim sTmp As String = dgPatients.Rows(e.RowIndex).Cells(5).Value
                CreateBreedBox(e.RowIndex)
                dgPatients.Rows(e.RowIndex).Cells(5).Value = sTmp

            Case Else



        End Select
    End Sub

Private Sub CreateBreedBox(ByVal ROWNUM As Integer)
        Dim cb As New DataGridViewComboBoxCell
        Dim sCell As String = dgPatients.Rows(ROWNUM).Cells(5).Value
        cb.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing

        Dim sb As New System.Text.StringBuilder
        Dim dt As DataTable
        'this will creat a combo box with the active breeds and the breed of this PID (in case it is inactive)
        sb.Append(" SELECT Breed_id,descr FROM  v_EV_Species_breed  where (species_id='@ID') and (active='Y') union ")
        sb.Append(" Select Breed_ID ,breed from v_EV_PatientInfo where (ACCID=@A) and (species_ID='@ID') order by descr")
        sb.Replace("@A", gACCID.ToString)
        sb.Replace("@ID", dgPatients.Rows(ROWNUM).Cells(4).Value.ToString)

        dt = DataFuncs.ReturnDataTable(sb.ToString)
        With cb
           .Value = sCell
            .DataSource = dt
            .ValueMember = "Breed_ID" 'dt.Columns(0).ColumnName
            .DisplayMember = "descr" 'dt.Columns(1).ColumnName
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
        End With

        Try
            dgPatients.Rows(ROWNUM).Cells(5) = cb

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, "ERROR", MessageBoxButtons.OK)
        End Try
        sb = Nothing


    End Sub
0
 

Accepted Solution

by:
vollmesg earned 0 total points
ID: 35159493
Well, I ended up solving (sort of) then problem by doing this

 Private Sub dgPatients_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgPatients.CellLeave

     ..deleted code to increae readability....

      Select Case e.ColumnIndex

            Case 4
               _HOLDER = dgPatients.Rows(e.RowIndex).Cells(5).Value                CreateBreedBox(e.RowIndex)


            Case 5
               If dgPatients.Rows(e.RowIndex).Cells(5).Value = "" Then
                    If e.RowIndex = 0 Then
                        dgPatients.Rows(e.RowIndex).Cells(5).Value = _HOLDER
                    Else
                        dgPatients.Rows(e.RowIndex).Cells(5).Value = dgPatients.Rows(e.RowIndex - 1).Cells(5).Value
                    End If
                End If
           
              _HOLDER = ""
            Case Else
        End Select
    End Sub

On the cellleave property, If the user was leaving cell 4, I stored the current value of cell 5 in a form level variable.  When they left cel 5, if the value was blank, I set the value to the stored variable.
A bit of a kludge, but it works.
0
 

Author Closing Comment

by:vollmesg
ID: 35187441
No one else came up with a better solution.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 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