将项目添加到数据网格的代码不起作用。不知道怎么了。我想将所有列添加到 gridview

Code to add items to a data grid is not working. Not sure whats wrong. I want to add all the columns into the gridview

我的PublicClass如下:

public class GlobalVariable
        public shared listbox2Count = listbox2.items.count
        public shared containsListbox2Item
End Class

我将文本项分配给变量对象的代码:

Public Function getListBoxText()

    If ListBox2.Text = "X,Y Coordinate" Then
        GlobalVariable.containsListBox2Item = "X,Y Coordinate"
    ElseIf ListBox2.Text = "Latitude, Longitude" Then
        GlobalVariable.containsListBox2Item = "Latitude, Longitude"

    Return Nothing

End Function

我卡住的代码如下:

Dim dt As New DataTable
        dt.Clear()
        For i As Integer = 0 To GlobalVariable.listbox2Count - 1
            If GlobalVariable.containsListBox2Item(i) = "X,Y Coordinate" Then
                dt.Columns.Add("X Coordinate")
                dt.Columns.Add("Y Coordinate")
            ElseIf GlobalVariable.containsListBox2Item(i) = "Latitude, Longitude" Then
                If (Not dt.Columns.Contains("Latitude")) Then dt.Columns.Add("Latitude")
                If (Not dt.Columns.Contains("Longitude")) Then dt.Columns.Add("Longitude")
            End If
        Next
        Dim mr As DataRow
        mr = dt.NewRow
                mr("X Coordinate") = "203910"
                mr("Y Coordinate") = "190280"
                mr("Latitude") = "100022"
                mr("Longitude") = "201999"
        dt.Rows.Add(mr)
GridView1.DataSource = dt
GridView1.DataBind()

我完全不确定这段代码有什么问题,如果有人能帮助我或为我纠正它会很有帮助谢谢。我想将所有四列添加到网格视图中。然后使用 else if 继续添加越来越多的列。

通过使用 ListBox 的内容提供列信息来定义 GridView 的简单表单

重要 我已经更改了列表框值以表示列 Headers。请参阅下面的原因。

<form id="form1" runat="server">
  <div>
    <asp:ListBox ID="ListBox2" runat="server">
      <asp:ListItem Text="X,Y Coordinate" Value="X Coordinate, Y Coordinate" />
      <asp:ListItem Text="Latitude, Longitude" Value="Latitude, Longitude" />
    </asp:ListBox>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
      EmptyDataText="No Data" ShowHeader="true">
    </asp:GridView>
  </div>
</form>

代码隐藏

Public Class WebForm1
  Inherits System.Web.UI.Page

  ' DataTable Name and Application Cache Key
  Const DataTableCacheKey As String = "CoordinatesDataTable"

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' Create the Table and add to Cache, only once
    If Not Page.IsPostBack Then
        CreateDataTableFromListView()
    End If

    GridView1.DataSource = Cache(DataTableCacheKey)
    GridView1.DataBind()
  End Sub

创建数据源 注意:通过将 ListItem 的值 属性 用作逗号分隔的字符串列表,您可以将多个列关联到给定的坐标系。请注意,这不是理想的方式,因为无法设置数据类型,我假设一切都是 Double 值。它仍然比确定列和 headers.

的一长串 If 条件要好得多
  ' All the magic happens here...
  Private Sub CreateDataTableFromListView()

    ' Instantiate a DataTable with a table name
    Dim dt As New DataTable(DataTableCacheKey)

    ' Loop throught the ListBox and...
    For Each li As ListItem In ListBox2.Items

        ' Split each Value Property into an array 
        ' of strings to use as Column identifiers
        Dim ColumnHeaders() As String = li.Value.Split(",")

        ' Loop through the array of strings to create
        ' DataTable Columns
        For Each ch As String In ColumnHeaders
            dt.Columns.Add(ch, GetType(Double))
        Next
    Next

    ' Add an initial Blank Row 
    Dim row As DataRow = dt.NewRow()

    ' Loop Through Columns and set a default 
    ' value of 0 for each column 
    For Each c As DataColumn In dt.Columns
        row(c.ColumnName) = 0
    Next

    ' Add the Row to the Table
    dt.Rows.Add(row)

    ' Persist the DataTable to the Application cache
    Cache(DataTableCacheKey) = dt

  End Sub
End Class

试试这个,它很广泛但确实有效,它显示了你在 listbox2 上的所有值

  Dim i As Integer
    Dim dt As New DataTable
    Dim Card As String = ""
    Dim Geo As String = ""

    For i = 0 To ListBox2.Items.Count - 1
        If ListBox2.Items.Item(i).Text = "X,Y Coordinate" Then
            If Card = "ok" Then

            Else
                dt.Columns.Add("X Coordinate")
                dt.Columns.Add("Y Coordinate")
                Card = "ok"
            End If
        ElseIf ListBox2.Items.Item(i).Text = "Latitude, Longitude" Then
            If Geo = "ok" Then

            Else
                dt.Columns.Add("Latitude")
                dt.Columns.Add("Longitude")
                Geo = "ok"
            End If
        End If
    Next


    Dim mr As DataRow

    For i = 0 To ListBox2.Items.Count - 1
        If ListBox2.Items.Item(i).Text.Contains(dt.Columns.Item(0).ToString.Substring(0, 1)) Then
            If dt.Columns.Item(0).ToString = "Latitude" Then
                mr = dt.NewRow
                mr("Latitude") = "100909"
                mr("Longitude") = "1002929"


            ElseIf dt.Columns.Item(0).ToString = "X Coordinate" Then
                mr = dt.NewRow
                mr("X Coordinate") = "909090"
                mr("Y Coordinate") = "109209"
            End If

        End If

        If ListBox2.Items.Item(i).Text.Contains(dt.Columns.Item(2).ToString.Substring(0, 1)) Then
            If dt.Columns.Item(2).ToString = "Latitude" Then
                mr("Latitude") = "100909"
                mr("Longitude") = "1002929"

            ElseIf dt.Columns.Item(2).ToString = "X Coordinate" Then
                mr("X Coordinate") = "909090"
                mr("Y Coordinate") = "109209"
            End If
        End If

        Dim res As Integer
        res = i Mod 2

        If res > 0 Or i = ListBox2.Items.Count - 1 Then

            dt.Rows.Add(mr)
            GridView1.DataSource = dt
            GridView1.DataBind()
        End If
    Next