Results 1 to 7 of 7

Thread: [RESOLVED] binding datagridview and (textbox, radiobutton) to the same table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    19

    Resolved [RESOLVED] binding datagridview and (textbox, radiobutton) to the same table

    Hi there,

    im currently binding a datagridview and some textboxes + 2 radiobutton to a table in my db. Everytime I click on a row in my datagrid, the values are perfectly showing also in the appropriate textboxes using databinding properties.

    My only issue is with the radiobutton option. I have a field in my table called "Transmission" - on my form, I have 2 radiobutton, the first is called "Automatic" and the second "Manual"

    I would like to see the "Automatic" radiobutton checked if the value in my table is Automatic or else. I look for the checked value in the IDE propreties, tag and also text and could not find a way to retrieve the value of my table into the appropriate radiobutton. Basically, if the field in my table has the "Automatic" value, then my radiobutton "Automatic" should be checked.

    Any idea how to fix this using databinding ?

    Thank you

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: binding datagridview and (textbox, radiobutton) to the same table

    Here is an example where a DataTable is loaded with mocked data but works the same with getting table data from a database.

    Name:  PM.png
Views: 1543
Size:  25.7 KB

    Code:
    Public Class Form1
        WithEvents bs As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim dt As DataTable = New DataTable With {.TableName = "MyTable"}
            dt.Columns.Add(New DataColumn With {.ColumnName = "Identifier",
                                                .DataType = GetType(Int32),
                                                .AutoIncrement = True, .AutoIncrementSeed = 1
                                               }
                                           )
            dt.Columns.Add(New DataColumn With {.ColumnName = "Descision", .DataType = GetType(Boolean)})
            dt.Columns.Add(New DataColumn With {.ColumnName = "SerialNumber", .DataType = GetType(String)})
    
            dt.Columns.Add(New DataColumn With {.ColumnName = "CK1", .DataType = GetType(Boolean)})
            dt.Columns.Add(New DataColumn With {.ColumnName = "CK2", .DataType = GetType(Boolean)})
            dt.Columns.Add(New DataColumn With {.ColumnName = "CK3", .DataType = GetType(Boolean)})
            dt.Columns.Add(New DataColumn With {.ColumnName = "CK4", .DataType = GetType(Boolean)})
    
            dt.Rows.Add(New Object() {Nothing, True, "A1", True, False, False, False})
            dt.Rows.Add(New Object() {Nothing, False, "B1", False, False, True, False})
            dt.Rows.Add(New Object() {Nothing, True, "C1", False, True, False, False})
            bs.DataSource = dt
    
            BindingNavigator1.BindingSource = bs
    
            CheckBox2.DataBindings.Add("Enabled", CheckBox1, "Enabled")
            CheckBox4.DataBindings.Add("Enabled", CheckBox3, "Enabled")
    
            CheckBox1.DataBindings.Add("Checked", bs, "CK1")
            CheckBox2.DataBindings.Add("Checked", bs, "CK2")
            CheckBox3.DataBindings.Add("Checked", bs, "CK3")
            CheckBox4.DataBindings.Add("Checked", bs, "CK4")
    
            GroupBox1.DataBindings.Add("Text", bs, "SerialNumber")
    
        End Sub
        ''' <summary>
        ''' Using the two Radio buttons check change event below we
        ''' will make appropriate changes to the underlying data of the 
        ''' BindingSource who's DataSource is a DataTable
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _
            Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    
            If CType(sender, Control).Name = "RadioButton1" Then
                CType(bs.Current, DataRowView).Row.SetField(Of Boolean)("Descision", True)
                CheckBox1.Enabled = True
                CheckBox3.Enabled = False
            Else
                CheckBox1.Enabled = False
                CheckBox3.Enabled = True
                CType(bs.Current, DataRowView).Row.SetField(Of Boolean)("Descision", False)
            End If
        End Sub
        ''' <summary>
        ''' Update Radio buttons checked state dependent on underlying field value
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub bs_PositionChanged(sender As Object, e As EventArgs) Handles bs.PositionChanged
            If CType(bs.Current, DataRowView).Row.Field(Of Boolean)("Descision") Then
                RadioButton1.Checked = True
            Else
                RadioButton2.Checked = True
            End If
        End Sub
    
    End Class

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: binding datagridview and (textbox, radiobutton) to the same table

    Quote Originally Posted by Mimosa777 View Post
    I would like to see the "Automatic" radiobutton checked if the value in my table is Automatic or else. I look for the checked value in the IDE propreties, tag and also text and could not find a way to retrieve the value of my table into the appropriate radiobutton. Basically, if the field in my table has the "Automatic" value, then my radiobutton "Automatic" should be checked.
    I am not certain whether the "Transmission" field is of type String or Boolean. It would make sense for it to be Boolean, but based on your description, I believe that it is a String. It looks like Kevin is treating the value as a Boolean.

    If it is a String, then you could accomplish the goal with something like this:
    Code:
    Public Class Form1
       Private dt As DataTable
       Private bs As BindingSource
       Private nav As BindingNavigator
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          dt = New DemoTable ' simulates loading data from db
          ' add 2 computed columns to the table to bind the radio buttons to
          dt.Columns.Add("IsAutomatic", GetType(Boolean), "[Transmission] = 'Automatic'")
          dt.Columns.Add("IsManual", GetType(Boolean), "Not [IsAutomatic]")
    
          bs = New BindingSource(dt, Nothing)
          nav = New BindingNavigator(bs)
          nav.Parent = Me
          dgv1.DataSource = bs
    
          ' set RadioButton bindings
          rbAutomatic.DataBindings.Add("Checked", bs, "IsAutomatic", True, DataSourceUpdateMode.OnPropertyChanged)
          rbManual.DataBindings.Add("Checked", bs, "IsManual", True, DataSourceUpdateMode.OnPropertyChanged)
       End Sub
    
       Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
          ' DataGridViewColumns are not generated until the control becomes visible
          ' so need to wait until the Form is shown to remove them if Autogenerated columns are used
          dgv1.Columns("IsAutomatic").Visible = False
          dgv1.Columns("IsManual").Visible = False
       End Sub
    
       ' Use the MouseDown event and not CheckChanged to avoid unnecessary event generation
       ' ,due to the bindings, to set the Transmission string value on the current datarow.
       ' The bindings will then update the buttons.
       Private Sub rbStandard_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rbManual.MouseDown
          If Not rbManual.Checked Then
             DirectCast(bs.Current, DataRowView).Row("Transmission") = "Manual"
             bs.CurrencyManager.Refresh()
          End If
       End Sub
    
       Private Sub rbAutomatic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rbAutomatic.MouseDown
          If Not rbAutomatic.Checked Then
             DirectCast(bs.Current, DataRowView).Row("Transmission") = "Automatic"
          End If
       End Sub
    End Class
    
    Public Class DemoTable
       Inherits DataTable
       Public Sub New()
          With Me.Columns
             .Add("Brand", GetType(String))
             .Add("Color", GetType(String))
             .Add("Transmission", GetType(String))
          End With
          With Me.Rows
             .Add("Ford", "Red", "Automatic")
             .Add("Chevy", "Red", "Automatic")
             .Add("Chrysler", "Red", "Standard")
          End With
       End Sub
    End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    19

    Re: binding datagridview and (textbox, radiobutton) to the same table

    thank you Kevin - that did helped me a lot

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    19

    Re: [RESOLVED] binding datagridview and (textbox, radiobutton) to the same table

    Hi TnTinMN,

    Indeed, transmission field is a string - I was going to change the field to Boolean to be able to work with Kevin solution,
    But I'm definitely trying what you just suggested and will keep you posted.
    I'd rather keep the field as string for other purposes.

    Thanks a lot for the help - I'm working on it with your code .

    Thank you

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: [RESOLVED] binding datagridview and (textbox, radiobutton) to the same table

    In regards to keeping the Boolean as a string, store the data as a Boolean and perhaps setup a DataColumn with an Expression as shown below. If the data was coming from a database you would add the DataColumn onto the DataTable populated.

    Name:  AM.png
Views: 1345
Size:  25.4 KB

    Add a label then use this code
    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim dt As DataTable = New DataTable With {.TableName = "MyTable"}
    
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "Identifier",
                .DataType = GetType(Int32),
                .AutoIncrement = True,
                .AutoIncrementSeed = 1
            }
        )
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "Descision",
                .DataType = GetType(Boolean)
            }
        )
    
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "SerialNumber",
                .DataType = GetType(String)
            }
        )
    
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "CK1",
                .DataType = GetType(Boolean)
            }
        )
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "CK2",
                .DataType = GetType(Boolean)
            }
        )
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "CK3",
                .DataType = GetType(Boolean)
            }
        )
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "CK4",
                .DataType = GetType(Boolean)
            }
        )
    
        dt.Columns.Add(
            New DataColumn With
            {
                .ColumnName = "DescisionAsString",
                .DataType = GetType(String),
                .Expression =
                "IIF(Descision,'Your string for True','Value for False')"
            }
        )
    
        dt.Rows.Add(New Object() {Nothing, True, "A1", True, False, False, False})
        dt.Rows.Add(New Object() {Nothing, False, "B1", False, False, True, False})
        dt.Rows.Add(New Object() {Nothing, True, "C1", False, True, False, False})
        bs.DataSource = dt
    
        BindingNavigator1.BindingSource = bs
    
        CheckBox2.DataBindings.Add("Enabled", CheckBox1, "Enabled")
        CheckBox4.DataBindings.Add("Enabled", CheckBox3, "Enabled")
    
        CheckBox1.DataBindings.Add("Checked", bs, "CK1")
        CheckBox2.DataBindings.Add("Checked", bs, "CK2")
        CheckBox3.DataBindings.Add("Checked", bs, "CK3")
        CheckBox4.DataBindings.Add("Checked", bs, "CK4")
    
        GroupBox1.DataBindings.Add("Text", bs, "SerialNumber")
        Label1.DataBindings.Add("Text", bs, "DescisionAsString")
    
    End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    19

    Re: [RESOLVED] binding datagridview and (textbox, radiobutton) to the same table

    Thank you Kevin - I also tested your method and it works great. cheers

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