更新GridView单元格背景颜色 [英] updating GridView cell background color

查看:70
本文介绍了更新GridView单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解如何将SqlDataSource连接到GridView并且有一个很好的

数据连接,分页网页几乎没有编码生成。

从那个基础开始,我想在GridView中添加一个额外的列

并做两件事:对于每一行,根据另一列的值,在新的中插入

a string列单元格并设置新

列单元格的背景颜色。建议的方法将不胜感激。

I understand how to connect a SqlDataSource to a GridView and have a nice
data-connected, paginated web page generated with virtually no coding.
Starting from that base, I would like to add an extra column to the GridView
and do two things: for each row, based on the value of another column, insert
a string in the new column cell and set the background color of the new
column cell. Suggested approaches would be appreciated.

推荐答案

嗨迈克尔,

您可以为GridView的RowDataBound事件创建处理程序。在这个

处理程序中,您将能够访问和修改当前

行的单元格和数据。


protected void myGridView_OnRowDataBound (对象发送者,GridViewRowEventArgs

e)

{

if(e.Row.RowType = DataGridRowType.DataRow)

{

// e.Row.Cells包含服务器端td元素的集合,你可以

填写

//文本属性或工作此元素中包含控件集合

e.Row.Cells [INDEX_OF_YOUR_COLUMN] .Text = EvalMyField(e.Row.DataItem);

// DataItem包含数据源对于这一行,它是数据使用的对象

来源填充你的行

//你也可以为你的元素修改css样式

e.Row.Cells [INDEX_OF_YOUR_COLUMN] .Attributes.Add(" style"," .......");

}

}


问候,

Ladislav


" michael sorens"写道:
Hi Michael,
you can create handler for RowDataBound event of your GridView. In this
handler you will be able to access and modify cells and data of your current
row.

protected void myGridView_OnRowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType = DataGridRowType.DataRow)
{
// e.Row.Cells contains collection of server side td elements, you can
fill
// text property or work with collection of controls inside this element
e.Row.Cells[INDEX_OF_YOUR_COLUMN].Text = EvalMyField(e.Row.DataItem );
// DataItem contains source of data for this row, it is object used by data
source to fill your row
// you can also modify css style for your elelment
e.Row.Cells[INDEX_OF_YOUR_COLUMN].Attributes.Add("style", ".......");
}
}

Regards,
Ladislav

"michael sorens" wrote:

我理解如何将SqlDataSource连接到GridView并且有一个很好的

数据连接,分页生成的虚拟网页没有编码。

从那个基础开始,我想在GridView中添加一个额外的列

并做两件事:对于每一行,基于另一行的值列,在新列单元格中插入

a字符串,并设置新

列单元格的背景颜色。建议的方法将不胜感激。
I understand how to connect a SqlDataSource to a GridView and have a nice
data-connected, paginated web page generated with virtually no coding.
Starting from that base, I would like to add an extra column to the GridView
and do two things: for each row, based on the value of another column, insert
a string in the new column cell and set the background color of the new
column cell. Suggested approaches would be appreciated.


Splendid!我不得不测试DataControlRowType.DataRow而不是

DataGridRowType.DataRow,但这基本上完成了我需要的大部分工作。


现在我如何定位新列是最右边的列?

我首先尝试在可视化设计器中添加一个类型为Template的新列。

这个新列成为第一列。我通过调用PageLoad处理程序中的gridView.Columns.Add()
以编程方式尝试了同样的事情

,它也成了第一列。然后我在PageLoad处理程序中切换到Columns.Insert()但是

尚未发生绑定,因此列数

仍为0.当绑定发生时,列被添加到右边,

再次离开我的新列作为最左边。在列加载之后,什么事件处理程序会给我

我访问GridView所以我可以然后

在我想要的地方添加我的新列?

Splendid! I had to test for DataControlRowType.DataRow rather than
DataGridRowType.DataRow, but that essentially did most of what I needed.

Now how do I position the new column as the right-most column?
I first tried adding a new column of type Template in the visual designer.
This new column became the first column. I tried the same thing
programmatically by calling gridView.Columns.Add() in the PageLoad handler
and it also became the first column. I then switched to Columns.Insert() but
in the PageLoad handler the binding has not yet occurred, hence column count
is still 0. When the binding occurs, the columns are added to the right,
again leaving my new column as the left-most. What event handler would give
me access to the GridView after the columns have been loaded so I can then
add my new column where I want?


嗨迈克尔,

我很高兴您已经解决了第一个问题。

对于第二个问题,您是否尝试在GridView的

DataBound事件中添加该列?在这种情况下,数据已被绑定,并且生成了这些列

,因此您可以将列添加到最右侧。


请让我知道结果。谢谢。

问候,

Walter Wang(wa****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


================================= =================

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子提供按现状 ;没有保证,也没有授予任何权利。

Hi Michael,

I''m glad that you''ve solved your first issue.

For the second question, have you tried to add the column in GridView''s
DataBound event? In this event, the data has been bound and those columns
are generated, therefore you can add your column to the right-most.

Please let me know the result. Thanks.
Regards,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于更新GridView单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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