locked
Rename the Datagridview Columns

    Question

  • Hi dears.
    I just filled my datagridview in run time.
    how i can rename columns without using properties?
    For instance, i have a field in Database named FName. It has shown FName in my project to. But i wanna change it to something else.
    another thing is that... how i can reverse my columns in run time?
    for example i have Fname and Lname i as row in my DB.
    Now i just wanna them to appear LName and FName in my datagridview in run time.
    thank you in advance.
    Saturday, January 02, 2010 10:13 AM

Answers

  • Dear MKSoft,

    I wrote you an example on how to rename a columname in a DatagridView.

        Public Sub RenameColumn(ByVal Grid As DataGridView, ByVal OldColumnName As String, ByVal NewColumnName As String)
            Try
                Dim gColumn As DataGridViewColumn
                For Each gColumn In Grid.Columns
                    If gColumn.HeaderText = OldColumnName Then
                        gColumn.HeaderText = NewColumnName
                    End If
                Next
                Grid.Refresh()
            Catch ex As Exception
                Debug.Print(ex.ToString)
            End Try
        End Sub

    If you want to do this without using properties, you will have to do this in the dataset / datatable itself that you are using as the datasource for your grid.

    In the SQL for instance. Your original Query looks like this:

    "SELECT FNAME, LNAME FROM sometable; "

    you can use aliases for the selected columns like this "SELECT FNAME as FirstName, LNAME as LastName FROM sometable". The order of the columns is also best changed -before- you display them. This is much faster.

    Hope this helps!

    Cheers,

    John


    • Proposed as answer by jwavila Saturday, January 02, 2010 7:33 PM
    • Marked as answer by Martin_XieModerator Tuesday, January 05, 2010 8:31 AM
    Saturday, January 02, 2010 1:42 PM

All replies

  • Dear MKSoft,

    I wrote you an example on how to rename a columname in a DatagridView.

        Public Sub RenameColumn(ByVal Grid As DataGridView, ByVal OldColumnName As String, ByVal NewColumnName As String)
            Try
                Dim gColumn As DataGridViewColumn
                For Each gColumn In Grid.Columns
                    If gColumn.HeaderText = OldColumnName Then
                        gColumn.HeaderText = NewColumnName
                    End If
                Next
                Grid.Refresh()
            Catch ex As Exception
                Debug.Print(ex.ToString)
            End Try
        End Sub

    If you want to do this without using properties, you will have to do this in the dataset / datatable itself that you are using as the datasource for your grid.

    In the SQL for instance. Your original Query looks like this:

    "SELECT FNAME, LNAME FROM sometable; "

    you can use aliases for the selected columns like this "SELECT FNAME as FirstName, LNAME as LastName FROM sometable". The order of the columns is also best changed -before- you display them. This is much faster.

    Hope this helps!

    Cheers,

    John


    • Proposed as answer by jwavila Saturday, January 02, 2010 7:33 PM
    • Marked as answer by Martin_XieModerator Tuesday, January 05, 2010 8:31 AM
    Saturday, January 02, 2010 1:42 PM
  • so appreciate dear john. it works.
    Saturday, January 02, 2010 6:18 PM
  • No problem,

    Glad i could help.

    Cheers,

    John
    Saturday, January 02, 2010 7:19 PM
  • so appreciate dear john. it works.
    can u mark as Answered Please....
    Just Be Humble Malange!
    Monday, January 04, 2010 3:45 PM