行绑定时间.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="行绑定事件.aspx.cs" Inherits="ASP.NET练习.行绑定事件" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False"
DataSourceID="练习1" OnRowDataBound="Onrowdatabound">
<Columns>
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
<asp:BoundField DataField="学号" HeaderText="学号" SortExpression="学号" />
<asp:BoundField DataField="语文" HeaderText="语文" SortExpression="语文" />
<asp:BoundField DataField="数学" HeaderText="数学" SortExpression="数学" />
<asp:BoundField DataField="英语" HeaderText="英语" SortExpression="英语" />
<asp:BoundField DataField="物理" HeaderText="物理" SortExpression="物理" />
<asp:BoundField DataField="化学" HeaderText="化学" SortExpression="化学" />
</Columns>
</asp:gridview>
<asp:SqlDataSource ID="练习1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [成绩表]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
行绑定时间.aspx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASP.NET练习
{
public partial class 行绑定事件 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Onrowdatabound(object sender, GridViewRowEventArgs e)//GridViewRowEventArgs e
{
Response.Write(e.Row.RowType + "<br/>");//RowType类型有
if (e.Row.RowType == DataControlRowType.DataRow) {//RoeType共有三种类型:header datarow footer
for (int i = 2; i <= 6; i++) {
TableCell mytc = e.Row.Cells[i];//获取单元格
Single _f = Convert.ToSingle(mytc.Text);//将单元格内容转变成Single型
if (_f < 60) {
mytc.ForeColor = System.Drawing.Color.Red;
mytc.Text = "不合格";
}
if (_f > 90) {
mytc.ForeColor = System.Drawing.Color.Green;
}
}
}
}
}
}
效果如下: