您的位置:首页 > 运维架构

关于数据绑定,还有DropDownList和DATAGRID的使用

2006-10-31 15:42 609 查看
学习.net有所收获了,来记录一下

一个小例子,以下测试均通过有效

要实现在下拉菜单中读取用户名
在表格中显示用户ID,用户名……

先写一个DB类

namespace _127._0._0._1.include
{
public class DataBaseDB
{
//public static String ConnectionString = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];
//此方法调用web.config中的连接方法
public static SqlConnection createCon()
{
return new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
}
}
}

下面是.cs文件

protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//绑定DropDownList
SqlConnection myConnection = DataBaseDB.createCon();
myConnection.Open();
String cmdText = "select * from Employees ";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
SqlDataReader recn = myCommand.ExecuteReader();
this.DropDownList1.DataSource = recn;
this.DropDownList1.DataTextField="LastName";
this.DropDownList1.DataValueField="EmployeeID";
this.DropDownList1.DataBind();

//绑定DATAGRID
SqlConnection con =DataBaseDB.createCon();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("SELECT * FROM Employees");
sda.SelectCommand.Connection = con;
DataSet ds = new DataSet();
sda.Fill(ds,"emp");
this.DataGrid1.DataSource = ds.Tables["emp"];
this.DataGrid1.DataBind();
//*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐