Results 1 to 3 of 3

Thread: DataGridViewComboBoxColumn question

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    34

    DataGridViewComboBoxColumn question

    I have a datagridviewcomboboxcolumn that I am populating this way :
    Code:
    '---------------------------------------------------------------------
    ' If person is editing a record, their previous choice should always be the first 
    ' in the list
    '---------------------------------------------------------------------
    If intNameKey <> 0 Then	
        strSql = "select '1' as sort_seq, name_key, species from species " & _
                    "where name_key = " & intNameKey & " " & _
                    "  and upper(name_type) = 'FISH' " & _
                    "union " & _
                    "select '2' as sort_seq, name_key, species from species " & _
                    "where name_key <> " & intNameKey & " " & _
                    "  and upper(name_type) = 'FISH' " & _
                    "order by sort_seq, species"
    End If
    
    '---------------------------------------------------------------------
    ' If person is adding a record, show fish in alpha order.
    '---------------------------------------------------------------------
    If intNameKey = 0 Then	
        strSql = "select '1' as sort_seq, name_key, species from species " & _
                   "where upper(name_type) = 'FISH' order by species"
    End If
    
    objDA = New SqlDataAdapter(strSql, objConn)
    objDS = New DataSet
    objDA.Fill(objDS, "species")
    objConn.Close()
    
    With Me.cboSpecies     (defined as a datagridviewcomboboxcolumn)
        .DataSource = objDS.Tables("species")
        .DisplayMember = "species"
        .ValueMember = "name_key"
        .DataPropertyName = "name_key"
    End With
    strSpecies = objDS.Tables("species").Rows(0).Item(2).ToString
    I am doing the following code so that there is no blank in the first line of the combobox where the user is in edit mode. If I don't do this, there is a blank line, followed by the value previously chosen, followed by the rest of the table.
    Code:
    Private Sub dgvFish_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvFish.CellFormatting
    
        If e.ColumnIndex = Me.dgvFish.Columns(intSpeciesCol).Index And e.RowIndex >= 0 Then
            Dim d As DataGridViewComboBoxCell = Me.dgvFish.Rows(e.RowIndex).Cells(e.ColumnIndex)
            If e.Value = Nothing And d.Items.Count > 0 Then
                e.Value = strSpecies
            End If
        End If
    End Sub
    Loading the data works fine. My problem is that if the user doesn't make any changes and hits the <UPDATE> button, when I loop thru each line of the datagridview, I can't seem to get the value (name_key) behind the cboSpecies. Here is what I am using :
    Me.dgvFish.Rows(intRowIdx).Cells(3).Value to try to get the value. Any help would be greatly appreciated.
    Last edited by bfoley; Mar 16th, 2010 at 07:17 AM.

  2. #2
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    Re: DataGridViewComboBoxColumn question

    This code -- Me.dgvFish.Rows(intRowIdx).Cells(3).Value -- will try to return the value in the cell, which should be the combobox selected text that is displayed. You need to get the combobox's selected value. This isn't tested but stands to reason you should be able to get the value you are looking for similar to this:
    DirectCast(Me.dgvFish.Rows(intRowIdx).Cells(3),DataGridViewComboBoxCell).SelectedValue
    If someone has helped you, please make sure to rate them.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    34

    Re: DataGridViewComboBoxColumn question

    Unfortunately, .SelectedValue doesn't work. When I tried :
    Code:
    DirectCast(Me.dgvFish.Rows(intRowIdx).Cells(3),DataGridViewComboBoxCell).SelectedValue
    I get 'SelectedValue' is not a member of 'DataGridViewComboBoxCell'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width