长时间使用2.0的控件可能会使程序员退化的不会写代码,下面的代码是使GridView绑定到DataSet或DataReader上。
1
private
void
Bind()
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
3
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)