上次准备做一个GridView使用SqlDataSource 的简单案例 ,由于发现用
SqlDataSource做数据源直接填充GridView的时候,
如果是数据库中的Bit列,它会
默认显示一个CheckBox ,并且显示的是"True"或"False".
于是想到让它和Label , DropDownList 控件相结合 ,实现姓别的不同显示方法
效果如下:
默认状态:

更新状态:

思路: 在数据库中将性别列设置为bit型(只有0和1),用0和1分别表示男和女;
数据库的如下:

前台代码如下:
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
DataKeyNames
=
"
UID
"
DataSourceID
=
"
SqlDataSource1
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
OnRowUpdating
=
"
GridView1_RowUpdating
"
CellPadding
=
"
4
"
ForeColor
=
"
#333333
"
GridLines
=
"
None
"
>
<
Columns
>
<
asp:BoundField DataField
=
"
UID
"
HeaderText
=
"
UID
"
InsertVisible
=
"
False
"
ReadOnly
=
"
True
"
SortExpression
=
"
UID
"
/>
<
asp:BoundField DataField
=
"
UName
"
HeaderText
=
"
UName
"
SortExpression
=
"
UName
"
/>
<
asp:BoundField DataField
=
"
UAddr
"
HeaderText
=
"
UAddr
"
SortExpression
=
"
UAddr
"
/>
<
asp:TemplateField HeaderText
=
"
Sex
"
>
<
EditItemTemplate
>
<
asp:DropDownList ID
=
"
editdrop
"
runat
=
"
server
"
Style
=
"
position: relative
"
SelectedValue
=
'
<%# Bind("USex", "{0}") %>
'
>
<
asp:ListItem Selected
=
"
True
"
Value
=
"
False
"
>
男
</
asp:ListItem
>
<
asp:ListItem Value
=
"
True
"
>
女
</
asp:ListItem
>
</
asp:DropDownList
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
'
<%# Eval("Usex", "{0}") %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:CommandField ShowEditButton
=
"
True
"
/>
</
Columns
>
<
FooterStyle BackColor
=
"
#507CD1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
RowStyle BackColor
=
"
#EFF3FB
"
/>
<
EditRowStyle BackColor
=
"
#2461BF
"
/>
<
SelectedRowStyle BackColor
=
"
#D1DDF1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
#333333
"
/>
<
PagerStyle BackColor
=
"
#2461BF
"
ForeColor
=
"
White
"
HorizontalAlign
=
"
Center
"
/>
<
HeaderStyle BackColor
=
"
#507CD1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
AlternatingRowStyle BackColor
=
"
White
"
/>
</
asp:GridView
>
<
asp:SqlDataSource ID
=
"
SqlDataSource1
"
runat
=
"
server
"
ConnectionString
=
"
<%$ ConnectionStrings:TempConnectionString %>
"
SelectCommand
=
"
SELECT * FROM [User1]
"
DeleteCommand
=
"
DELETE FROM [User1] WHERE [UID] = @UID
"
InsertCommand
=
"
INSERT INTO [User1] ([UName], [Usex], [UAddr]) VALUES (@UName, @Usex, @UAddr)
"
UpdateCommand
=
"
UPDATE [User1] SET [UName] = @UName, [Usex] = @Usex, [UAddr] = @UAddr WHERE [UID] = @UID
"
>
<
DeleteParameters
>
<
asp:Parameter Name
=
"
UID
"
Type
=
"
Int32
"
/>
</
DeleteParameters
>
<
UpdateParameters
>
<
asp:Parameter Name
=
"
UName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
Usex
"
Type
=
"
Boolean
"
/>
<
asp:Parameter Name
=
"
UAddr
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
UID
"
Type
=
"
Int32
"
/>
</
UpdateParameters
>
<
InsertParameters
>
<
asp:Parameter Name
=
"
UName
"
Type
=
"
String
"
/>
<
asp:Parameter Name
=
"
Usex
"
Type
=
"
Boolean
"
/>
<
asp:Parameter Name
=
"
UAddr
"
Type
=
"
String
"
/>
</
InsertParameters
>
</
asp:SqlDataSource
>
主要是添加一个模板列(代码如下):
<asp:TemplateField HeaderText="Sex">
<EditItemTemplate>
<asp:DropDownList ID="editdrop" runat="server" Style="position: relative" SelectedValue='<%# Bind("USex", "{0}") %>'>
<asp:ListItem Selected="True" Value="False">男</asp:ListItem>
<asp:ListItem Value="True">女</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Usex", "{0}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
注意:
1.需要将ListItem的Value属性设置为"False"或"True",这,就可以让它对应数据库中的BIt列.
2.使用Bind方法进行双向绑定,以便能够更新数据库.
因为在创建gridView控件时,必须先为GridView的每一行创建一个GridViewRow对象,创建每一行时,将引发一个RowCreated事件;
当行创建完毕,每一行GridViewRow就要绑定数据源中的数据,当绑定完成后,将引发RowDataBound事件。
如果说我们可以利用RowCreated事件来控制每一行绑定的控件,那么我们同样可以利用RowDataBound事件来控制每一行绑定的数据,也就是让数据如何呈现给大家。
因为需要将默认的"True","False"显示为"男"或"女"
所以有如下的后台代码:
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)

...
{
if (e.Row.RowType == DataControlRowType.DataRow)

...{ //用FindControl方法找到模板中的Label控件
Label lb1 = (Label)e.Row.FindControl("Label1");
if (lb1 == null) ;//在编辑的时候控件变为DropDownList

//因为RowDataBound是发生在数据绑定之后,所以我们可以
//判断Label绑定的数据,如果是True,就更改其text属性为男
else

...{
if (lb1.Text == "False")
lb1.Text = "男";
else
lb1.Text = "女";
}

}
}
好了,基本上就OK了
PS:如有错误,敬请指正