如何显示从表到下拉列表的数据 [英] how to display the data from table to dropdownlist

查看:58
本文介绍了如何显示从表到下拉列表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家..............



如何在asp.net c#中将数据从表格填充到下拉列表中br />


我在Sqldatabase中有一个表作为大学,这个大学列表必须使用代码显示在下拉列表中。



这是我的CollegeTable



CollegeCode CollegeName

HITS Hipoint engg college

WITS Wisdom engg college
SITS Shaaz engg大学

Pits Padmavathi engg大学





现在我要显示使用asp.net c#代码下拉列表中的collegeCodes。



请帮助



感谢@dvance。

Hi experts..............

how to fill the data from table to dropdownlist in asp.net c#

I have a table in Sqldatabase as colleges, this colleges list must be displayed in dropdownlist using code.

This is my CollegeTable

CollegeCode CollegeName
HITS Hipoint engg college
WITS Wisdom engg college
SITS Shaaz engg college
PITS Padmavathi engg college


Now i want to display collegeCodes in dropdownlist using asp.net c# code.

Please help

Thanks in @dvance.

推荐答案

1)从数据库中获取数据并将其填充到数据表中,如dtCollege

1) Get data from your database and fill it to datatable like dtCollege
SqlConnection con = new SqlConnection("");//connection name
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dtCollege = new DataTable();
da.Fill(dtCollege);



2)设置下拉列表的两个属性


2) set two properties of your dropdown list

//If you need to display college name to dropdown list
   ddl.DataValueField= "CollegeCode";
   ddl.DataTextField= "CollegeName";

//OR

//If you need to display college Code to dropdown list
   ddl.DataValueField= "CollegeCode";
   ddl.DataTextField= "CollegeCode";



3)然后将数据源分配到您的下拉菜单


3) then assign data-source to your dropdown

ddl.DataSource = dtCollege;
  ddl.DataBind();





4)最后代码如下所示---- Select College -----



4) Finally code looks like this with "----Select College-----"

SqlConnection con = new SqlConnection("");//connection name
con.Open();
SqlCommand cmd = new SqlCommand("select * from College", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dtCollege = new DataTable();
da.Fill(dtCollege);
//If you need to display college name to dropdown list
ddl.DataValueField= "CollegeCode";
ddl.DataTextField= "CollegeName";

//OR

//If you need to display college Code to dropdown list
ddl.DataValueField= "CollegeCode";
ddl.DataTextField= "CollegeCode";

ddl.DataSource = dtCollege;
ddl.DataBind();
ddl.Items.Insert(0,"---Select College----");











就是这样,你的下拉列表与你的数据库表数据绑定。






that''s it, your dropdownlist is bind with your database table data.


你好,



1st通过存储过程或简单的sql命令从数据库中获取数据。然后将数据保存到数据表中并将数据表数据提供给下拉列表。



代码snipet:



Hi,

1st get the data from the database by a stored procedure or simply a sql command. Then keep the data into a data table and feed the datatable data to the dropdownlist.

Code snipet here:

SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=test;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("select * from hospital", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DropdownList.DataSource = dt;
            DropdownList.DataTextField = "CURRENCY_SHORT_DESCRIPTION";
            DropdownList.DataValueField = "CURRENCY_SHORT_DESCRIPTION";
            DropdownList.DataBind();





谢谢



Thanks





尝试此代码..



Hi,

try this code..

SqlConnection con = new SqlConnection("connection string");// create connection string
con.Open();   // open the connection
SqlCommand cmd=new SqlCommand("Select CollegeCode, CollegeName from CollegeTable",con);
              // call the data from database 
SqlDataAdapter da=new SqlDataAdapter(cmd); // communicate between Dataset and DB
DataSet ds=new DataSet();
da.Fill(ds); 

//Bind the result set to DropDownList 
dropdownList1. DataSource=ds.Tables[0];  
dropdownList1.DisplayMember ="CollegeName"; 
dropdownList1.ValueMember ="CollegeCode"; 
dropdownList1.DataBind();

ListItem lstSelect = new ListItem();
lstSelect.Text = "----Select----->";
lstSelect.Value = "";
dropdownList1.Items.Insert(0, lstSelect);





如果您想了解更多相关信息,请参阅以下链接...



http://www.codersource.net/AspNet/AspNetArticles/DropDownListinASPNet.aspx [ ^ ]


这篇关于如何显示从表到下拉列表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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