ARTICLE

How to Bind ComboBox,RadioButton and DataGridView in DataBase

Posted by Munesh Sharma Articles | ADO.NET in VB.NET April 11, 2011
In this article we will learn, who to bind the multiple DataControls.
 
Reader Level:

ComboBox:

ComboBox is a combination of a TextBox and a ListBox. The ComboBox displays an editing field (TextBox) combined with a ListBox allowing us to select from the list or to enter new text. ComboBox displays data in a drop-down style format. The ComboBox class is derived from the ListBox class.

RadioButton:

RadioButtons are similar to CheckBoxes but RadioButtons are displayed as rounded instead of boxed as with a checkbox. Like CheckBoxes, RadioButtons are used to select and deselect options but they allow us to choose from mutually exclusive options. The RadioButton control is based on the ButtonBase class which is based on the Control class. A major difference between CheckBoxes and RadioButtons is, RadioButtons are mostly used together in a group.

There are Various steps of Binding  Controls:

  • Step 1 :visual Studio>project>New>Window Application.

  • Step 2:Design Window form.

    Design-win.gif

  • Step 3: Design Data Base.

    Design-Database.gif

  • Step 4:Write connection String.

    Imports System.Data.OleDb
    Module
    Module1
        Public constring As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\my_db7.accdb")

    End
    Module
     

  • Write Form1 Coding:

    Imports System.Data.OleDb  
    Public
    Class Form1
        Dim Ds As DataSet
        Dim Ds1 As DataSet
        Dim Ds2 As DataSet
        Dim Da As OleDbDataAdapter
        Dim cmd As OleDbCommand
        Dim Sql As String
        Dim Sql1 As String
        Dim Da1 As OleDbDataAdapter 
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Try
                If RadioButton1.Checked Then
                    constring.Open()  
                    Ds = New DataSet 
                    Sql1 = "select * from info where name = '" + ComboBox1.SelectedItem + "'".ToString()  
                    Da1 = New OleDbDataAdapter(Sql1, constring) 
                    Da1.Fill(Ds, "info")
                    DataGridView1.DataSource = Ds.Tables(0)
                    constring.Close()
                ElseIf RadioButton2.Checked Then
                    constring.Open()  
                    Ds = New DataSet 
                    Sql1 = "select * from info1 where name = '" + ComboBox1.SelectedItem + "'".ToString()  
                    Da1 = New OleDbDataAdapter(Sql1, constring) 
                    Da1.Fill(Ds, "info")
                    DataGridView1.DataSource = Ds.Tables(0)
                    constring.Close()   
                End If
     
            Catch ex As Exception
                MessageBox.Show(ex.Message)   
            End Try
     
        End Sub
     
        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged 
            Try
                Ds = New DataSet
                Dim i As Integer
                If RadioButton1.Checked = True Then
                    constring.Open() 
                    Sql = "select name from Companyp "
                    Da = New OleDbDataAdapter(Sql, constring)
                    Da.Fill(Ds, "companyp")
                    For i = 0 To Ds.Tables(0).Rows.Count - 1
                        ComboBox1.Items.Add(Ds.Tables("companyp").Rows(i)("name")) 
                    Next
     
                End If
                constring.Close()  
            Catch ex As Exception
                MessageBox.Show(ex.Message)  
            End Try
     
        End Sub
     
        Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged       
            Try
                Ds1 = New DataSet
                Dim i As Integer
                If RadioButton2.Checked = True Then
                    constring.Open() 
                    Sql = "select name from Localp "
                    Da = New OleDbDataAdapter(Sql, constring)
                    Da.Fill(Ds1, "Localp")
                    For i = 0 To Ds1.Tables(0).Rows.Count - 1
                        ComboBox1.Items.Add(Ds1.Tables("Localp").Rows(i)("name")) 
                    Next
     
                End If
                constring.Close()  
            Catch ex As Exception
                MessageBox.Show(ex.Message)  
            End Try
        
        End Sub
     
        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e AsSystem.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick 
        End Sub

    End
    Class
     

  • Output:

    Output-b.gif

    Output-b1.gif

    Outputb2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor