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设置为类“header”。
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!
这可能是我对如何正确使用它的误解,但它似乎没有做任何事情。我认为它会将所有行设置为类“header”,如果有的话,我将从那里开始处理我的逻辑,但我甚至无法实现这一点。感谢任何人都能提供的帮助!
1 个解决方案
#1
17
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'分配给Grid:
/* 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 after the databind has occurred.
这就是我使用VS2005做的事情,它似乎都“正常工作”。我能想到的唯一一件事是你在数据绑定发生后手动绑定事件。
#1
17
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'分配给Grid:
/* 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 after the databind has occurred.
这就是我使用VS2005做的事情,它似乎都“正常工作”。我能想到的唯一一件事是你在数据绑定发生后手动绑定事件。