将GridView绑定到DataSet或DateReader上

长时间使用2.0的控件可能会使程序员退化的不会写代码,下面的代码是使GridView绑定到DataSet或DataReader上。

 1 None.gif private   void  Bind()
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        string dbConnection = ConfigurationManager.ConnectionStrings["NewsConnectionString"].ToString();//获得数据库连接字符串
 4InBlock.gif        Response.Write(dbConnection);
 5InBlock.gif        SqlConnection conn = new SqlConnection(dbConnection);
 6InBlock.gif        conn.Open();
 7InBlock.gif        string sql = "select * from News_News where Class_Id=" +this.ClassId;
 8InBlock.gif        SqlCommand cmd = new SqlCommand(sql, conn);
 9InBlock.gif        DataSet ds = new DataSet();
10InBlock.gif        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
11InBlock.gif        da.Fill(ds);
12InBlock.gif        //SqlDataReader dr = cmd.ExecuteReader();
13InBlock.gif        GridView1.DataSource = ds.Tables[0];
14InBlock.gif        //GridView1.DataSource = dr;
15InBlock.gif        GridView1.DataBind();//将GridView绑定到DataSet上
16InBlock.gif        string sql1 = "select Class_Name from News_Class where Id=" + this.ClassId;
17InBlock.gif        cmd.CommandText = sql1;
18InBlock.gif        string className=cmd.ExecuteScalar().ToString();//返回首行首列,动态显示新闻分类
19InBlock.gif        Label1.Text = className;
20InBlock.gif        conn.Close();
21InBlock.gif        
22ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/qyjun/archive/2007/05/05/736566.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值