什么时候我们需要绑定:
1. 当我们在WebForm使用DataGrid的时候,最后一定不要忘记了绑定语句的使用。当然在WinForm中,DataGrid控件有对数据的自动功能,所以就不用使用这个语句了,不要多此一举哦!(下面是一个WebServices的例子)
[WebMethod]
public DataSet SQLDB(string Query)
{
try
{
SqlConnection CS = new SqlConnection("server=(local);database=Northwind;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter (Query, CS);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "Products");
return myDataSet;
}
catch(Exception ex)
{
return DataError(ex);
}
}
private void btSQL_Click(object sender, System.EventArgs e)
{
DBWebServices.DBService db = new DBWebServices.DBService();
DataSet ds = db.SQLDB("Select * from Products");
this.DataGrid1.DataSource = ds.Tables[0];
this.DataBind();
}
2. 在想要分页的时候,产生了PageIndexChanged事件后,要使用数据绑定。
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
DBWebServices.DBService db = new DBWebServices.DBService();
DataSet ds = db.SQLDB("Select * from Products");
this.DataGrid1.DataSource = ds.Tables[0];
this.DataBind();
[注]对数据进行重新绑定,不能少,不明白为什么!
以后遇到了再加上。。。