code将项目添加到数据网格中是行不通的。不知道什么是错。我想所有的列加入到在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

查看:101
本文介绍了code将项目添加到数据网格中是行不通的。不知道什么是错。我想所有的列加入到在GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公共类如下:

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

我的code其中我一个文本项分配给一个变​​量对象:

my code where i assign a text item to a variable object:

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

我的code,其中我坚持是如下:

My code where i am stuck is below:

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()

我绝对不知道什么是错与此code,如果有人可以帮助我或纠正对我来说将是巨大的帮助,谢谢。我想所有的四列加入到网格视图。然后继续使用否则,如果增加越来越多的列。

I am absolutely not sure whats wrong with this code and if someone can help me or correct it for me would be great help Thanks. I want to add all four columns into the grid view. and then keep adding more and more columns using else if.

推荐答案

试试这个,广泛的,但它的工作,它会显示你对listbox2值

Try this, its extensive but it does work, it displays all the values you have on 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

这篇关于code将项目添加到数据网格中是行不通的。不知道什么是错。我想所有的列加入到在GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆