English 中文(简体)
如何在 asp: grid 中第一行的颜色
原标题:How to color the first row in a asp:grid
  • 时间:2012-05-23 09:22:59
  •  标签:
  • c#
  • asp.net

早上好/下午所有,

我有一个 ASP:grid, 显示当前版本的术语和条件, 我们目前使用/ 使用在网格中第一行的术语和条件, 总是我们目前使用的版本, 我需要突出显示该行, 但难以突出显示

在此显示 asp: gridview

<asp:GridView runat="server" ID="grvTermsAndConditions" AutoGenerateColumns="false"
                    OnRowDataBound="grvTermsAndConditions_rowDataBound" Style="margin-bottom: 20px;">
                    <Columns>
                        <asp:TemplateField HeaderText="CurrentVersion">
                            <ItemTemplate>
                                <asp:Label ID="lblVersion" CssClass="gvItem" runat="server" Text= <%# DataBinder.Eval(Container, "DataItem.CurrentVersion") %> ></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Added">
                            <ItemTemplate>
                                <asp:Label ID="lblDateAdded" CssClass="gvItem" runat="server" Text= <%# DataBinder.Eval(Container, "DataItem.Added") %> ></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="CreatedBy">
                            <ItemTemplate>
                                <asp:Label ID="lblCreatedBy" CssClass="gvItem" runat="server" Text= <%# DataBinder.Eval(Container, "DataItem.CreatedBy") %> ></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

这是我在暗号后面的代码 我试图把第一排的颜色变成红色

protected void grvTermsAndConditions_rowDataBound(Object sender, GridViewRowEventArgs e)
        {

            for (int i = 0; i < grvTermsAndConditions.Rows.Count; ++i )
            {
                if (i == 0)
                {
                    e.Row.CssClass = "gvRowRed";
                    e.Row.Cells[0].CssClass = "white";
                    e.Row.Cells[1].CssClass = "white";

                }
            }
        }

但每次我跑这行,第二排就染上彩色?

任何帮助都将不胜感激。

最佳回答

请注意, < a href=> http://msdn.microsoft.com/ en- us/library/ system.web. ui.webcontrols. gridview.rowdatabound" rel="nofollow"\\ code>RowDataBound 事件为每行执行 - 您正在做的是, 当您将每行捆绑时, 环绕所有行。 对性能非常糟糕 。

尝试此 :

protected void grvTermsAndConditions_rowDataBound(Object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowIndex == 0)
        {
            e.Row.CssClass = "gvRowRed";
            e.Row.Cells[0].CssClass = "white";
            e.Row.Cells[1].CssClass = "white";

        }
}
问题回答

试这个

protected void grvTermsAndConditions_rowDataBound(Object sender, GridViewRowEventArgs e)
    {
             if(e.Row.RowType == DataControlRowType.DataRow) // Use this if condition otherwise exception is occurred
             {
                      if(e.RowIndex == 0)
                      {
                             e.Row.CssClass = "gvRowRed";
                e.Row.Cells[0].CssClass = "white";
                e.Row.Cells[1].CssClass = "white";
                      }
             }            
    }

为了表现起见,我建议使用网格视图数据包活动,因为你只想要改变一行:

protected void grvTermsAndConditions_DataBound(object sender, EventArgs e)
    {
        GridViewRow firstRow = grvTermsAndConditions.Rows[0];
        firstRow.CssClass = "gvRowRed";
        //etc.
    }

因此,你不必环绕所有行 以大网格视图可以变得有趣。

排集是零基的,所以第一排应该是 I = 0,而不是 I = = = 1 。虽然这似乎也是个坏主意,

我在这里的一般性建议是使用CSS中的第一胎假类来为第一排染色。 http://www.w3schools.com/csref/sel_firstchild.asp





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!