首页
最近更新
热门教程
ASP.NET
开发语言
开发环境
AJax教程
控件开发
统计报表
数据库
Web服务
安装部署
HTML教程
Javascript
XML教程
Community Server
NHibernate
书籍推荐
常用工具
实用代码
教程全文搜索
首页
>>
ASP.NET
GridView 72般绝技(五)
添加人:
iyond
添加时间:2007-08-12 阅读次数:6644
收藏此教程
16.GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
效果图:
解决方案:主要是绑定后过滤
1
GridView1.DataBind();
2
for
(
int
i
=
0
; i
<=
GridView1.Rows.Count
-
1
; i
++
)
3
{
4
DataRowView mydrv
=
myds.Tables[
"
飞狐工作室
"
].DefaultView[i];
5
string
score
=
Convert.ToString(mydrv[
"
起薪
"
]);
6
if
(Convert.ToDouble(score)
<
34297.00
)
//
大家这里根据具体情况设置可能ToInt32等等
7
{
8
GridView1.Rows[i].Cells[
4
].BackColor
=
System.Drawing.Color.Red;
9
}
10
}
11
sqlcon.Close();
全部后台代码:
1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Web;
5
using
System.Web.Security;
6
using
System.Web.UI;
7
using
System.Web.UI.WebControls;
8
using
System.Web.UI.WebControls.WebParts;
9
using
System.Web.UI.HtmlControls;
10
using
System.Data.SqlClient;
11
using
System.Drawing;
12
public
partial
class
Default7 : System.Web.UI.Page
13
{
14
SqlConnection sqlcon;
15
SqlCommand sqlcom;
16
string
strCon
=
"
Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa
"
;
17
protected
void
Page_Load(
object
sender, EventArgs e)
18
{
19
if
(
!
IsPostBack)
20
{
21
bind();
22
}
23
}
24
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
25
{
26
GridView1.EditIndex
=
e.NewEditIndex;
27
bind();
28
}
29
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
30
{
31
sqlcon
=
new
SqlConnection(strCon);
32
string
sqlstr
=
"
update 飞狐工作室 set 姓名='
"
33
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[
1
].Controls[
0
])).Text.ToString().Trim()
+
"
',家庭住址='
"
34
+
((TextBox)(GridView1.Rows[e.RowIndex].Cells[
3
].Controls[
0
])).Text.ToString().Trim()
+
"
' where 身份证号码='
"
35
+
GridView1.DataKeys[e.RowIndex].Value.ToString()
+
"
'
"
;
36
sqlcom
=
new
SqlCommand(sqlstr, sqlcon);
37
sqlcon.Open();
38
sqlcom.ExecuteNonQuery();
39
sqlcon.Close();
40
GridView1.EditIndex
=
-
1
;
41
bind();
42
}
43
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
44
{
45
GridView1.EditIndex
=
-
1
;
46
bind();
47
}
48
public
void
bind()
49
{
50
string
sqlstr
=
"
select top 10 * from 飞狐工作室
"
;
51
sqlcon
=
new
SqlConnection(strCon);
52
SqlDataAdapter myda
=
new
SqlDataAdapter(sqlstr, sqlcon);
53
DataSet myds
=
new
DataSet();
54
sqlcon.Open();
55
myda.Fill(myds,
"
飞狐工作室
"
);
56
GridView1.DataSource
=
myds;
57
GridView1.DataKeyNames
=
new
string
[]
{
"
身份证号码
"
}
;
58
GridView1.DataBind();
59
for
(
int
i
=
0
; i
<=
GridView1.Rows.Count
-
1
; i
++
)
60
{
61
DataRowView mydrv
=
myds.Tables[
"
飞狐工作室
"
].DefaultView[i];
62
string
score
=
Convert.ToString(mydrv[
"
起薪
"
]);
63
if
(Convert.ToDouble(score)
<
34297.00
)
//
大家这里根据具体情况设置可能ToInt32等等
64
{
65
GridView1.Rows[i].Cells[
4
].BackColor
=
System.Drawing.Color.Red;
66
}
67
}
68
sqlcon.Close();
69
}
70
}
前台代码:
1
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
2
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
3
<
head
id
="Head1"
runat
="server"
>
4
<
title
>
GridView突出显示某一单元格 清清月儿http://blog.csdn.net/21aspnet
</
title
>
5
</
head
>
6
<
body
>
7
<
form
id
="form1"
runat
="server"
>
8
<
div
>
9
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
CellPadding
="3"
OnRowEditing
="GridView1_RowEditing"
10
OnRowUpdating
="GridView1_RowUpdating"
OnRowCancelingEdit
="GridView1_RowCancelingEdit"
BackColor
="White"
BorderColor
="#CCCCCC"
BorderStyle
="None"
BorderWidth
="1px"
Font-Size
="12px"
>
11
<
FooterStyle
BackColor
="White"
ForeColor
="#000066"
/>
12
<
Columns
>
13
<
asp:CommandField
HeaderText
="编辑"
ShowEditButton
="True"
/>
14
<
asp:BoundField
DataField
="身份证号码"
HeaderText
="编号"
ReadOnly
="True"
/>
15
<
asp:BoundField
DataField
="姓名"
HeaderText
="姓名"
/>
16
<
asp:BoundField
DataField
="出生日期"
HeaderText
="邮政编码"
/>
17
<
asp:BoundField
DataField
="起薪"
HeaderText
="起薪"
DataFormatString
="{0:C}"
HtmlEncode
="false"
/>
18
<
asp:BoundField
DataField
="家庭住址"
HeaderText
="家庭住址"
/>
19
<
asp:BoundField
DataField
="邮政编码"
HeaderText
="邮政编码"
/>
20
21
</
Columns
>
22
<
RowStyle
ForeColor
="#000066"
/>
23
<
SelectedRowStyle
BackColor
="#669999"
Font-Bold
="True"
ForeColor
="White"
/>
24
<
PagerStyle
BackColor
="White"
ForeColor
="#000066"
HorizontalAlign
="Left"
CssClass
="ms-formlabel DataGridFixedHeader"
/>
25
<
HeaderStyle
BackColor
="#006699"
Font-Bold
="True"
ForeColor
="White"
/>
26
</
asp:GridView
>
27
</
div
>
28
</
form
>
29
</
body
>
30
</
html
>
共
3
页 第
1
页
上一页
1
2
3
下一页
相关的教程:
GridView
技巧
GridView 72般绝技
收藏此教程
Currently.-0.05/5
-5
-4
-3
-2
-1
0
1
2
3
4
5
当前平均分:
-2.0
(
2
次打分)
-5
-4
-3
-2
-1
0
1
2
3
4
5
推荐阅读
GridView 72般绝技(一)
GridView 72般绝技(一)
GridView 72般绝技(一)
c#开发-基础知识及有用技巧(一)
GridView 72般绝技(三)
在GridView显示时间列时,设置时间的格式
asp.net 局域网存放文件
扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
GridView 72般绝技(四)
GridView 72般绝技(四)
添加评论
评论主题
您的大名
您的评论
验证码
评论列表
ASP.NET论坛
|
网站帮助
|
加入收藏
知识库搜索:
用户信息
欢迎您,游客。
登录
|
注册
为什么要注册?
马上加入GotAspx,建立自己的知识库,与大家分享您的知识库,还可获得丰厚积分奖励!
本类热门
从零开始学ASP.NET(基础篇)
ASP.NET 2.0轻松实现数据库应用开发
ASP.NET 程序中常用的三十三种代码
GridView 72般绝技(一)
对初学者的建议:ASP.NET技术的学习顺序
单点登录在ASP.NET上的简单实现
ASP.NET数据库编程快速入门之技术慨述
ASP.NET 2.0中构造个性化网页
本类最新
介绍SubSonic【转】
ASP.NET访问XML的例子
WEB开发者版本级别
基于.NET2.0的System.Net.Mail发送邮件Demo
Asp.Net 文件操作基类(读取,删除,批量拷贝,批量删除,写入)
c#生成与 追加xml
一个复杂的Eval()绑定
将服务器上的一个.doc文档另存为到客户端