使用 vb.net 重命名 DataGridView 控件中的列标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17895013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:02:27  来源:igfitidea点击:

Renaming column header in DataGridView Control using vb.net

vb.netdatagridview

提问by Random User

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)

我还需要使用 REGNO(第一列)按升序对内容进行排序

I am using vb.net

我正在使用 vb.net

回答by bansi

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

要更改列标题,请使用 .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)

回答by Akhil Ratheesh

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

否则SELECT rollno as 'RollNo', name as 'Name', class as 'Class' FROM student_tbl这将重命名标题

回答by TrikkStar

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

或者也"Data Grid Object".Columns("column index").HeaderText = "value"可以工作。

回答by Manish Patil

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!

试试这个,我用过同样的。对我来说有效!