长时间使用2.0的控件可能会使程序员退化的不会写代码,下面的代码是使GridView绑定到DataSet或DataReader上。
1
private void Bind()
2
{
3
string dbConnection = ConfigurationManager.ConnectionStrings["NewsConnectionString"].ToString();//获得数据库连接字符串
4
Response.Write(dbConnection);
5
SqlConnection conn = new SqlConnection(dbConnection);
6
conn.Open();
7
string sql = "select * from News_News where Class_Id=" +this.ClassId;
8
SqlCommand cmd = new SqlCommand(sql, conn);
9
DataSet ds = new DataSet();
10
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
11
da.Fill(ds);
12
//SqlDataReader dr = cmd.ExecuteReader();
13
GridView1.DataSource = ds.Tables[0];
14
//GridView1.DataSource = dr;
15
GridView1.DataBind();//将GridView绑定到DataSet上
16
string sql1 = "select Class_Name from News_Class where Id=" + this.ClassId;
17
cmd.CommandText = sql1;
18
string className=cmd.ExecuteScalar().ToString();//返回首行首列,动态显示新闻分类
19
Label1.Text = className;
20
conn.Close();
21
22
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)