我已经看了很多,似乎没有什么能回答我的问题。我有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
所有内容都运行并完美编译,尽管如前所述,只显示了最后一行的数据(正确填充)。
如果有人可以帮助弄清楚为什么只填充了一行,那将非常感激。
谢谢,
洛根
答案 0 :(得分: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
参考:Adding a New Row to DataGridView和How to: Manipulate Rows in the Windows Forms DataGridView Control
答案 1 :(得分: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