我需要有動態的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我有這個錯誤
代碼
很確定您可以使用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