日期:2014-05-17  浏览次数:20372 次

ArrayList 绑定到 DropDownList 怎么指定DataTextField和DataValueField
本帖最后由 flyso 于 2013-03-26 14:40:20 编辑
ArrayList 绑定到 DropDownList 怎么指定DataTextField和DataValueField

代码如下:
  Function GetRows(ByVal tablename As String) As ArrayList
        Dim arrRows As New ArrayList
        Dim dr = BASE.getReader("select * from " & tablename)
        Dim schemaTable As DataTable = dr.GetSchemaTable()
        Dim MaxFields As Integer = schemaTable.Rows.Count

        For j = 0 To MaxFields - 1
            Dim ColumnName = schemaTable.Rows.Item(j)("columnname")
            Dim Value = GetCode(ColumnName)
            arrRows.Add(New ListItem(ColumnName, Value))
        Next
        BASE.closeReader(dr)
        Return arrRows
    End Function


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            DropDownList1.DataSource = GetRows("t_Projects")
            DropDownList1.DataValueField = 字段不知道是什么,只知道这是第一个值(上面的ColumnName),这里写什么
            DropDownList1.DataTextField = 字段不知道是什么,只知道这是第二个值(上面的Value),这里写什么
            DropDownList1.DataBind()
        End If
    End Sub

如果不加 DropDownList1.DataValueField 和 DropDownList1.DataTextField 绑定后是成功的有值的
但是只有一个值,text和value是一样的,我现在需要的是text和value不一样,分别指定不同的字段,但因为是ArrayList字段也不确定,不知道写什么,请高手指点

------解决方案--------------------
你可以这样 托上去一个GridView 然后自动生成列 绑定arraylist

看列名字. 然后知道列名了 so. 就可以了