Monday, 23 June 2014

Set Filter on datagridview in vb.net

 
 Dim filterlst As New List(Of String)
    Dim filterval As New List(Of String)
    Private Sub gridCollegedetails_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridCollegedetails.CellContentDoubleClick
        If e.RowIndex < 0 Then Exit Sub
        If e.ColumnIndex < 0 Then Exit Sub
        Me.Cursor = Cursors.WaitCursor
        gridCollegedetails.EnableHeadersVisualStyles = False
        If filterlst.Contains(e.ColumnIndex) = False Then
            If filterlst.Count > 0 Then gridCollegedetails.Columns(CInt(filterlst.Item(filterlst.Count - 1))).HeaderCell.Style.ForeColor = Color.Blue
            filterlst.Add(e.ColumnIndex)
            filterval.Add(Trim(UCase(gridCollegedetails.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)))
            gridCollegedetails.Columns(e.ColumnIndex).HeaderCell.Style.ForeColor = Color.Green
            For i As Integer = 0 To gridCollegedetails.Rows.Count - 1
                If gridCollegedetails.Rows(i).Visible = True Then
                    If Trim(UCase(gridCollegedetails.Rows(i).Cells(e.ColumnIndex).Value)) <> Trim(UCase(gridCollegedetails.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) And i <> e.RowIndex Then
                        gridCollegedetails.Rows(i).Visible = False
                    Else
                        gridCollegedetails.Rows(i).Visible = True
                    End If
                End If
            Next
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub gridCollegedetails_ColumnHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gridCollegedetails.ColumnHeaderMouseClick
        Try
            If gridCollegedetails.Columns(e.ColumnIndex).HeaderCell.Style.ForeColor <> Color.Green Then Exit Sub
            Me.Cursor = Cursors.WaitCursor
            If e.ColumnIndex = CInt(filterlst.Item(filterlst.Count - 1)) Then
                Dim penindex As Integer
                If filterlst.Count = 1 Then
                    penindex = 1
                    For i As Integer = 0 To gridCollegedetails.Rows.Count - 1
                        If gridCollegedetails.Rows(i).Visible = False Then
                            gridCollegedetails.Rows(i).Visible = True
                        End If
                    Next
                    filterval.Remove(filterval.Item(filterlst.Count - 1))
                    filterlst.Remove(filterlst.Item(filterlst.Count - 1))
                    gridCollegedetails.Columns(e.ColumnIndex).HeaderCell.Style.ForeColor = gridCollegedetails.ColumnHeadersDefaultCellStyle.ForeColor
                    Me.Cursor = Cursors.Default
                    Exit Sub
                Else
                    penindex = CInt(filterlst.Item(filterlst.Count - 2))
                End If

                For i As Integer = 0 To gridCollegedetails.Rows.Count - 1
                    If gridCollegedetails.Rows(i).Visible = False Then

                        If Trim(UCase(gridCollegedetails.Rows(i).Cells(penindex).Value)) = filterval.Item(filterlst.Count - 2) Then
                            gridCollegedetails.Rows(i).Visible = True
                        End If
                    End If
                Next
                filterval.Remove(filterval.Item(filterlst.Count - 1))
                filterlst.Remove(filterlst.Item(filterlst.Count - 1))
                gridCollegedetails.Columns(e.ColumnIndex).HeaderCell.Style.ForeColor = gridCollegedetails.ColumnHeadersDefaultCellStyle.ForeColor
                If filterlst.Count > 0 Then gridCollegedetails.Columns(CInt(filterlst.Item(filterlst.Count - 1))).HeaderCell.Style.ForeColor = Color.Green
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

Alphabetical sort function

 Function alphabetsorting(ByVal str As String) As String
        alphabetsorting = ""
        Dim arrayLetters As Array = str.ToCharArray()
        Array.Sort(arrayLetters)
        For i As Integer = 0 To arrayLetters.Length - 1
            If alphabetsorting = "" Then
                alphabetsorting = arrayLetters(i)
            Else
                alphabetsorting = alphabetsorting & arrayLetters(i)
            End If
        Next
        Return alphabetsorting
    End Function

Remove duplicate from string function

Function rduplicatestring(ByVal str As String) As String
        rduplicatestring = ""
        Dim myarr() As String = Split(str, Space(1))
        myarr = myarr.Distinct().ToArray
        myarr = myarr.DefaultIfEmpty().ToArray
        rduplicatestring = Replace(Join(myarr, Space(1)), Space(2), Space(1))
        Return rduplicatestring
    End Function