none
Storing radio button selection in data table? RRS feed

  • Question

  • Hi,

    I have more than 5 radio buttons and i want to store the selection in data table and if the user selects the row in datagrid view , corresponding radio button should be selected. Any idea how to program it? 

    Thanks

    Monday, July 22, 2019 9:35 AM

Answers

  • Hi,

    like this?

    Public Class Form1
        Dim bind As New BindingSource
        Dim dt As New DataTable
        Dim radiobtn
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            radiobtn = New RadioButton() {RadioButton1, RadioButton2, RadioButton3}
            dt.Columns.Add("column1", GetType(String))
            dt.Columns.Add("column2", GetType(String))
            dt.Columns.Add("column3", GetType(Boolean))
            dt.Rows.Add(New Object() {"1", "2", False})
            dt.Rows.Add(New Object() {"3", "4", False})
            dt.Rows.Add(New Object() {"5", "6", False})
            bind.DataSource = dt
            DataGridView1.DataSource = bind
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            bind.Position = 0
            dt.Rows(0)(2) = IIf(RadioButton1.Checked, True, False)
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            bind.Position = 1
            dt.Rows(1)(2) = IIf(RadioButton2.Checked, True, False)
    
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            bind.Position = 2
            dt.Rows(2)(2) = IIf(RadioButton3.Checked, True, False)
        End Sub
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If e.ColumnIndex = 2 Then
                CType(radiobtn(e.RowIndex), RadioButton).Checked = True
            End If
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shan1986 Wednesday, July 24, 2019 8:09 AM
    Tuesday, July 23, 2019 9:30 AM

All replies

  • Hi,

    code:

       Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    
            RadioButton1.Checked = IIf(DataGridView1.Rows(0).Selected = True, True, False)
    
            RadioButton2.Checked = IIf(DataGridView1.Rows(1).Selected = True, True, False)
            RadioButton3.Checked = IIf(DataGridView1.Rows(2).Selected = True, True, False)
    
            RadioButton4.Checked = IIf(DataGridView1.Rows(3).Selected = True, True, False)
            RadioButton5.Checked = IIf(DataGridView1.Rows(4).Selected = True, True, False)
    
    
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 22, 2019 9:56 AM
  • For clarification, each time the row changes in the DataGridView do you want the RadioButton selected to reflect the same in the DataGridView or just one row in the DataGridView?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 22, 2019 10:49 AM
    Moderator
  • For clarification, each time the row changes in the DataGridView do you want the RadioButton selected to reflect the same in the DataGridView or just one row in the DataGridView?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, User can add more rows with different selection. so each row could contain specific radio button selection. 

    Your code shows data grid view to radio button selection. now how do i add selected radio button to data table? can we bind radio button group to one data table column?

    Monday, July 22, 2019 11:08 AM
  • For clarification, each time the row changes in the DataGridView do you want the RadioButton selected to reflect the same in the DataGridView or just one row in the DataGridView?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, User can add more rows with different selection. so each row could contain specific radio button selection. 

    Your code shows data grid view to radio button selection. now how do i add selected radio button to data table? can we bind radio button group to one data table column?

    It was Alex who posted code not me.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 22, 2019 2:27 PM
    Moderator
  • Hi,

    like this?

    Public Class Form1
        Dim bind As New BindingSource
        Dim dt As New DataTable
        Dim radiobtn
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            radiobtn = New RadioButton() {RadioButton1, RadioButton2, RadioButton3}
            dt.Columns.Add("column1", GetType(String))
            dt.Columns.Add("column2", GetType(String))
            dt.Columns.Add("column3", GetType(Boolean))
            dt.Rows.Add(New Object() {"1", "2", False})
            dt.Rows.Add(New Object() {"3", "4", False})
            dt.Rows.Add(New Object() {"5", "6", False})
            bind.DataSource = dt
            DataGridView1.DataSource = bind
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            bind.Position = 0
            dt.Rows(0)(2) = IIf(RadioButton1.Checked, True, False)
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            bind.Position = 1
            dt.Rows(1)(2) = IIf(RadioButton2.Checked, True, False)
    
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            bind.Position = 2
            dt.Rows(2)(2) = IIf(RadioButton3.Checked, True, False)
        End Sub
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If e.ColumnIndex = 2 Then
                CType(radiobtn(e.RowIndex), RadioButton).Checked = True
            End If
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shan1986 Wednesday, July 24, 2019 8:09 AM
    Tuesday, July 23, 2019 9:30 AM
  • Hi,

    like this?

    Public Class Form1
        Dim bind As New BindingSource
        Dim dt As New DataTable
        Dim radiobtn
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            radiobtn = New RadioButton() {RadioButton1, RadioButton2, RadioButton3}
            dt.Columns.Add("column1", GetType(String))
            dt.Columns.Add("column2", GetType(String))
            dt.Columns.Add("column3", GetType(Boolean))
            dt.Rows.Add(New Object() {"1", "2", False})
            dt.Rows.Add(New Object() {"3", "4", False})
            dt.Rows.Add(New Object() {"5", "6", False})
            bind.DataSource = dt
            DataGridView1.DataSource = bind
    
        End Sub
    
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
            bind.Position = 0
            dt.Rows(0)(2) = IIf(RadioButton1.Checked, True, False)
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
            bind.Position = 1
            dt.Rows(1)(2) = IIf(RadioButton2.Checked, True, False)
    
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
            bind.Position = 2
            dt.Rows(2)(2) = IIf(RadioButton3.Checked, True, False)
        End Sub
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            If e.ColumnIndex = 2 Then
                CType(radiobtn(e.RowIndex), RadioButton).Checked = True
            End If
        End Sub
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks Alex for the answer. Got the idea.
    Wednesday, July 24, 2019 8:09 AM