[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2112
  • Last Modified:

DataGridViewComboBoxCell default value

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
vollmesg
Asked:
vollmesg
1 Solution
 
Mez4343Commented:
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
 
vollmesgAuthor Commented:
That throws this exception:

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
0
 
John (Yiannis) ToutountzoglouCommented:
...."(The combobox is in cell 5 of the datagridview)"....
just a single cell?or you mean Column with ColumnIndex 5?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
vollmesgAuthor Commented:
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
 
foobarrCommented:
before you bind the combobox with the datasource, try adding the dropdown item there
0
 
vollmesgAuthor Commented:
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
 
John (Yiannis) ToutountzoglouCommented:
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
 
vollmesgAuthor Commented:
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
 
vollmesgAuthor Commented:
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
 
vollmesgAuthor Commented:
No one else came up with a better solution.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now