1

我环顾四周,似乎没有什么特别回答我的问题。我有 DataGridView,在其中添加行和列,具体取决于 ArcGIS 中保存的地理空间数据的特定记录数。然后将数据传递到 gridview 进行编辑。

所以我不使用任何数据绑定方法,而是一个非常手动的过程。我将数据存储到一个二维数组中,这些数组的索引基于那些特定的数据记录计数。

循环完成后,数据应根据索引存储在每个单元格中。填充的最后一行被完美填充,但其他 72(左右)行是空白的。

这是代码如下:

这会填充列标题(效果很好):

For i_FieldNum = 0 To pFields_1.FieldCount - 1

        str_FieldName = UCase(pFields_1.Field(i_FieldNum).Name)
        str_FieldAliasName = pFields_1.Field(i_FieldNum).AliasName

        ReDim Preserve m_array_str_FieldNames(i_FieldNum)
        ReDim Preserve m_array_str_FieldAliasNames(i_FieldNum)
        m_array_str_FieldNames(i_FieldNum) = str_FieldName
        m_array_str_FieldAliasNames(i_FieldNum) = str_FieldAliasName

        i_FieldCount = i_FieldCount + 1
        i_ColNum = i_FieldCount - 1

        'If this is the "SHAPE", "FID", or "OBJECTID" field, remember it:
        If (str_FieldName = "SHAPE") Then
            m_i_FieldNum_Shape = i_FieldCount - 1
        ElseIf (str_FieldName = "FID") Then
            m_i_FieldNum_FID = i_FieldCount - 1
        ElseIf (str_FieldName = "OBJECTID") Then
            m_i_FieldNum_OBJECTID = i_FieldCount - 1
        End If

        'Store the field name in the top row:
        grid_Data.ColumnCount = i_FieldCount
        grid_Data.Columns.Item(i_ColNum).HeaderText = str_FieldName

        ReDim Preserve m_array_l_FieldColLengths(i_ColNum)
        m_array_l_FieldColLengths(i_ColNum) = pFields_1.Field(i_FieldNum).Length

    Next i_FieldNum

这将填充二维数组:

 Do Until pFeat_1 Is Nothing
        'Until we run out of features ...

        i_FeatCount = i_FeatCount + 1


        For i_FieldNum = 0 To pFields_1.FieldCount - 1

        'If the field is recorded as a shape
            If (i_FieldNum = m_i_FieldNum_Shape) Then
                array_Data(i_FeatCount - 1, i_FieldNum) = "<Shape>"
            Else

        'If the value of the field is null
                If IsNothing(pFeat_1.Value(i_FieldNum)) Or pFeat_1.Value(i_FieldNum).ToString = "" Then
                    array_Data(i_FeatCount - 1, i_FieldNum) = "<Null>"
                Else

                    array_Data(i_FeatCount - 1, i_FieldNum) = pFeat_1.Value(i_FieldNum).ToString
                End If
            End If

        Next i_FieldNum

        'End If

        'Move to the next feature (if any).
        pFeat_1 = pFeatCursor_1.NextFeature


    Loop

这是假设显示数据:

 For i_RowNum = 0 To i_FeatCount - 1

        'Add a new row to the grid:
        grid_Data.RowCount = i_RowNum + 2

        For i_ColNum = 0 To pFields_1.FieldCount - 1

            grid_Data.Item(i_ColNum, i_RowNum + 1).Value = array_Data(i_RowNum, i_ColNum)

        Next i_ColNum

    Next i_RowNum

一切都运行和编译完美,尽管正如我之前所说,只显示最后一行的数据(正确填充)。

如果有人可以帮助弄清楚为什么只填充了一行,那将不胜感激。

谢谢你,

洛根

4

2 回答 2

2

检查您更新的部分 -这是假设显示数据:如果您的数据设置正确,那么根据我的猜测,它会使网格显示错误的数据。根据您添加的网格中的列对其进行修改。或者您可以创建列名字符串数组以将数据放入特定单元格中。作为dgvr.Cells(columnsArray[index]).Value = array[,]

For i_RowNum = 0 To i_FeatCount - 1

        'Add a new row to the grid:
        DataGridViewRow dgvr = new System.Windows.Forms.DataGridViewRow();
        dgvr.CreateCells(grid_Data)

        For i_ColNum = 0 To pFields_1.FieldCount - 1
            dgvr.Cells(i_ColNum).Value = array_Data(i_RowNum, i_ColNum)
        Next i_ColNum
        grid_Data.Rows.Add(dgvr)
Next i_RowNum

参考向 DataGridView 添加新行以及如何:在 Windows 窗体 DataGridView 控件中操作行

于 2012-05-25T06:49:31.523 回答
0

请参见此处,每次增加行数时都会清除数据:

DataGridView1.RowCount += 1
DataGridView1.Item(0, 0).Value = "a value"
DataGridView1.RowCount += 1
DataGridView1.Item(0, 1).Value = "the previous value has been wiped"

要在不进行任何重大更改的情况下修复您的代码,只需使用此方法添加行:

grid_Data.Rows.Add(paramArray)

甚至更容易将此行放在 for 循环上方并在填充之前设置行数:

grid_Data.RowCount = i_FeatCount - 1
于 2012-05-25T06:05:50.437 回答