更改颜色是白色,然后灰色
问题描述:
我需要有动态的GridView一个白三种颜色,另一种是灰色的,但如果它在同日 ,如果以前的日期不发现红色 我的意思是:更改颜色是白色,然后灰色
Name Checkin Checkout Branch
450 10/6/2014 9:13:38 AM 10/6/2014 6:01:50 PM branch0 white
450 10/7/2014 9:16:34 AM 10/7/2014 6:44:21 PM branch0 gray
450 10/8/2014 9:11:53 AM branch0 white
450 10/8/2014 6:03:25 PM branch0 white
450 10/11/2014 9:17:33 AM 10/11/2014 6:29:16 PM branch0 red
450 10/11/2014 4:50:42 PM branch0 red
450 10/12/2014 9:09:38 AM branch0 white
,这是我的GridView
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource2"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" HorizontalAlign="Center" Width="602px">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Checkin" HeaderText="Checkin"
SortExpression="Checkin" ReadOnly="True" />
<asp:BoundField DataField="Checkout" HeaderText="Checkout"
SortExpression="Checkout" ReadOnly="True" />
<asp:BoundField DataField="MachineAlias" HeaderText="Branch"
SortExpression="MachineAlias" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
,这是代码
Public Class WebForm2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
我寻找,我有这样的代码
Dim found As Boolean
Dim dt As DateTime
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Checkout")) Then
dt = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Checkout")).Date
End If
If e.Row.RowIndex > 0 And found = False Then
e.Row.BackColor = Drawing.Color.Red
End If
found = Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Checkout"))
End If
End Sub
但它给我的错误
当我试图从MR @Zack我有这个错误
代码
答
尝试使用此...
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim arg1 As Date = Date.Parse(e.Row.Cells(1).Text)
Dim arg2 As Date = Date.Parse(iif(isDate(e.Row.Cells(2).Text),e.Row.Cells(2).Text,"1/1/1900"))
If arg1 = arg2 Then
e.Row.BackColor = Drawing.Color.Red
End If
End If
End Sub
+0
首先请允许我感谢你和我编辑自己的帖子对你我有一个错误,是要确保,如果我尝试这个代码,我将有三种颜色的W和G和R – 2014-10-29 23:05:06
+0
尝试上述改变对我的建议,对不起,如果语法是抬高我从记忆的时刻做这个... – Zack 2014-10-30 01:01:43
很确定您可以使用RowDataBound事件来比较两列彼此并在日期匹配时突出显示它们。您需要将来自单元格的数据格式化为mm/dd/yyyy,然后放下时间,以便使其完全匹配。我给你举个例子,但我并没有坐在带编译器的电脑上,也不想给你丑陋的代码。如果你谷歌它,但你应该能够找到它的东西。 – Zack 2014-10-29 00:38:07
错误行是因为它需要e.Row.RowType你不得不在它的中间一个“R”。它也处理GridView1.DataBinding,需要GridView1.RowDataBound – Zack 2014-10-29 14:49:00