Web 动态绑定 GridView

1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class query : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18
19 }
20
21 protected void BindGridView(string QuerySql)
22 {
23 if (QuerySql == null) return;
24 this.SqlDataSource3.ConnectionString = ConfigurationManager.ConnectionStrings["学生成绩管理系统ConnectionString"].ConnectionString;
25 this.SqlDataSource3.ProviderName = "System.Data.SqlClient";
26 this.SqlDataSource3.SelectCommand = QuerySql;
27 this.SqlDataSource3.SelectCommandType = SqlDataSourceCommandType.Text;
28 this.GridView2.DataSource = this.SqlDataSource3;
29 GridView2.DataBind();
30 }
31
32 protected void Button1_Click(object sender, EventArgs e)
33 {
34 if (TextBox1.Text == null || TextBox1.Text == "") return;
35 string sql = "select StName,Class,SuName,SuScore from Student,Score where Student.StId=Score.StId and Score.StId='";
36 sql = sql + TextBox1.Text + "'";
37 Session["QuerySql"] = sql;
38 BindGridView((string)Session["QuerySql"]);
39 }
40
41 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)//在绑定数据后根据相应条件将对应行背景色改为红
42 {
43 if (e.Row.RowType == DataControlRowType.DataRow)
44 {
45 int score = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SuScore"));//分数>90
46 if (score >= 90) e.Row.BackColor = System.Drawing.Color.Red;
47 }
48 }
49 }
posted @ 2011-05-28 17:01  Jog  阅读(207)  评论(0)    收藏  举报