Friday, December 2, 2011

Retaining Last Column Resized in Datagridview

Retaining Last Grid Column Resized in Datagridview 

Initially insert formname and its gridname in MyProject->Settings.Settings file.

Setting file :

1. Open Settings.Setting file
2. Insert name like gridName_formname
3. choose System.COllection.Specialized.StringCollection
4. choose scope. if u want to user specific then choose User else Application.
5. Paste this code as dummy in Value Column
    <?xml version="1.0" encoding="utf-16"?>
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <string>100~0</string>
    </ArrayOfString>



Writing Last Resized Grid Column in Form UnLoad :

In form Unload  Call this function as 

Set_User_Settings(me)

function :

Public Sub Set_User_Settings(ByVal Objfrm As Object)
        Dim ExitFlag As Boolean = False
        For Each Cnt As Object In Objfrm.Controls
            Set_GridControls(Objfrm, Cnt, ExitFlag)
        Next
End Sub


Private Sub Set_GridControls(ByVal Objfrm As Object, ByVal Cntrl As Object, ByRef canExit As Boolean)
        If canExit = True Then Exit Sub
        If Cntrl.HasChildren = True Then
            If TypeOf Cntrl Is DataGridView Then
                Dim _Key As String = Cntrl.name & "_" & Objfrm.name
                With Cntrl
                    Try
                        My.Settings.Item(_Key) = New System.Collections.Specialized.StringCollection()
                    Catch ex As Exception
                        canExit = True
                        Exit Sub
                    End Try
                    For i = 0 To Cntrl.Columns.Count - 1
                        Dim setting = .Columns(i).Width.ToString + "~" + .Columns(i).DisplayIndex.ToString
                        My.Settings.Item(_Key).Add(setting)
                    Next
                    My.Settings.Save()
                End With
            Else
                For Each chldcntrl In Cntrl.Controls
                    Set_GridControls(Objfrm, chldcntrl, canExit)
                Next
            End If
        End If
 End Sub


On Form Load, Assign last resized column :
call as Get_User_Settings(Me)

and its function is

Public Sub Get_User_Settings(ByVal Objfrm As Object)
        Dim ExitFlag As Boolean = False
        For Each Cnt As Object In Objfrm.Controls
            Get_GridControls(Objfrm, Cnt, ExitFlag)
        Next
        If ExitFlag = True Then Exit Sub
End Sub


Private Sub Get_GridControls(ByVal Objfrm As Object, ByVal Cntrl As Object, ByRef canExit As Boolean)
        If canExit = True Then Exit Sub
        If Cntrl.HasChildren = True Then
            If TypeOf Cntrl Is DataGridView Then
                Dim _Key As String = Cntrl.name & "_" & Objfrm.name
                With Cntrl
                    Try
                        If My.Settings.Item(_Key).Count > 0 Then
                            For i = 0 To My.Settings.Item(_Key).Count - 1
                                Dim setting = My.Settings.Item(_Key).Item(i).ToString
                                Dim settings() = setting.Split("~")
                                .Columns(i).Width = Int(settings(0))
                                .Columns(i).DisplayIndex = Int(settings(1))
                            Next
                        End If
                    Catch ex As Exception
                        canExit = True
                        Exit Sub
                    End Try
                End With
            Else
                For Each chldcntrl In Cntrl.Controls
                    Get_GridControls(Objfrm, chldcntrl, canExit)
                Next
            End If
        End If
    End Sub

 


No comments:

Post a Comment