none
Adding a Radio button to a datagrid RRS feed

  • Question

  • I want to add a Radio button to my datagrid, but I am unsure how to do it.  Some od the examples I have seen add it to the form load, but I filter the view by using 2 combo boxes.

    I assume that I would have to add them to the BtnSearch sub, but most of the examples I have seen add them to a Datagridview and not a datatable as I have.

    Once the chosen item is selected I want to use this choice along with some other controls to print a document using FPDF .net.

    Any help woul be appreciated.

    Here is the code for the form:

    Public Class frmSecond
        Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
        Public Property dt As Object
    
        Private Sub frmSecond_Load_1(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Sub_EventTableAdapter2.Fill(Me.Verses_FindDataSet21.Sub_Event)
            Me.Event_TypeTableAdapter1.Fill(Me.Verses_FindDataSet11.Event_Type)
            Me.VerseTableAdapter1.Fill(Me.Verses_FindDataSet2.Verse)
            DataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            Me.DataGridView2.DefaultCellStyle.WrapMode = DataGridViewTriState.True
            Me.TopMost = True
            Me.WindowState = FormWindowState.Maximized
    
    
            Me.VerseTableAdapter1.Fill(Me.Verses_FindDataSet2.Verse)
            Dim sql As String = Nothing
            Dim ds As New DataSet()
            Dim stepInfo As String = String.Empty
            Try
                stepInfo = "Step: instatiate connection"
                Using connection As New SqlConnection(connectionString)
                    stepInfo = "Step: test open connection"
                    connection.Open()
                    stepInfo = "Step: load first data"
                    sql = "Select SID, EVENT from Event_Type"
                    Using adaptor As New SqlDataAdapter(sql, connection)
                        adaptor.Fill(ds, "Tab_Event_Type")
                    End Using
                    stepInfo = "Step: load second data"
                    sql = "Select BID, SUBEVENT from Sub_Event"
                    Using adaptor As New SqlDataAdapter(sql, connection)
                        adaptor.Fill(ds, "Tab_Sub_Event")
                    End Using
                End Using
                stepInfo = "Step: bind first combobox"
                ComboBox3.DataSource = ds.Tables("Tab_Event_Type")
                ComboBox3.ValueMember = "SID"
                ComboBox3.DisplayMember = "Event"
                stepInfo = "Step: bind second combobox"
                ComboBox4.DataSource = ds.Tables("Tab_Sub_Event")
                ComboBox4.ValueMember = "BID"
                ComboBox4.DisplayMember = "Event_Sub"
    
    
            Catch ex As Exception
                MessageBox.Show($"Error: {stepInfo}{vbNewLine}{ex.ToString}")
            End Try
        End Sub
        Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
            Dim sql As String = Nothing
            Dim sqlAdapter As SqlDataAdapter
            Dim cboVal1 As Integer
            Dim cboVal2 As Integer
            Dim dt As New DataTable()
            cboVal1 = CInt(ComboBox3.SelectedValue)
            cboVal2 = CInt(ComboBox4.SelectedValue)
    
            Using connection As New SqlConnection(connectionString)
                connection.Open()
    
                Dim selectStatement = $"Select Verse From Verse where Event= {cboVal1} and Event_Sub= {cboVal2}"
                sqlAdapter = New SqlDataAdapter(selectStatement, connection)
                dt = New DataTable()
                sqlAdapter.Fill(dt)
                DataGridView2.DataSource = dt
    
                Dim rowCount = 0
                rowCount = dt.Rows.Count
                TextBox1.Text = rowCount
            End Using
        End Sub
    
        Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
            Close()
        End Sub
    End Class


    TEH

    Friday, March 22, 2019 4:59 PM

All replies

  • Hello,

    Is this what you are looking for?

    How did I do this? Using three DataGridViewImageColumn columns. The DataGridView is loaded via a DataTable which contains, in this case the company name and the "fake" radio buttons are not in the DataTable.

    And yes we can get the results :-)


    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

    Friday, March 22, 2019 5:55 PM
    Moderator
  • Not quite!

    I filter the database with the combo boxes, then I want a radio button on each row of the filtered data table. If selected I want to store that choice and I will be adding some more controls, probabily combo boxes to other tables, then when all of the selections are made I will use the FPDF .Net class to print the resultant document.


    TEH

    Friday, March 22, 2019 7:08 PM
  • Not quite!

    I filter the database with the combo boxes, then I want a radio button on each row of the filtered data table. If selected I want to store that choice and I will be adding some more controls, probabily combo boxes to other tables, then when all of the selections are made I will use the FPDF .Net class to print the resultant document.


    TEH

    Okay, do you want one RadioButton per row in a DataGridView where a) each row can be check b) only one row can be checked? If (A) then a RadioButton is not the correct column type but instead you should use a CheckBox column.

    On a side note there have been attempts over the years (actually a very long time ago) to create a custom RadioButton group for a DataGridView but never seen a perfect one.

    Any ways is there more suitable? If not I have nothing else.


    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

    Friday, March 22, 2019 9:32 PM
    Moderator
  • Hello,

    Is this what you are looking for?

    How did I do this? Using three DataGridViewImageColumn columns. The DataGridView is loaded via a DataTable which contains, in this case the company name and the "fake" radio buttons are not in the DataTable.

    And yes we can get the results :-)


    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

    Karen, are you changing the image(s) of the DataGridViewImageCell in the row through an event such as ContentClick?

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, March 22, 2019 9:53 PM

  • Karen, are you changing the image(s) of the DataGridViewImageCell in the row through an event such as ContentClick?

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Greetings,

    I don't have the code in VB.NET for starters, was going to convert to VB.NET if the person wanted it. But if you like check out the C# version, it's not complicated.

    https://github.com/karenpayneoregon/DataGridViewWithRadioButtons/tree/master/DataGridViewFalseRadioButton


    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

    Friday, March 22, 2019 10:13 PM
    Moderator
  • I only want one item in the list selected, thats why I chose a radio button, which I understand that you can only make ONE selection.  As I explained, this choice I want to use, together with some other controls, which I have not added yet, will be used to format the FPDF .Net pdf writer.

    I hope you understand?


    TEH

    Saturday, March 23, 2019 6:31 PM
  • There are only two choices left which are from the following post.

    https://social.msdn.microsoft.com/Forums/windows/en-US/5f12ebd7-0dcf-44b7-a332-1ff781061b35/how-to-add-radiobutton-in-datagridview-in-window-form-in-c?forum=winforms

    If neither are suitable I have nothing else to offer.


    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

    Saturday, March 23, 2019 8:47 PM
    Moderator
  • Here is a working copy for the screenshot below


    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

    • Proposed as answer by Alex Li-MSFT Thursday, March 28, 2019 7:12 AM
    Sunday, March 24, 2019 5:13 PM
    Moderator