3

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

4 Answers 4

11

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)
Sign up to request clarification or add additional context in comments.

Comments

0

or SELECT rollno as 'RollNo', name as 'Name', class as 'Class' FROM student_tbl this will rename the header

Comments

0

Alternatively "Data Grid Object".Columns("column index").HeaderText = "value" works as well.

Comments

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

Try this, I used same. Working for me!

4 Comments

Maybe add some explanation
@WesleyCoetzee, Is this explanation satisfied ?
@evry1falls - Unless I'm not seeing it, but this has no explanation.
The above answer and this answer are both suitable for Renaming column header in DataGridView Control using vb.net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.