VB.Net Programming - How to Create a Master-Detail Windows Forms


Visual Basic .Net Programming - Creating a Master-Detail Windows Forms (SQL Server 2014 Database).

🎓 Master Visual Basic .NET and Access Database By Building the Point Of Sale System (POS).
ðŸ“ē Enroll Now: https://bit.ly/2WcbRhX

Visual C# Version: » https://goo.gl/UE0aP7

Part 1/2 : How to Create a Master-Detail Windows Form.

Part 2/2 : How to Open a Form from Another Form Using DataGridView (Link Column).

[Screenshot]

[Source Code]

1. Module : CodeAMinute

Imports System.Data
Imports System.Data.SqlClient

Module CodeAMinute
    Public str As String = GetConString()
    REM sorry
    Public con As New SqlConnection(str)
    Public cmd As SqlCommand
    Public da As SqlDataAdapter
    Public ds As DataSet
    Public bs As BindingSource
    Public qry As String

    Public Function GetConString() As String
        Return "Data Source=CodeAMinute\SQLEXPRESS;" &
               "Initial Catalog=Northwind;" &
               "Integrated Security=True;" &
               "MultipleActiveResultSets=True"
    End Function

    Public ProID As String 'Integer
    Public ProName As String
    Public QtyPerUnit As String
    Public ProPrice As Double
    Public UInStock As Integer
    Public RLevel As Integer
    Public IsDiscontinued As Boolean
    Public CatName As String
End Module

2. Form : Form1

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        con.Open()
        qry = "SELECT * FROM dbo.Categories"
        cmd = New SqlCommand(qry, con)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet()
        da.Fill(ds, "MyCats")

        ListBox1.DataSource = ds.Tables("MyCats")
        ListBox1.DisplayMember = "CategoryName"

        bs = New BindingSource
        bs.DataSource = ds.Tables("MyCats")

        txtCatID.DataBindings.Add("Text", bs, "CategoryID")
        txtCatName.DataBindings.Add("Text", bs, "CategoryName")
        txtDesc.DataBindings.Add("Text", bs, "Description")
        PictureBox1.DataBindings.Add("Image", bs, "Picture", True)
        ListBox1.SelectedIndex = 0

        Dim LinkColumn As New DataGridViewLinkColumn()
        With LinkColumn
            .DisplayIndex = 0
            .DefaultCellStyle.NullValue = "Select"
            .DefaultCellStyle.Alignment =
             DataGridViewContentAlignment.MiddleCenter
            .Width = 50
            .ActiveLinkColor = Color.White
            .LinkBehavior = LinkBehavior.HoverUnderline
            .LinkColor = Color.Crimson
            .TrackVisitedState = True
            .VisitedLinkColor = Color.YellowGreen
        End With
        '-- Sorry
        DataGridView1.Columns.Add(LinkColumn)
        ShowProducts()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If Not IsNothing(bs) Then
            bs.Position = ListBox1.SelectedIndex
        End If
        ShowProducts()
    End Sub

    Private Sub ShowProducts()
        If Not IsNothing(ds.Tables("MyProducts")) Then
            ds.Tables("MyProducts").Clear()
        End If
        qry = "SELECT * "
        qry &= "FROM Products "
        qry &= "WHERE CategoryID = "
        qry &= "'" & txtCatID.Text & "'"

        cmd.CommandText = qry
        da.SelectCommand = cmd
        da.Fill(ds, "MyProducts")
        REM Sorry for a little mistake ^_^
        REM Thank you for watching
        REM Please Like Comment and Subsribe
        REM Thanks once again.
        DataGridView1.DataSource = ds.Tables("MyProducts")

        '-- Product ID
        DataGridView1.Columns(0).DefaultCellStyle.Alignment =
        DataGridViewContentAlignment.MiddleCenter
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        con.Close()
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim dgv As DataGridView
        dgv = DataGridView1
        If e.ColumnIndex = 10 Then 'Link Column
            ProID = CStr(dgv.CurrentRow.Cells(0).Value)
            ProName = CStr(dgv.CurrentRow.Cells(1).Value)
            QtyPerUnit = CStr(dgv.CurrentRow.Cells(4).Value)
            ProPrice = CDbl(dgv.CurrentRow.Cells(5).Value)
            UInStock = CInt(dgv.CurrentRow.Cells(6).Value) 'Unit In Stock
            RLevel = CInt(dgv.CurrentRow.Cells(8).Value) 'Reorder Level
            IsDiscontinued = CBool(dgv.CurrentRow.Cells(9).Value)
            CatName = Me.txtCatName.Text.ToString
            '- Open the frmProducts Form
            frmProducts.ShowDialog()
        End If
    End Sub
End Class

3. Form : frmProducts

Public Class frmProducts
    Private Sub frmProducts_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.txtProID.Text = ProID.ToString
        Me.txtProName.Text = ProName.ToString
        Me.txtQtyPerUnit.Text = QtyPerUnit.ToString
        Me.txtPrice.Text = ProPrice.ToString("$#,##0.00")
        Me.txtUInStock.Text = UInStock.ToString
        Me.txtRLevel.Text = RLevel.ToString
        If (IsDiscontinued) Then
            Me.chkDiscontinued.Checked = True
        Else
            Me.chkDiscontinued.Checked = False
        End If
        Me.txtCatName.Text = CatName.ToString

    End Sub

    Private Sub frmProducts_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Form1.Show()
    End Sub
End Class

★✩★ Follow me on ★✩★

Twitter

Facebook

Google+

YouTube Channel

Thank you very much.
āļ‚āļ­āļšāļ„ุāļ“āļ„āļĢัāļš.

Comments

  1. This blog gives very important info about .Net Thanks for sharing
    Dot Net Online Training Hyderabad

    ReplyDelete
    Replies
    1. You are welcome. I'm glad to here that. Thanks so much.

      Delete

Post a Comment