如何为 GridView 中的特定行指定 CSS 类? [英] How do I specify CSS classes for specific rows in a GridView?

查看:29
本文介绍了如何为 GridView 中的特定行指定 CSS 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 创建一个 SharePoint Web 部件,其中一部分将 GridView 控件输出到页面.虽然我可以通过设置 GridView 本身的 CSS 类来对其显示方式进行相当广泛的控制,但我真正想做的是能够为某些特定的 td 元素指定类.我不确定如何执行此操作,或者是否会在 GridView 填充行时或将 GridView 添加到页面时完成.

I am creating a SharePoint web part in C# and part of it outputs a GridView control to the page. While I can get fairly extensive control over the way it is displayed by setting the CSS class of the GridView itself, what I would really like to do is be able to specify classes to certain specific td elements. I'm not sure how to go about doing this, or if it would be done at the time that the GridView is being populated with rows, or at the time the GridView is added to the page.

在伪代码中,我基本上设想的是能够说类似 gridView.Row[4].CssClass = "header" 的内容,这将设置第五行的 tdGridView 到类标题".

In pseudocode, what I had essentially envisioned was to be able to say something like gridView.Row[4].CssClass = "header", which would set the td of the fifth row in the GridView to the class "header."

我研究过使用 RowDataBound 事件,所以我只使用以下内容来测试它:

I've looked into using the RowDataBound event, so I just used the following to test it:

protected void outputGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.CssClass = "outputHeader";
}

这可能是我对如何正确使用它的误解,但它似乎没有任何作用.我认为它会将所有行设置为标题"类,如果有,我将从那里开始处理我的逻辑,但我什至无法让它工作.感谢任何人提供的任何帮助!

It's probably my misunderstanding of how to use that properly, but it doesn't appear to do anything. I thought it would set all of the rows to the class "header," and if it had, I was going to work on my logic from there, but I can't even get that to work. Thanks for any help anyone can provide!

推荐答案

我对 RowDataBound 做了类似的事情:

I do something similar with RowDataBound:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    // Check the XXXX column - if empty, the YYYY needs highlighting!
    if (e.Row.Cells[6].Text == " ")
    {
       e.Row.CssClass = "highlightRow"; // ...so highlight it
    }
}

检查您所做的事情是否正确的一种方法是通过浏览器监控您的 html 输出......像 Firebug 这样的东西真的很有帮助.

One way to check that what you are doing is correct is to monitor your html output via the browser... something like Firebug really helps.

这是一些示例 CSS,我们将 CssClass 'dataGrid' 分配给网格:

Here's some sample CSS, where we assign the CssClass 'dataGrid' to the Grid:

/* Used to highlight rows */
table.dataGrid tr.highlightRow td
{
    background-color: #FF6666;
    border-bottom: 1px solid #C0C0FF;
}

更新: 连接所有这些:我在 aspx 页面上使用自动连接.您的页面声明如下所示:

Update: Wiring all this up: I use auto-wire-up on the aspx page. Your page declaration looks something like this:

<%@ Page Language="C#" MasterPageFile="~/XXXXXX.master" AutoEventWireup="true" CodeBehind="YYYY.aspx.cs" Inherits="ZZZ.ZZZ.AAAAAA" Title="View Blah" %>

页面上的此设置允许您使用 UI 连接事件.单击网格,选择属性,单击闪电图标,然后在 RowDataBound 事件下,选择您的方法.所有这一切在幕后都是为 DataGridView 添加一个属性,因此:

This setting on the page allows you to use the UI to connect up the events. Click the grid, select the properties, click the lightning-strike icon, and under the RowDataBound event, select your method. All this does behind the scenes is add an attribute to the DataGridView, thus:

        <asp:GridView ID="uiActionGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False"
        OnRowDataBound="uiActionGridView_RowDataBound" OnDataBound="uiActionGridView_DataBound">

  • 这显示了两个正在连接的事件,DataBound 和 RowDataBound 事件.
  • 这就是我使用 VS2005 所做的事情,而且这一切似乎都正常工作".我认为您遇到的唯一一件事是您正在 数据绑定发生后手动绑定事件.

    This is what I do using VS2005 and it all seems to 'just work'. The only thing that I can think you are experiencing is that you are manually binding the event after the databind has occurred.

    这篇关于如何为 GridView 中的特定行指定 CSS 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆