我正在使用一个asp:DataGrid绑定到一个由我的SQLDataAdapter填充的DataSet。在我的页面上显示asp:DataGrid之前,我想检查asp:DataGrid,并且对于每个等于$ 10.00的金额,我想突出显示黄色行。
这是我的VB CodeBehind
'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>
答案 0 :(得分:1)
DataGrid具有您可以使用的ItemDataBound事件。
protected void ERNDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
//cast the dataitem back to a datarowview
DataRowView row = e.Item.DataItem as DataRowView;
//check the column value and color the row
if (Convert.ToDecimal(row["amount"]) == 10)
{
e.Item.BackColor = Color.Red;
}
}
}
VB
Protected Sub ERNDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Item) Then
'cast the dataitem back to a datarowview
Dim row As DataRowView = CType(e.Item.DataItem,DataRowView)
'check the column value and color the row
If (Convert.ToDecimal(row("amount")) = 10) Then
e.Item.BackColor = Color.Red
End If
End If
End Sub
答案 1 :(得分:0)
编辑:我刚注意到你使用的是DataGrid,而不是GridView。我建议在遵循我的示例之前将其更改为GridView。
为gridview使用OnRowDataBound事件。
因此,当您在gridview中生成每一行时,您会检查该行的金额是否等于$ 10.
Protected Sub ERNDataGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles ERNDataGrid.RowDataBound
If e.Row.Cells(2).Text = "$10" Then
e.Row.BackColor = Drawing.Color.Yellow
End If
End Sub
更改代码以符合您的规格和数据类型。