Solved

DataGridViewComboBoxCell default value

Posted on 2011-03-15
10
1,989 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
start a process from a service 3 33
SqlServer no dupes 25 38
run a stored procedure from vb.net 1 27
Need syntax help building hyperlink 2 27
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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