0

我是 vb.net 的新手。我有一个示例 datagridview,其中包含一些来自 Internet 的示例代码。运行代码使用数组并基于代码:

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 上获取我编辑的列名的第一个字母并循环它。抱歉,如果我的问题有重复。我刚刚发布了代码,以便我可以让任何人理解我要问的内容。我真的需要帮助。:(

4

1 回答 1

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"
于 2013-09-09T23:05:15.237 回答