有选择地应用CSS来连续在GridView [英] Selectively apply css to a row in a gridview
问题描述
我正在寻找一种方式来选择性地应用CSS类各行的 GridView控件
基于数据的属性绑定项。
I'm looking for a way to selectively apply a CSS class to individual rows in a GridView
based upon a property of the data bound item.
例如:
GridView的数据源 SummaryItems
和 SummaryItem
有一个属性 ShouldHighlight的泛型列表
。当 ShouldHighlight ==真
为相关行CSS应该设置为强调
GridView's data source is a generic list of SummaryItems
and SummaryItem
has a property ShouldHighlight
. When ShouldHighlight == true
the CSS for the associated row should be set to highlighted
什么想法?
推荐答案
很容易
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (drv["ShouldHighlight"].ToString().ToLower() == "true")
e.Row.CssClass = "highlighted";
}
}
在code以上的作品,如果你使用的数据表作为数据源
更改为:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
myClass drv = (myClass)e.Row.DataItem;
if (drv.ShouldHighlight)
e.Row.CssClass = "highlighted";
}
}
只是使用泛型时上面的例子:
just for the example above when using generics:
public class myClass
{
public Boolean ShouldHighlight
{ get; set; }
}
如果您是泛型(列表,字典等)
if you are working with Generics (List, Dictionary, etc)
记住:
e.Row.dataItem
总是返回您正在使用填充该行的整个对象,所以很容易从这里来操纵数据的网页的外观。
always return the entire object that you are populating the row with, so it is easy from here to manipulate the appearance of the data in the webpage.
您应该使用RowDataBound事件中的数据连接后,该行的对象,但没有写的HTML code页面,这样你可以检查ShouldHighlight值将触发(我转换为String的原因我不知道是什么类型,你可以改变它,如果你知道这是一个布尔值)。
you should use RowDataBound event that will trigger after the data is attached to the row object but not yet written the HTML code in the page, in this way you can check the ShouldHighlight value (I converted to a String cause I do not know the type, you can change it if you know it's a boolean value).
这code的运行速度比megakemp code原因你没有创建一个列表对象,并使用每一行...
this code runs much faster than megakemp code cause you're not creating a List object and populated with the entire data source for each row...
的 P.S。采取看看这个网站,你可以找到几个教程使用的GridView对象项目的
P.S. take a look at this website, you can find several tutorials for your project using the GridView object
这篇关于有选择地应用CSS来连续在GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!