ASP.NET中的绑定(DataBind)问题

什么时候我们需要绑定:

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();

[注]对数据进行重新绑定,不能少,不明白为什么!

以后遇到了再加上。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值