方法一:
在CS文件内使用RowDataBound事件
protected
void
gvCourseChoosed_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
string
s
=
e.Row.Cells[
3
].Text;
e.Row.Cells[
3
].Text
=
(s
==
"
0
"
?
"
男
"
:
"
女
"
);
}
}
方法二:
直接在aspx文件内写
<
asp:TemplateField HeaderText
=
"
性别:
"
>
<
EditItemTemplate
>
<
asp:DropDownList ID
=
"
DropDownList1
"
runat
=
"
server
"
SelectedValue
=
'
<%# Bind("TeacherSex") %>
'
>
<
asp:ListItem Value
=
"
0
"
>
男
</
asp:ListItem
>
<
asp:ListItem Value
=
"
1
"
>
女
</
asp:ListItem
>
</
asp:DropDownList
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
'
<%# (Eval("TeacherSex").ToString()=="0")?"男":"女" %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField HeaderText
=
"
出生年月:
"
>
<
EditItemTemplate
>
<
dxe:ASPxDateEdit ID
=
"
ASPxDateEdit1
"
runat
=
"
server
"
Value
=
'
<%# Bind("TeacherBirth") %>
'
>
</
dxe:ASPxDateEdit
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label2
"
runat
=
"
server
"
Text
=
'
<%# Bind("TeacherBirth", "{0:d}") %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
本文介绍了两种在ASP.NET中优化GridView组件显示数据的方法:一种是在CS文件内通过RowDataBound事件实现,另一种是在aspx文件中直接进行配置。这两种方法可以帮助开发者更灵活地展示性别等枚举类型的字段。
744

被折叠的 条评论
为什么被折叠?



