迭代表并保存结构中的项目

时间:2013-11-06 10:43:35

标签: asp.net vb.net iteration

好吧,所以我有这种结构:

Structure wspArtikel
    Dim gID As Guid()
    Dim sText As String
    ... more fields like this
End Structure

我还有一个包含IDText列的HTML表格;还有一个包含复选框的列 现在我想迭代(在button.Click-Event)表格中选中复选框的所有项目并将其保存到我的结构中。

我尝试了什么:

Dim wstruc As New wspArtikel
For Each gRow As GridViewRow In gvArtikel.Rows
    Dim chkArtikel As CheckBox = DirectCast(gRow.FindControl("checkbox"), CheckBox)
    If chkArtikel.Checked Then
        wstruc.gID = New Guid(DirectCast(gRow.FindControl("gID"), HiddenField).Value)
    End If
Next

如果只选择了一个项目,则可以正常工作 正如您可能已经看到的,如果选择了两个项目,那么它将覆盖第一个项目,并且只有一个项目将保留在我的结构中。

如何收集结构中每个已检查项目的所有数据?

1 个答案:

答案 0 :(得分:1)

我不喜欢使用Structs。有时候使用像DataTable这样的其他结构会更容易。

提示:

要保存不同的结构,您需要使用LIST结构(或其某种变体)。下面是使用结构列表的示例。列表中的每个项目都可以通过索引访问。下面我演示添加1个项目(1个结构出现在列表中):

 Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text

    Namespace ConsoleApplication1021
        Class Program

            Private Structure wspArtikel
                Public gID As Guid()
                Public sText As String
                '... more fields like this
            End Structure

            Private Shared Sub Main(args As String())

                'Define list 
                Dim structList As New List(Of wspArtikel)()

                'Create list object
                Dim artListVar = New wspArtikel()

                'Define array of 2 items - This is an example, you need to set the correct value
                artListVar.gID = New Guid(1) {}

                'Assign value to array of 1st occurrence in the list
                artListVar.gID(0) = Guid.NewGuid()
                artListVar.gID(1) = Guid.NewGuid()


                'Assign value to string in 1st occurrence in the list
                artListVar.sText = "String-0"

                structList.Add(artListVar)

                      'Display items in list
                       For Each itm As var In structList
                            Console.WriteLine((artListVar.gID(0).ToString() & " ") + artListVar.sText)
                       Next

                Console.WriteLine("Done")
            End Sub
        End Class
    End Namespace