【问题标题】:VB.NET - Issue with looping through rows in a datagridVB.NET - 循环遍历数据网格中的行的问题
【发布时间】:2012-04-11 04:42:41
【问题描述】:
 For Each row As DataRow In dgrFarms.Rows
        Dim sendtroopid As Integer
        sendtroopid = row("idColumn")
        'Do Something
Next

一段时间以来,我一直试图在 VB.NET 中的一列中的一行中循环,我也已经完成了我的作业。当我使用上面的代码时,我得到:

无法将“System.Windows.Forms.DataGridViewRow”类型的对象转换为“System.Data.DataRow”类型。

我看到另一个指南告诉我这样做:

For Each row As DataGridView In dgrFarms.Rows
        sendtroopid = row("idColumn")
        'Do Something
Next

但这给了我错误:

重载解析失败,因为没有可访问的“项目”接受此数量的参数。

(这是 'Row("idColumn") 上的蓝色下划线)

【问题讨论】:

  • 退后一步...您真的要循环遍历 UI 元素(数据网格)的行,还是真的要遍历 data?前者有一些场景,但后者更为常见。
  • '是的,数据,我该怎么做?

标签: vb.net datagrid datagridview


【解决方案1】:

您是否尝试通过检查被绑定行的 itemType 来运行您的代码?尝试将您的代码放在:

If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
'do loop
end if

【讨论】:

  • 记住大卫的评论。此外,要添加到此答案,该行可以是标题行(具有自己的类型)
  • 我对 VB 还不是很自信,我不知道这是做什么的,也不知道如何在我自己的代码中实现它。当我尝试这个时,我只是得到了大量的蓝色下划线。
【解决方案2】:

代码应该是:

For Each row As DataGridViewRow In dgrFarms.Rows
    sendtroopid = row.Cells("idColumn").Value
    'Do Something
Next

请注意,每一行都是DataGridViewRow,而不是DataGridView。此外,您还可以使用 Cells 属性从此行中获取特定单元格的内容。

【讨论】:

  • 非常感谢!我知道这会很简单!
【解决方案3】:

这是我在所有应用程序中使用的功能:

''' <summary>
''' Provides the Properties and Methods to insert each Column from eaqch Row in a Dataset
''' when given a Dataset and a List Box 
''' </summary>
''' <remarks></remarks>
Public Class clsDisplayDataset
    Private mstrClsTitle As String = "clsDisplayDataset"

    ''' <summary>
    ''' For Each Row and Each Column create a single line to be inserted into the 
    ''' List Box
    ''' </summary>
    ''' <param name="idsDataset"></param>
    ''' <param name="ilstListBox"></param>
    ''' <returns>True   -    When successful
    '''          False  -    When unsuccessful</returns>
    ''' <remarks></remarks>
    Public Function DisplayDataSet(ByVal idsDataset As DataSet,
                                   ByRef ilstListBox As ListBox) As Boolean
        Dim lstrRowValue As String = ""
        Try
            For Each ldsRow As DataRow In idsDataset.Tables(0).Rows
                lstrRowValue = ""
                For Each ldsCol As DataColumn In idsDataset.Tables(0).Columns
                    lstrRowValue = lstrRowValue & ldsRow(ldsCol.ColumnName).ToString() & vbTab
                Next
                ilstListBox.Items.Add(lstrRowValue)
            Next
            Return True
        Catch ex As Exception
            DisplayDataSet = False
            Throw New Exception(mstrClsTitle & ".DisplayDataSet" & vbCrLf &
                                "Error Number      [" & Err.Number & vbCrLf &
                                "Error Description [" & Err.Description & "]" & vbCrLf &
                                "Failed to insert columns into List Box")
        End Try
    End Function
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多