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

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

问题描述

我创造了C#和一部分的SharePoint Web部件的输出一个GridView控件到页面上。虽然我可以通过它可以通过设置CSS类GridView控件本身,我真的很想做的显示方式得到相当广泛的控制是可以指定类某些特定的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.

在伪code,我已经基本上设想是能够说类似 gridView.Row [4] .CssClass =头,其中将设置第五行的TD在GridView到类头。

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输出的一种方式......像萤火虫确实有帮助。

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属性的数据网格分配到网格:

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">


  • 这说明两个事件是有线起来,数据绑定和事件的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天全站免登陆