Random User Random User - 4 months ago 9
Vb.net Question

Renaming column header in DataGridView Control using vb.net

I tried out the following code but the columns still inherit the table's field names

DataGridView1.DataSource = ds.Tables("student_attendance_table")
With DataGridView1
.RowHeadersVisible = False
.Columns(0).Name = "Register No."
.Columns(1).Name = "Date"
.Columns(2).Name = "Year"
.Columns(3).Name = "Batch"
.Columns(4).Name = "Hour 1"
.Columns(5).Name = "Hour 2"
.Columns(6).Name = "Hour 3"
.Columns(7).Name = "Hour 4"
.Columns(8).Name = "Hour 2"
.Columns(9).Name = "Attendance"
End With


Content follows:

1138M0345 27-07-2013 3 1 P P P P P P
1138M0346 27-07-2013 3 1 P P P P P P
1138M0347 27-07-2013 3 1 P P P P P P
1138M0348 27-07-2013 3 1 P P P P P P
1138M0349 27-07-2013 3 1 P P P P P P
1138M0350 27-07-2013 3 1 P P P P P P
1138M0343 27-07-2013 3 1 A A A A A A
1138M0344 27-07-2013 3 1 A A A A A A


Also I need to sort the content in ascending order using REGNO (The first column)

I am using vb.net

Answer

To change the column header use the .HeaderCell.Value = "Display Value"

DataGridView1.DataSource = ds.Tables("student_attendance_table")
    With DataGridView1
        .RowHeadersVisible = False
        .Columns(0).HeaderCell.Value = "Register No."
        .Columns(1).HeaderCell.Value = "Date"
        .Columns(2).HeaderCell.Value = "Year"
        .Columns(3).HeaderCell.Value = "Batch"
        .Columns(4).HeaderCell.Value = "Hour 1"
        .Columns(5).HeaderCell.Value = "Hour 2"
        .Columns(6).HeaderCell.Value = "Hour 3"
        .Columns(7).HeaderCell.Value = "Hour 4"
        .Columns(8).HeaderCell.Value = "Hour 2"
        .Columns(9).HeaderCell.Value = "Attendance"
    End With

and for initial sorting you can use

DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)