Wednesday, October 10, 2012

Sorting of ListView in VB.NET

Private lvwColumnSorter As Utils.ListViewColumnSorter


Private Sub lstView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstView.ColumnClick
        Try
            If (e.Column = lvwColumnSorter.SortColumn) Then
                ' Reverse the current sort direction for this column.
                If (lvwColumnSorter.Order = SortOrder.Ascending) Then
                    lvwColumnSorter.Order = SortOrder.Descending
                Else
                    lvwColumnSorter.Order = SortOrder.Ascending
                End If
            Else
                ' Set the column number that is to be sorted; default to ascending.
                lvwColumnSorter.SortColumn = e.Column
                lvwColumnSorter.Order = SortOrder.Ascending
            End If

            ' Perform the sort with these new sort options.
            lstView.Sort()
        Catch ex As Exception
            Utils.LogHandler.HandleError(ex)
        End Try
    End Sub



 Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        lvwColumnSorter = New Utils.ListViewColumnSorter()
        lstView.ListViewItemSorter = lvwColumnSorter
    End Sub

Comobox binding in VB.NET using List(Of T) from database


Data Layer

Public Function GetAll() As List(Of SMSCLayer.PromoterType)
        Dim result As New List(Of SMSCLayer.PromoterType)
        Dim params As New List(Of DbParameter)

        Dim dt As DataTable = GetSQLTable("select * from promotertype")

        For Each dr As DataRow In dt.Rows
            result.Add(New SMSCLayer.PromoterType() With {.Id = ToInteger(dr("Id")),
                                                          .Name = ToString(dr("Name"))})
        Next

        Return result
    End Function

Business Layer


Public Shared Function GetAll() As List(Of SMSCLayer.PromoterType)
        Dim d As New SMSDLayer.PromoterType
        Return d.GetAll()
    End Function

Communication Layer


Public Class PromoterType
    Public Property Id As Long = 0
    Public Property Name As String = String.Empty
End Class

Presentation Layer


 Private Sub FillPromoterTypeCombo()
        Dim result As New List(Of SMSCLayer.PromoterType)
        result = SMSBLayer.PromoterType.GetAll()
        result.Insert(0, New SMSCLayer.PromoterType() With {.Id = 0, .Name = "Select"})
        cmbPromoterType.DataSource = result
        cmbPromoterType.DisplayMember = "Name"
        cmbPromoterType.ValueMember = "Id"
        cmbPromoterType.SelectedIndex = 0
    End Sub




Combobox binding in VB.NET using List(Of T)


Public Class Salutation

    Sub New()
        ' TODO: Complete member initialization
    End Sub

    Public Function GetAll() As List(Of String)
        Dim Salutations As New List(Of String)
        Salutations.Add("Select")
        Salutations.Add("Mr")
        Salutations.Add("Mrs")
        Salutations.Add("Miss")
        Salutations.Add("Ms")
        Salutations.Add("Dr")

        Return Salutations
    End Function

    Public Sub New(ByVal name As String, ByVal id As Integer)
        Me.Name = name
        Me.Id = id
    End Sub
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set(ByVal value As Integer)
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
End Class


Private salutation As New List(Of SMSCLayer.Salutation)


    Public Sub FillSalutations()
        Dim Salutations As New List(Of String)
        Dim d As New SMSCLayer.Salutation
        Salutations = d.GetAll()
        For i As Integer = 0 To Salutations.Count - 1
            salutation.Add(New SMSCLayer.Salutation(Salutations(i).ToString(), i))
        Next

        cmbSalutation.DataSource = salutation
        cmbSalutation.DisplayMember = "Name"
        cmbSalutation.ValueMember = "Id"
        cmbSalutation.SelectedIndex = 0
        cmbSalutation.Focus()

    End Sub

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...