首页 > 开发 > 综合 > 正文

DPC: Creating a multicolumn dropdownlist[等级:初 中]

2024-07-21 02:25:03
字体:
来源:转载
供稿:网友
国内最大的酷站演示中心!
creating a multicolumn dropdownlist
click here to return to my article index
one of the frequently asked questions over at asplists.com is: "how do i create a multicolumn dropdownlist?". i have come up with two ways to accomplish this task.

the first technique combines the two columns in the sql query used to create the resultset and then binds the resultset to the dropdownlist. more aspalliance sql articles can be found here.

code:
    sub filldropdownsql()
    dim myconnection as sqlconnection = new sqlconnection("data source=test; user id=test; password=test; initial catalog=pubs")
    dim mycommand as sqlcommand = new sqlcommand("select pub_id + ',' + pub_name as idandname from publishers", myconnection)
    
    myconnection.open()

    dropdownlist1.datatextfield = "idandname"

    dropdownlist1.datasource = mycommand.executereader(commandbehavior.closeconnection)
        dropdownlist1.databind()
    end sub



result:
0736,new moon books 0877,binnet & hardley 1389,algodata infosystems 1622,five lakes publishing 1756,ramona publishers 9901,ggg&g 9952,scootney books 9999,lucerne publishing

the second technique combines the two columns while looping through a sqldatareader. the combination of the two columns is then programmatically added to the dropdownlist. more information on the sqldatareader can be found here.

code:
    sub filldropdowncode()
    dim myconnection as sqlconnection = new sqlconnection("data source=test; user id=test; password=test; initial catalog=pubs")
    dim mycommand as sqlcommand = new sqlcommand("select pub_id,pub_name from publishers", myconnection)
    dim mydatareader as sqldatareader

    myconnection.open()
    mydatareader = mycommand.executereader()
    
    while mydatareader.read()
        dropdownlist2.items.add(mydatareader.getstring(0) & "," & mydatareader.getstring(1))
    end while

    mydatareader.close()
    myconnection.close()
    end sub


result:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表