日期:2014-05-18  浏览次数:20474 次

急,在线等,GridView的一个问题
用GridView显示数据,现在从数据库中读出来的一个字段值是是0或1,那么现在如果是0我想显示“否”,如果是1想显示成“是”!
各位朋友,帮个忙啊!!!
谢谢!!

------解决方案--------------------
或者直接在GridView里写

<asp:TemplateField HeaderText="列标题">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("字段").ToString()="0" ? "否":"是"'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
------解决方案--------------------
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False" DataKeyNames="F_ID" DataSourceID="sqlLink" AllowPaging="True" OnRowEditing="gvList_RowEditing" Width="100%">
<Columns>
<asp:BoundField DataField="Title" HeaderText="链接标题" SortExpression="Title" />
<asp:BoundField DataField="Link" HeaderText="链接地址" SortExpression="Link" />
<asp:BoundField DataField="Target" HeaderText="链接方式" SortExpression="Target" />
<asp:TemplateField HeaderText="审核" SortExpression="Status">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Status").ToString() == "1" ? "已审核" : "未审核" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="End_Date" HeaderText="到期日期" SortExpression="End_Date" DataFormatString="{0:yyyy年MM月dd日}" HtmlEncode="False" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlLink" runat="server" ConnectionString="<%$ ConnectionStrings:WebSiteDB %>" SelectCommand="SELECT * FROM [Link]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
------解决方案--------------------
C# code

protected void GridView1_DataBound(Object sender, GridViewRowEventArgs e)
    {
            for(int i=0;i<GridView1.Rows.Count;i++)
            {
                 GridView1.Rows[i].Cells[列索引].Text= GridView1.Rows[i].Cells[列索引].Text ==0?"否":"是";
            }
    }