zoukankan
html css js c++ java
DataGrid的多种格式化显示方法
//
1.用<%# DataBinder.Eval(Container.DataItem,"MaritalStatus").ToString()=="1"?"已婚":"未婚" %>方式显示
//
2.用sql中的case Gender when 1 then '男' else '女' end显示
//
3.在后台写方法,前台调用显示,如Format(object item,string type)
//
4.DataGrid1_ItemDataBound
1.html
<
HTML
>
<
HEAD
>
<
title
>
GridFormat
</
title
>
<
meta
content
="Microsoft Visual Studio .NET 7.1"
name
="GENERATOR"
>
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
</
HEAD
>
<
body
MS_POSITIONING
="GridLayout"
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
asp:datagrid
id
="DataGrid1"
style
="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px"
runat
="server"
AutoGenerateColumns
="False"
BorderColor
="#CC9966"
BorderStyle
="None"
BorderWidth
="1px"
BackColor
="White"
CellPadding
="4"
>
<
FooterStyle
ForeColor
="#330099"
BackColor
="#FFFFCC"
></
FooterStyle
>
<
SelectedItemStyle
Font-Bold
="True"
ForeColor
="#663399"
BackColor
="#FFCC66"
></
SelectedItemStyle
>
<
ItemStyle
ForeColor
="#330099"
BackColor
="White"
></
ItemStyle
>
<
HeaderStyle
Font-Bold
="True"
ForeColor
="#FFFFCC"
BackColor
="#990000"
></
HeaderStyle
>
<
Columns
>
<
asp:TemplateColumn
HeaderText
="ID"
>
<
ItemTemplate
>
<%
# DataBinder.Eval(Container.DataItem,
"
UserID
"
)
%>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="学位"
>
<
ItemTemplate
>
<%
# Format(Container.DataItem,
"
Degree
"
)
%>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="性别"
>
<
ItemTemplate
>
<%
# DataBinder.Eval(Container.DataItem,
"
Gender
"
)
%>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="婚姻状况"
>
<
ItemTemplate
>
<%
# DataBinder.Eval(Container.DataItem,
"
MaritalStatus
"
).ToString()
==
"
1
"
?
"
已婚
"
:
"
未婚
"
%>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="描述"
>
<
ItemTemplate
>
<%
# DataBinder.Eval(Container.DataItem,
"
Description
"
)
%>
</
ItemTemplate
>
</
asp:TemplateColumn
>
</
Columns
>
<
PagerStyle
HorizontalAlign
="Center"
ForeColor
="#330099"
BackColor
="#FFFFCC"
></
PagerStyle
>
</
asp:datagrid
></
form
>
</
body
>
</
HTML
>
2.cs代码
public
class
GridFormat : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.DataGrid DataGrid1;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
1.用<%# DataBinder.Eval(Container.DataItem,"MaritalStatus").ToString()=="1"?"已婚":"未婚" %>方式显示
//
2.用sql中的case Gender when 1 then '男' else '女' end显示
//
3.在后台写方法,前台调用显示,如Format(object item,string type)
//
4.DataGrid1_ItemDataBound
DataBind();
for
(
int
i
=
0
; i
<
DataGrid1.Items.Count; i
++
)
{
if
(DataGrid1.Items[i].Cells[
2
].Text
==
"
男
"
)
{
DataGrid1.Items[i].Cells[
2
].ForeColor
=
System.Drawing.Color.Red;
}
}
}
GetDataSet
#region
GetDataSet
private
DataSet GetDataSet(
string
sql)
{
string
constring
=
System.Configuration.ConfigurationSettings.AppSettings[
"
ConnectionString
"
];
SqlDataAdapter sda
=
new
SqlDataAdapter(sql,constring);
DataSet ds
=
new
DataSet();
sda.Fill(ds);
return
ds;
}
#endregion
DataBind
#region
DataBind
private
void
DataBind()
{
string
sql
=
"
select UserID,Degree,Gender =case Gender when 1 then '男' else '女' end,MaritalStatus,Description from formatgrid
"
;
DataSet ds
=
GetDataSet(sql);
this
.DataGrid1.DataSource
=
ds;
this
.DataGrid1.DataBind();
}
#endregion
Format
#region
Format
protected
string
Format(
object
item,
string
type)
{
DataRowView drv
=
(DataRowView)item;
string
val
=
string
.Empty;
switch
(drv[
"
Degree
"
].ToString().ToLower())
{
case
"
1
"
:
val
=
"
博士后
"
;
break
;
case
"
2
"
:
val
=
"
博士
"
;
break
;
case
"
3
"
:
val
=
"
硕士
"
;
break
;
case
"
4
"
:
val
=
"
学士
"
;
break
;
default
:
val
=
drv[
"
Degree
"
].ToString();
break
;
}
return
val;
}
#endregion
Web Form Designer generated code
#region
Web Form Designer generated code
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base
.OnInit(e);
this
.DataGrid1.ItemDataBound
+=
new
DataGridItemEventHandler(DataGrid1_ItemDataBound);
}
/**/
/**/
/**/
/**/
/**/
/**/
/**/
///
<summary>
///
Required method for Designer support - do not modify
///
the contents of this method with the code editor.
///
</summary>
private
void
InitializeComponent()
{
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
DataGrid1_ItemDataBound
#region
DataGrid1_ItemDataBound
private
void
DataGrid1_ItemDataBound(
object
sender, DataGridItemEventArgs e)
{
if
(e.Item.ItemType
!=
ListItemType.Header)
{
string
subject
=
Convert.ToString(DataBinder.Eval(e.Item.DataItem,
"
Description
"
));
if
(subject.Length
>
10
)
{
subject
=
subject.Substring(
0
,
10
)
+
""
;
}
e.Item.Cells[
4
].Text
=
subject;
}
if
(e.Item.ItemType
!=
ListItemType.Header)
{
e.Item.Cells[
1
].Attributes.Add(
"
onmouseover
"
,
"
this.style.color='Red';this.style.cursor='hand'
"
);
e.Item.Cells[
1
].Attributes.Add(
"
onmouseout
"
,
"
this.style.color='Black';this.style.cursor='default'
"
);
}
}
#endregion
}
3.数据库教本
if
exists
(
select
*
from
dbo.sysobjects
where
id
=
object_id
(N
'
[dbo].[FormatGrid]
'
)
and
OBJECTPROPERTY
(id, N
'
IsUserTable
'
)
=
1
)
drop
table
[
dbo
]
.
[
FormatGrid
]
GO
CREATE
TABLE
[
dbo
]
.
[
FormatGrid
]
(
[
UserID
]
[
int
]
NULL
,
[
Degree
]
[
int
]
NULL
,
[
Gender
]
[
int
]
NULL
,
[
MaritalStatus
]
[
int
]
NULL
,
[
Description
]
[
nvarchar
]
(
500
) COLLATE Chinese_PRC_CI_AS
NULL
)
ON
[
PRIMARY
]
GO
--
测试数据
insert
into
FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)
values
(
1
,
1
,
1
,
0
,
'
可以读取流。读取是从流到数据结构(如字节数组)的数据传输。
'
)
insert
into
FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)
values
(
2
,
2
,
0
,
1
,
'
可以写入流。写入是从数据结构到流的数据传输。
'
)
insert
into
FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)
values
(
3
,
3
,
1
,
0
,
'
流可以支持查找。查找是对流内的当前位置进行查询和修改。查找功能取决于流具有的后备存储区类型。例如,网络流没有当前位置的统一概念,因此一般不支持查找。
'
)
insert
into
FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)
values
(
4
,
4
,
0
,
1
,
'
小山哥
'
)
insert
into
FormatGrid(UserID,Degree,Gender,MaritalStatus,Description)
values
(
5
,
5
,
1
,
0
,
'
山哥
'
)
查看全文
相关阅读:
时间形式的转换
vue 按enter键 进行搜索或者表单提交
关于Cookie 关于前端存储数据
复杂数组去重
蜜蜂
MongoDB学习记录一
python 基础 day03—函数
python 基础 day03—文件操作
python 基础 day02—列表List / 元组Tuple
python 基础 day02—初识模块
原文地址:https://www.cnblogs.com/Fooo/p/407827.html
最新文章
QT中QWS的含义 (转至 宋金时的专栏
init.sh 学习(转
关于QGraphicsScene 和 QGraphicsView 和 QDialog 的杂乱笔记【或说指针复习。。】
Qt msgBox 快捷版
CodeceptJS学习笔记-入门01
VMware Workstation Pro 启动虚拟机报错,您的主机不满足在启用 Hyper-V 或 Device/Credential Guard 的情况下运行 VMware Workstation 的最低要求
golang 如何使用beego实现配置Redis和MySQL等连接方式的集中配置
visual studio code的终端命令窗口替换成conda的环境
selenium webdriver 学习笔记(2)-关于内嵌页面frame的切换和导出文件时浏览器弹出窗口的操作
selenium webdriver 学习笔记(1)
热门文章
ubuntu下安装rubymine
Java并发编程-阻塞队列
ruby开发环境搭建
Java实现二分查找
排序算法小结
面试题之去重
面试题之运行顺序
js加减乘除精度问题
前端 对身份证号和手机号的验证及模糊处理
修改gitLab 密码后, sourcetree无法推送
Copyright © 2011-2022 走看看