1

在我的网站中,我有一个带有 Gridview 的页面,用于显示一些数据。我捕获 RowDataBound 事件,以确定单元格中是否存在某些文本。如果是,我把它涂成绿色,否则我把它涂成红色。

这是问题所在:Gridview 只有水平网格线。当我更改 RowDataBound 中单元格的颜色时(我实际上是在更改类),网格线采用应用的颜色。无论我尝试什么(循环遍历所有单元格并设置边框颜色),我都无法将其还原。请帮忙。

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 2; i <= 3; i++)
        {
            if (e.Row.Cells[i].Text.Contains("monkey"))
            {
                e.Row.Cells[i].Attributes.Add("class", "monkey bold");
            }
            else
            {
                e.Row.Cells[i].Attributes.Add("class", "nomonkey bold");
            }
        }
    }

}

样式如下:

.monkey
{
    color: #009900;
    border-color: black;
}

.nomonkey
{
    color: red;
    border-color: black;
}

边框颜色属性似乎没有效果。

GridView 定义为:

                        <asp:GridView ID="GridView2" runat="server" AllowSorting="False" GridLines="Horizontal" AutoGenerateColumns="false" CellPadding="4" OnRowDataBound="GridView2_RowDataBound" OnDataBound="GridView2_DataBound" CssClass="reportGrid"> 
                        <FooterStyle BackColor="#2F76B8" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#2F76B8" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#FFFFFF" ForeColor="#222222" HorizontalAlign="Center" />
4

2 回答 2

2

我在任何地方都找不到答案,也无法诱使任何人回答它,所以我通过在单元格内添加一个跨度来解决它,并将其样式设置如下:

            if (e.Row.Cells[i].Text.Contains("monkey"))
            {
                e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("monkey", "<span class=\"monkey\">monkey</span> ");
            }
于 2013-02-02T01:18:32.850 回答
0

我也经历过同样的事情。另一种方法是添加一个标签控件并在其上设置属性。

不要使用 BoundField,而是使用 TemplateField。假设您的数据返回一个可索引的项目:

<asp:GridViewControl runat="server" ID="GridView2" AutoGenerateColumns="false">
    <asp:BoundField HeaderText="Field0" DataField="[0]" />
    <asp:BoundField HeaderText="Field1" DataField="[1]" />
    <asp:TemplateField HeaderText="Monkey1" />
    <asp:TemplateField HeaderText="Monkey2" />
</asp:GridViewControl>

然后在代码隐藏中:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var data_item = e.Row.DataItem; // Can use "as <type>;" if you know the type.
        if (data_item != null)
        {
            for (int i = 2; i <= 3; i++)
            {
                var cell_content = new Label();
                e.Row.Cells[i].Controls.Add(cell_content);

                cell_content.Text = data_item[i];
                if (data_item[i].Contains("monkey"))
                {
                    cell_content.Attributes.Add("class", "monkey bold");
                }
                else
                {
                    cell_content.Attributes.Add("class", "nomonkey bold");
                }
            }
        }

当然,另一种方法是在 TemplateField -> ItemTemplate 声明中添加带有 ID 的标签并使用“Cells[i].FindControl("label_id")”。

于 2013-11-05T14:59:26.520 回答