Results 1 to 11 of 11

Thread: CONNECT RadioButton TO DATABASE

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    CONNECT RadioButton TO DATABASE

    Hi,
    i have a form with a bunch of textbox's that are connected to a ms access data source, so i can update, edit and delete records.
    I also have 4 radio buttons.
    What i'm trying to acheive is that when a row is selected in the datagridview, the textboxes show the values in them (id, name, number, etc.),which works perfectly, and the radio buttons should show the value of the "grade" column of the cell that is selected.

    So basicaly when you click a record, if the grade is 5 i want the radiobutton5 to be selected. And if i click radiobutton6 it should update the field to 6.

    I hope i am being clear enough for you to understand me.
    Thanks in advance!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: CONNECT RadioButton TO DATABASE

    You will need to evaluate the value of the "grade" field and set checked property of the appropriate radio button manually.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    Re: CONNECT RadioButton TO DATABASE

    thanks for replying,
    can you please explain further to what you mean (maybe with an example) because i'm a noob.
    Thanks!

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: CONNECT RadioButton TO DATABASE

    Something like this:
    Code:
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
            Dim RBs() As RadioButton = {Me.RadioButton1, Me.RadioButton2, Me.RadioButton3, Me.RadioButton4, Me.RadioButton5, Me.RadioButton6}
            Dim grade As Integer = CInt(Me.DataGridView1.CurrentRow.Cells("Grade").Value)
            If grade > 0 AndAlso grade <= RBs.Length Then
                RBs(grade - 1).Checked = True
            End If
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    Re: CONNECT RadioButton TO DATABASE

    Thanks for the reply!
    I tried sticking that into my code and it doesnt check any radio button. I also noticed that i get this error when i try to add a new record: "Conversion from type 'DBNull' to type 'String' is not valid."

    This is how i entered the code:

    Code:
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Datalist.SelectionChanged
            Dim RBs() As RadioButton = {Me.RadioButton4, Me.RadioButton5, Me.RadioButton6, Me.RadioButton7}
            Dim grade As Integer = CInt (Me.Datalist.CurrentRow.Cells(5).Value)
            If grade > 0 AndAlso grade <= RBs.Length Then
                RBs(grade - 1).Checked = True
            End If
        End Sub
    Thanks again for your help!
    Last edited by vbginner; Jan 11th, 2011 at 08:52 PM. Reason: mistake

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: CONNECT RadioButton TO DATABASE

    What are all the possible grades? Are they 4, 5, 6 and 7?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    Re: CONNECT RadioButton TO DATABASE

    yes.
    thanks!

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: CONNECT RadioButton TO DATABASE

    Then try this:
    Code:
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Datalist.SelectionChanged
            Dim RBs() As RadioButton = {Me.RadioButton4, Me.RadioButton5, Me.RadioButton6, Me.RadioButton7}
            Dim value as Object = Me.Datalist.CurrentRow.Cells(5).Value
            Dim grade As Integer  = 0
            If value IsNot Nothing AndAlso Not IsDBNull(value) Then
                grade = Cint(value)
                Dim index As Integer = grade - 4
                 If index >= 0 AndAlso index < RBs.Length Then
                      RBs(index).Checked = True
                End If
            End If
        End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    Re: CONNECT RadioButton TO DATABASE

    I GET AN ERROR AT
    Code:
    grade = CInt(value)
    "Conversion from string "HJ" to type 'Integer' is not valid." (WHICH IS THE VALUE OF COLUMN 2)

    THANKS!
    Last edited by vbginner; Jan 12th, 2011 at 03:26 PM.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    Re: CONNECT RadioButton TO DATABASE

    I just added some more records to the database, and realized that the code is getting the value of column #2 (which is a data column) instead of column #5.
    How can i change that?
    Thanks!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    78

    Resolved Re: CONNECT RadioButton TO DATABASE

    FIGURED IT OUT!
    Dim value As Object = Me.Datalist.CurrentRow.Cells("GRADEDataGridViewTextBoxColumn").Value

    THANKS ALOT!

Tags for this Thread

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