DPC: Creating a multicolumn Dropdownlist[等级:初 中]
来源:互联网 发布:华为云计算工程师面试 编辑:程序博客网 时间:2024/06/01 08:05
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
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:
DPC: Creating a multicolumn Dropdownlist[等级:初 中]';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Creating a multicolumn DropdownlistClick 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:
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- DPC: Creating a multicolumn Dropdownlist[等级:初 中]
- DPC:Hiding Columns In A DataGrid[等级:初 中]
- DPC:Creating a DataBound List of Radio Buttons--PA
- dpc:Creating a DataBound List of Radio Buttons--预览
- DPC
- DPC
- android中Creating a Searchable Activity
- android中Search之Creating a Searchable Configuration
- Creating a Phong Specular type中对插图的疑问
- Creating a Scrollable DataGrid...
- Creating a Transparent Form
- Creating a Multithreaded Service
- Creating a Delete Button
- Creating a clickable map
- Creating a WCF ACD
- Creating a New Project
- Creating a Parameter Block
- Creating a Singleton Instance
- 桌面中心(三) 修改数据库
- dpc:Creating a DataBound List of Radio Buttons--预览
- 桌面中心(二) 数据库写入
- 在 Visual C# .NET 中建立一个平滑的进度条
- 桌面中心(一) 创建数据库
- DPC: Creating a multicolumn Dropdownlist[等级:初 中]
- PHP 留言簿(带Oracle数据库分页的显示功能)
- C#如何打开关闭CDROM
- 一个广告轮播系统的例子(内含文件上传的方法)
- DPC:Hiding Columns In A DataGrid[等级:初 中]
- 用PHP发送MIME邮件(六)
- 转: ASP.NET高级应用 (1)
- 用PHP发送MIME邮件(五)
- ASP.NET 数据访问类