我是vb.net的新手。我有一个示例datagridview,其中包含一些来自互联网的示例代码。运行代码使用数组并基于代码:
Const NumberOfColumns As Integer = 8
Const NumberOfRows As Integer = 8
Dim ColumnNumber As Integer
Dim RowNumber As Integer
Dim A As Integer = Asc("Pupil's Name")
Dim Array2D(NumberOfRows, NumberOfColumns) As Integer
DataGridView1.ColumnCount = NumberOfColumns
DataGridView1.RowCount = NumberOfRows
'Make the headers
For RowNumber = 0 To NumberOfRows - 1
DataGridView1.Rows.Item(RowNumber).HeaderCell.Value = _
(RowNumber + 1).ToString
Next
For ColumnNumber = 0 To NumberOfColumns - 1
DataGridView1.Columns.Item(ColumnNumber).HeaderCell.Value = _
Chr(A + ColumnNumber).ToString
DataGridView1.Columns.Item(ColumnNumber).HeaderCell.Style.Alignment = _
DataGridViewContentAlignment.MiddleCenter
Next
'Fill the Array
For RowNumber = 0 To NumberOfRows - 1
For ColumnNumber = 0 To NumberOfColumns - 1
Array2D(RowNumber, ColumnNumber) = _
RowNumber * NumberOfColumns + ColumnNumber
Next
Next
'Fill the Data Grid
For RowNumber = 0 To NumberOfRows - 1
For ColumnNumber = 0 To NumberOfColumns - 1
DataGridView1.Item(ColumnNumber, RowNumber).Value = _
Array2D(RowNumber, ColumnNumber)
DataGridView1.Item(ColumnNumber, RowNumber).Style.Alignment = _
DataGridViewContentAlignment.MiddleCenter
Next
Next
我不知道如何插入自定义列名。我尝试右键单击datagridview并编辑其列名,但是当我运行它时,列名称将在datagridview上获取我编辑的列名的第一个字母并将其循环。对不起,如果我的问题有重复。我刚刚发布了这些代码,以便让任何人了解我想要问的内容。我真的需要帮助。 :(
答案 0 :(得分:0)
查看此链接http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columns.aspx。
这是你如何做到的。
'设置列标题名称。
dataGridView1.Columns(0).Name = "Recipe"
dataGridView1.Columns(1).Name = "Category"
dataGridView1.Columns(2).Name = "Main Ingredients"
dataGridView1.Columns(3).Name = "Rating"