如何为 GridView 中的特定行指定 CSS 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I specify CSS classes for specific rows in a GridView?
提问by Geo Ego
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.
我正在用 C# 创建一个 SharePoint Web 部件,其中一部分将 GridView 控件输出到页面。虽然我可以通过设置 GridView 本身的 CSS 类来对其显示方式进行相当广泛的控制,但我真正想做的是能够为某些特定的 td 元素指定类。我不确定如何去做,或者是否会在 GridView 填充行时完成,或者在 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."
在伪代码中,我基本上设想的是能够说出类似gridView.Row[4].CssClass = "header"
,这会将 GridView 中第五行的 td 设置为类“标题”。
I've looked into using the RowDataBound event, so I just used the following to test it:
我研究过使用 RowDataBound 事件,所以我只使用以下内容来测试它:
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!
这可能是我对如何正确使用它的误解,但它似乎没有做任何事情。我认为它会将所有行设置为“标题”类,如果有,我将从那里开始处理我的逻辑,但我什至无法让它工作。感谢任何人可以提供的任何帮助!
回答by Nij
I do something similar with RowDataBound:
我对 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
}
}
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.
检查您正在做的事情是否正确的一种方法是通过浏览器监控您的 html 输出......像 Firebug 这样的东西真的很有帮助。
Here's some sample CSS, where we assign the CssClass 'dataGrid' to the Grid:
这是一些示例 CSS,我们将 CssClass 'dataGrid' 分配给网格:
/* Used to highlight rows */
table.dataGrid tr.highlightRow td
{
background-color: #FF6666;
border-bottom: 1px solid #C0C0FF;
}
Update:Wiring all this up: I use auto-wire-up on the aspx page. Your page declaration looks something like this:
更新:连接所有这些:我在 aspx 页面上使用自动连接。您的页面声明如下所示:
<%@ Page Language="C#" MasterPageFile="~/XXXXXX.master" AutoEventWireup="true" CodeBehind="YYYY.aspx.cs" Inherits="ZZZ.ZZZ.AAAAAA" Title="View Blah" %>
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:
页面上的此设置允许您使用 UI 连接事件。单击网格,选择属性,单击闪电图标,然后在 RowDataBound 事件下选择您的方法。所有这些在幕后都是为 DataGridView 添加一个属性,因此:
<asp:GridView ID="uiActionGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False"
OnRowDataBound="uiActionGridView_RowDataBound" OnDataBound="uiActionGridView_DataBound">
- this shows two events being wired-up, the DataBound and RowDataBound events.
- 这显示了两个正在连接的事件,DataBound 和 RowDataBound 事件。
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 afterthe databind has occurred.
这就是我使用 VS2005 所做的,它似乎“正常工作”。我认为您遇到的唯一一件事是您在数据绑定发生后手动绑定事件。