我正在使用一個asp:DataGrid綁定到由我的SQLDataAdapter填充的DataSet。在我的頁面上顯示asp:DataGrid之前,我想檢查一下asp:DataGrid,並且每個金額等於$ 10.00,我想要突出顯示黃色的行。高亮顯示DataGrid行如果單元格等於X
這裏是我的VB代碼隱藏
'Create a connection
Dim myConnection As New SqlConnection("Yes, this works")
'Create the command object, passing in the SQL string
Const strSQL As String = "SELECT CUID, Account, Amount/100 as Amount, Serial FROM [ACCU].[dbo].[ERN_ITEM_VIEW] Where Date = '04/13/2017' And CUID <> '0'"
Dim myCommand As New SqlCommand(strSQL, myConnection)
'Create the DataAdapter
Dim myDA As New SqlDataAdapter()
myDA.SelectCommand = myCommand
'Populate the DataSet
Dim myDS As New DataSet()
myDA.Fill(myDS)
'Set the datagrid's datasource to the dataset and databind
ERNDataGrid.DataSource = myDS
ERNDataGrid.DataBind()
'Display Information on what page we are currently viewing
lblMessage.Text = "Viewing Page " & ERNDataGrid.CurrentPageIndex + 1 & " of " & ERNDataGrid.PageCount
這裏是我的asp:DataGrid的
<asp:DataGrid ID="ERNDataGrid" runat="server" BorderWidth="0px"
CellPadding="2" Width="100%"
Font-Name="Verdana"
Font-Size="Smaller"
AutoGenerateColumns="False"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="#77a13d"
HeaderStyle-ForeColor="White"
AlternatingItemStyle-BackColor="#dddddd"
AllowPaging="True"
PageSize="15"
OnPageIndexChanged="ERNDataGrid_PageIndexChanged" Font-Names="Verdana">
<HeaderStyle HorizontalAlign="Center" BackColor="#464646" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle Mode="NextPrev" HorizontalAlign="Right"
ForeColor="White" BackColor="#464646"
NextPageText="Next Page >>" PrevPageText="<< Prev. Page" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></PagerStyle>
<AlternatingItemStyle BackColor="#DDDDDD"></AlternatingItemStyle>
<Columns>
<asp:BoundColumn HeaderText="Routing Number" DataField="CUID" ItemStyle-HorizontalAlign="Center" ReadOnly="True">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn HeaderText="Account" DataField="Account" ItemStyle-HorizontalAlign="Center" ReadOnly="True">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn HeaderText="Amount" DataField="Amount" ItemStyle-HorizontalAlign="Center" ReadOnly="True" DataFormatString="{0:C}">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn HeaderText="Check Number" DataField="Serial" ItemStyle-HorizontalAlign="Center" ReadOnly="True">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
我忍辱負重,改爲GridView控件。我相信我的一部分是固執的,因爲我在這方面很新穎,所以我的一部分人「害怕」。我確實需要添加RowIndex檢查。 '代碼 Private Sub ERNDataGrid_RowDataBound(sender As Object,e As GridViewRowEventArgs)Handles ERNDataGrid.RowDataBound If e.Row.RowIndex> -1 Then Then if.Row.Cells(2).Text =「$ 500.00」Then e.Row .BackColor = Drawing.Color.Yellow End If End If End Sub' 儘管如此,我勝利了,我能夠得到我想要的結果。謝謝@Wenadin。 –