Solved

DataGridViewComboBoxCell default value

Posted on 2011-03-15
10
2,014 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
Industry Leaders: 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

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 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