Results 1 to 3 of 3

Thread: DataGridViewComboBoxColumn question

Threaded View

  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.

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