[英]How do I replace a checkbox in gridview with text?
I have the following gridview: 我有以下gridview:
<asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport">
<Columns>
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone">
<ControlStyle Width="250px" />
</asp:BoundField>
<asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
</Columns>
</asp:GridView>
The second row is a bool in the database, but I do not want to show a checkbox or true\\false to the users. 第二行是数据库中的布尔值,但我不想向用户显示复选框或true \\ false。
How do I display something like this instead? 如何显示类似这样的内容? 0 = Don't Call 1 = Call Us
0 =不打电话1 =打电话给我们
You could create a TemplateField instead of a BoundField. 您可以创建一个TemplateField而不是BoundField。
<asp:TemplateField HeaderText="Whatever">
<ItemTemplate>
<asp:Literal ID="litTextValue" runat="server" />
</ItemTemplate>
</asp:TemplateField>
You could then put some code inline to display the text that you want or handle the RowDataBound event to do the logic there. 然后,您可以内联一些代码以显示所需的文本,或处理RowDataBound事件以执行此处的逻辑。
I ended up just using OnRowDataBound for this. 我最终只是为此使用OnRowDataBound。
<asp:GridView ID="gdvReport" runat="server" AutoGenerateColumns="False" DataSourceID="sdseport" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone">
<ControlStyle Width="250px" />
</asp:BoundField>
<asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
</Columns>
</asp:GridView>
protected void OnRowDataBound(object sender, EventArgs e)
{
GridViewRowEventArgs ea = e as GridViewRowEventArgs;
if (ea.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = ea.Row.DataItem as DataRowView;
Object ob = drv["Phone"];
if (!Convert.IsDBNull(ob))
{
bool iParsedValue = false;
if (bool.TryParse(ob.ToString(), out iParsedValue))
{
TableCell cell = ea.Row.Cells[1];
if (iParsedValue == false)
{
cell.Text = "Don't Call";
}
else
{
cell.Text = "Call Us";
}
}
}
}
}
And it is working great now. 现在它运行良好。
You should do it in the sql instead of doing it here using a CASE statement such as 您应该在sql中执行此操作,而不是使用CASE语句在此处执行操作,例如
CASE ToCall WHEN '1' THEN 'Call' ELSE 'Do not call' END AS ToCall
and then use a simple bound field such as 然后使用一个简单的绑定字段,例如
<asp:BoundField DataField="ToCall" HeaderText="Foramt" SortExpression="ToCall" />
I did this and it work 我做到了并且有效
<asp:Literal ID="isActive" runat="server"
Text='<%#Eval("isActive")==DBNull.Value ?
"inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive"
%>'></asp:Literal>
This is the important part. 这是重要的部分。
Text='<%#Eval("isActive")==DBNull.Value?"inactive":Convert.ToBoolean(Eval("isActive"))?"active":"inactive" %>' Text ='<%#Eval(“ isActive”)== DBNull.Value?“ inactive”:Convert.ToBoolean(Eval(“ isActive”))?“ active”:“ inactive”%>'
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.