none usercontrol RRS feed

  • Question

  • Hi
    I want to pull data using sql but I want to do it using usercontrol How do I add data to form with usercontrol
    Saturday, January 4, 2020 12:13 PM

All replies

  • Hello,

    The purpose of a user control is for is for placing controls together that can be reused in multiple forms or multiple projects. What you are asking is incorrect, instead data would be pulled/read from a class and feed into a user control.

    Example, the following user control has a BindingNavigator and two TextBox controls. Data is read from a database table into a list where the list type represents data in the table (a DataTable would be overkill be could be used).

    We can have as many properties as needs that match up to columns in the database table

    Public Class Person
        Public Property Id() As Integer
        Public Property FirstName() As String
        Public Property LastName As String
        Public Property BirthDate() As DateTime
        Public ReadOnly Property Age() As Integer
                Return Convert.ToInt32(Date.UtcNow.Date.Year - BirthDate.Year)
            End Get
        End Property
        Public Overrides Function ToString() As String
            Return $"{FirstName} {LastName}"
        End Function
    End Class

    In the main form in this case data is feed to the user control via a method where People.List contains data from the database table.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
    End Class

    In the user control the list is setup, controls are data bound so that when pressing buttons in the BindingNavigator we can traverse records then there is a save button would takes the list, feeds it back to a class which in turn saves changes to the database.

    Public Class UserControl1
        Public PeopleBindingSource As New BindingSource
        Public Sub LoadData(ByVal people As List(Of Person))
            PeopleBindingSource.DataSource = people
            BindingNavigator1.BindingSource = PeopleBindingSource
            If FirstNameTextBox.DataBindings.Count > 0 Then
            End If
            FirstNameTextBox.DataBindings.Add("Text", PeopleBindingSource, "FirstName")
            LastNameTextBox.DataBindings.Add("Text", PeopleBindingSource, "LastName")
        End Sub
        ''' <summary>
        ''' Save all method
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) _
            Handles SaveToolStripButton.Click
            People.SaveAll(CType(PeopleBindingSource.DataSource, List(Of Person)))
        End Sub
    End Class

    The user control back color is different so you can see it on a form.

    I've not shown any reading from a database, the basic steps are to create a connection and command, iterate a data reader and populate the list (as per above) and to update use a command with an update query. Alternate is to use a DataAdapter.

    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.

    profile for Karen Payne on Stack Exchange

    Saturday, January 4, 2020 2:52 PM
  • I want to add usercontrole without using datagridview
    Saturday, January 4, 2020 2:57 PM
  • I want to add usercontrole without using datagridview

    for "I want to pull data using sql but I want to do it using usercontrol How do I add data to form with usercontrol"

    You can do it exactly the same as with a normal control. 


    Sunday, January 5, 2020 1:32 PM
  • Hi Hollowmana, 

    Thank you for posting here.

    UserControl provides an empty control that can be used to create other controls, so it cannot be used to pull data or add data to form.

    In order to make a test, I have some questions to confirm with you.

    Do you want to use controls in UserControl to extract data from database or add data to database?

    Could you provide more details about your question?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao

    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

    Monday, January 6, 2020 3:22 AM