Solved

DataGridViewComboBoxCell default value

Posted on 2011-03-15
10
1,938 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
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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now