[英]Set ASP DataGrid row color based on cell value
I have an ASP:DataGrid
which I am displaying records in. The records are pulled from an SQL Server 2008 R2 database in to an SqlAdapter
which in is used to fill a DataSet
and that is bound to the ASP:DataGrid
我有一个ASP:DataGrid
。对此我在显示记录的记录是从SQL Server 2008 R2的数据库中抽取到的SqlAdapter
这是用来填充DataSet
,并且结合到ASP:DataGrid
The ASP:DataGrid
can span multiple pages and is limited to 20 records per page. ASP:DataGrid
可以跨越多个页面,并且每页限制为20条记录。
One of the columns is a value for how many days are remaining for the entry to be dealt with. 列之一是一个值,表示剩余多少天要处理该条目。 I want to highlight any which have a value 0 - 1 as red and any 2 - 5 as oracle. 我想突出显示红色值为0-1且oracle为2-5的值。 The method I have tried is to set a function call to the OnItemDataBound
field of the ASP:DataGrid
我尝试过的方法是将函数调用设置为ASP:DataGrid
的OnItemDataBound
字段
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Select Case CType(e.Item.Cells(5).Text, Integer)
Case 0 To 1
e.Item.BackColor = Drawing.Color.Red
Case 2 To 5
e.Item.BackColor = Drawing.Color.Orange
End Select
End If
End Sub
This works perfectly when the page first loads. 首次加载页面时,此方法非常有效。 If there are more than one page of records and you try to move to the next page, no records load at all and in fact the ASP:DataGrid
doesn't display at all. 如果记录多于一页,而您尝试移至下一页,则根本不会加载任何记录,实际上ASP:DataGrid
根本不会显示。 If I remove the OnItemDataBound
then the ASP:DataGrid
functions normally again (albeit without the highlights). 如果我删除OnItemDataBound
则ASP:DataGrid
可以再次正常运行(尽管没有突出显示)。
So it turned out that may code was actually correct. 因此事实证明,可能的代码实际上是正确的。 I put a Try
block around the code with a section in the Catch
part to log the details in a table in my database. 我在Catch
部分中的代码部分放置了一个Try
代码块,以将详细信息记录在数据库的表中。
Straight away it showed the following coming from the Select Case
立刻显示出来自Select Case
的以下内容
Conversion from string " " to type 'Integer' is not valid.
A simple change to ensure that the cell is only coloured when a valid value is present is all I needed. 我需要做的一个简单更改就是确保仅当存在有效值时才对单元格进行着色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.