关于数据绑定,还有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();
//*/
}
一个小例子,以下测试均通过有效
要实现在下拉菜单中读取用户名
在表格中显示用户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();
//*/
}
相关文章推荐
- 关于DataGrid数据绑定后对字段进行替换的问题与办法
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- windows mobile中使用datagrid绑定并显示数据及得到界面显示的数据值
- 关于EasyUI Datagrid 数据网格使用策略
- 关于使用asp.net 的分页显示数据。Repeater绑定数据后的分页显示。
- DataGrid中DropDownList 的数据绑定
- 在ascx中使用DropDownList绑定数据页面刷新时变空的问题
- 关于DropDownList绑定数据的一点认识(原创)
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- ASP.NET MVC DropDownList数据绑定及使用详解
- 在ListView中使用DropDownList绑定数据……好麻烦
- DataGrid中DropDownList 的数据绑定
- SilverLight学习笔记--关于使用IValueConvert对绑定数据的格式化操作
- 如何使用动态创建模板列并绑定数据(GridView,Repeater,DataGrid)
- flex2使用HTTPService得到xml数据绑定到datagrid
- 在ascx中使用DropDownList绑定数据页面刷新时变空的问题
- SilverLight中datagrid数据绑定的简单使用
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 关于DataGrid数据绑定后对字段进行替换的问题与办法
- 关于将数据库中的bit型数据绑定到DataGrid中的CheckBox的Checked属性的问题。