1

私の Web サイトには、いくつかのデータを表示するために使用する 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;
}

border-color プロパティは効果がないようです。

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

0

私も同じことを経験しました。別の方法として、Label コントロールを追加し、代わりにそのプロパティを設定することもできます。

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 に Label を追加することもできます -> ItemTemplate 宣言と ID を使用し、"Cells[i].FindControl("label_id")" を使用します。

于 2013-11-05T14:59:26.520 に答える