2017-05-11 118 views
0

我正在使用一個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> 

回答

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 

更改代碼以符合您的規範和數據類型。

+1

我忍辱負重,改爲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。 –

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 
+0

謝謝你的回答。在你回答之前,我咬緊牙關,把我的DataGrid改成了一個GridView,並且隨着第一個響應。經過一些調整,將屬性更改爲新名稱後,我能夠從GridView中獲得所需的結果。雖然我確信這個輸出可能會起作用,但將所有事情都改回來看看它是否會起作用,這可能需要我一些時間。我會將這個答案標記爲有用,因爲這正是我最初尋找的。 –

+0

沒問題。但我保證它的工作。我在發佈之前先測試一切。 – VDWWD

相關問題