【问题标题】:Get current row color in TemplateField?获取 TemplateField 中的当前行颜色?
【发布时间】:2009-03-13 14:26:47
【问题描述】:

我有一种情况,我需要知道 GridView 的 TemplateField 中交替行的当前颜色。

更新:

如何在<%# ??? %> 中检索此颜色值。

(或者我得到行号的解决方法)。

【问题讨论】:

  • 不清楚您希望在何处(即,在什么事件处理程序以及 gridview 数据绑定过程的哪个阶段)检索所述颜色?

标签: c# asp.net templates gridview


【解决方案1】:

在代码隐藏页面(或 .aspx 页面的某个部分)中创建此函数:

protected string GetColor(object container)
        {
            int ordinal = 0;
            try
            {
                ordinal = int.Parse(DataBinder.Eval(container, "DataItemIndex").ToString());
            }
            catch (Exception)
            {
                ordinal = int.Parse(DataBinder.Eval(container, "ItemIndex").ToString());
            }
            return (ordinal % 2) == 0 ? "Row" : "Alternate Row";
        }

然后在您的标记中,您可以这样称呼它:

<%# GetOrdinal(Container) %>

(注意大写的“Container”)。

【讨论】:

    【解决方案2】:

    要从模板字段本身的 标记内部获取颜色,您可以使用此代码...

    <asp:TemplateField>
        <ItemTemplate>
            <%# ((GridViewRow)Container).RowState == DataControlRowState.Alternate ? ((GridView)((GridViewRow)Container).Parent.Parent).AlternatingRowStyle.BackColor : ((GridView)((GridViewRow)Container).Parent.Parent).RowStyle.BackColor%>
        </ItemTemplate>
    </asp:TemplateField>
    

    您也可以在 GridView 的 RowDataBound 事件中执行此操作。在 RowDataBound 命令中,您可以查询 e.Row.RowState 以找出您所在的行的类型。值包括 DataControlRowState.Alternate 和 DataControlRowState.Normal。您可以使用 sender 根据该行类型获取颜色...

    protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
        // set first cell in the row to color just for demonstration purpose.
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
        {
            e.Row.Cells[0].Text = ((GridView)sender).AlternatingRowStyle.BackColor.ToString();
        }
    }
    

    【讨论】:

      【解决方案3】:
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              e.Row.Attributes.Add("style", "cursor:help;");
              if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
              { 
                  if (e.Row.RowType == DataControlRowType.DataRow)
                  {                
                      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
                      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");
                      e.Row.BackColor = Color.FromName("#E56E94");                
                 }           
              }
              else
              {
                  if (e.Row.RowType == DataControlRowType.DataRow)
                  {
                      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
                      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");
                      e.Row.BackColor = Color.FromName("gray");                
                  }
                  //e.Row.Cells[0].BackColor = Color.FromName("gray");
                  //e.Row.Cells[1].BackColor = Color.FromName("gray");
                  //e.Row.Cells[2].BackColor = Color.FromName("gray");
                  //e.Row.Cells[3].BackColor = Color.FromName("gray");
                  //e.Row.Cells[4].BackColor = Color.FromName("gray");
                  //e.Row.BorderWidth = 2;
                  //e.Row.BorderColor = Color.FromName("#43C6DB");
              }
          }
      

      【讨论】:

        【解决方案4】:

        这可能是一个奇怪的解决方法,但您始终可以检查您要检查的行是奇数行还是偶数行。如果是偶数,则很可能使用了 AlternatingRow 模板中设置的颜色。如果是偶数,则应该使用常规的 Row 模板。

        【讨论】:

          【解决方案5】:

          GridView 如何改变颜色 - 每隔一行,还是 X 块?还是以更“随机”的方式设置?

          如果是隔行,您可以简单地检查该行是“正常”行还是“交替”行。

          我不记得具体是怎么写的了,我只找到了这个 VB 示例,但它可能会有所帮助:

          If e.Row.RowState = DataControlRowState.Normal Then
           //do stuff
          ElseIf e.Row.RowState = DataControlRowState.Alternate Then
           //do other stuff
          

          其中 e 是 GridView 对象。不过,这不会检查行的实际颜色。我想您应该能够执行以下操作:

          if(System.Drawing.Color.Red == e.Row.BackColor)
          

          如果可以的话,请详细说明每行的颜色是如何设置的。

          【讨论】:

          • 那么你真的不需要知道颜色,只需要知道行的类型(正常,交替)。如果正常,你知道颜色是#E9CE90,否则是#F6EED5。我的 VB 示例应该对您有所帮助。
          • 从一开始就在问题中提供的典型信息。没关系,我认为斯科特只是给了你你需要的东西。
          猜你喜欢
          • 2010-11-26
          • 2015-05-18
          • 1970-01-01
          • 1970-01-01
          • 2019-02-01
          • 2021-01-24
          • 2017-11-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多