GridView性别列在数据库是bool型字段true or false,如何让其在页面中显示为男女?

来源:互联网 发布:java io继承图 编辑:程序博客网 时间:2024/05/22 04:26

上面的图片是GridView绑定后台数据库的部分截图。看到第四列,性别这一列,在后台数据库的数据类型是布尔型的,值为true 或者 false,定义true时为男,false时为女。。。

今天所说的就是,从后台数据库读取性别的时候,在浏览器页面上让性别列显示为男女而不是true和false。

很多人都说了,直接在查询语句里面写不就OK了,是的,这样的方法可行。

而我今天要用GridView里面自带的绑定事件RowDataBound来实现。。。

页面前台代码,我就不多写了(大致差不多),只写性别这一列作为示例:

<asp:BoundField DataField="Sex" HeaderText="性别"             SortExpression="Sex" ItemStyle-HorizontalAlign="Center" ><ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>        </asp:BoundField>

 这是绑定数据的前提条件,注意属性DataField和SortExpression,如果没有的话,你即便是写了查询语句也不能绑定后台数据到页面上。

准备以上前台页面后,触发GridView的事件RowDataBound,后台代码如下:

 protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)    {        if (e.Row.RowType == DataControlRowType.DataRow)        {            //e.Row.Cells[0].Text = e.Row.DataItemIndex.ToString();            if (e.Row.Cells[3].Text == "True")            {                e.Row.Cells[3].Text = "";            }            if (e.Row.Cells[3].Text.ToString() == "False")            {                e.Row.Cells[3].Text = "";            }        }    }

相信,这段代码你们都看的懂,不多说了,这样就可显示性别列为男女。。。

没有什么技术含量,也不是什么创新代码,发表只是对于需要的你们了解了解这个gridView_RowDataBound事件的用法。

一家之言,仅供参考!!!


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>