2013-07-27 20 views
2

我嘗試了下面的代碼重命名列標題在DataGridView控件,但仍列繼承表的字段名用vb.net

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 

內容如下:

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 

此外,我需要重新梳理內容按升序使用REGNO(第一列)

我正在使用vb.net

回答

5

要更改列標題使用.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 

,並進行初步排序,你可以使用

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

SELECT rollno as 'RollNo', name as 'Name', class as 'Class' FROM student_tbl 這將重新命名的標題

+0

不需要tat長的代碼:D –

0

或者"Data Grid Object".Columns("column index").HeaderText = "value"作品也是如此。

-1
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 

試試這個,我用過了。爲我工作!

+1

也許補充說明 –