• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

change row color of datagrid asp.net 1.1

Trying to change the color of my rows if a cell or mix of cells have no in them.  This is what I have far but it does not seem to work.  I even tried putting the code after the datagrid bind and still nothing.  Stumped why this does not work.


<asp:datagrid id="myDataGrid" runat="server" BorderWidth="1px" CellPadding="6" GridLines="Both" BorderColor="#404040" Font-Size="12px" Font-Names="Verdana,Arial,sans-serif" AutoGenerateColumns="False" cellspacing="0" HeaderStyle-HorizontalAlign="Center">
<AlternatingItemStyle BackColor="#b0c4de"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="steelblue"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="strName" HeaderText="Admin Name"></asp:BoundColumn>
<asp:BoundColumn DataField="strProfile" HeaderText="Tech Level"></asp:BoundColumn>
<asp:BoundColumn DataField="strAppOrders" HeaderText="Appointment Orders"></asp:BoundColumn>
<asp:BoundColumn DataField="strAccessAgree" HeaderText="AAccess Agreement"></asp:BoundColumn>
<asp:BoundColumn DataField="strAwareTrain" HeaderText="Awareness Training"></asp:BoundColumn>
<asp:BoundColumn DataField="strCertified" HeaderText="Certified"></asp:BoundColumn>
<asp:BoundColumn DataField="strCECertified" HeaderText="CE Certified"></asp:BoundColumn>
<asp:BoundColumn DataField="strMinReqTrain" HeaderText="Online Training"></asp:BoundColumn>
</Columns>
</asp:datagrid>
 
If Not IsPostBack Then
 
            sql = "Select distinct a.strName, c.strProfile, c.strAppOrders, c.strAccessAgree, c.strAwareTrain, c.strCertified, c.strCECertified, c.strMinReqTrain " _
                & "from tblADAccounts as a Inner join tblIACompliance as c on a.strEmail = c.strEmail Order by c.strProfile, a.strName, c.strCertified, " _
                & "c.strMinReqTrain"
 
            myDataTable = New DataTable
            myDataTable = getData(sql)
 
            Dim OneItem As DataGridItem
            For Each OneItem In myDataGrid.Items
                If OneItem.Cells(5).Text = "YES" Then
                    OneItem.Cells(5).BackColor = Drawing.Color.Red
                End If
            Next
 
            myDataGrid.DataSource = myDataTable
            myDataGrid.DataBind()
 
 
        End If

Open in new window

0
kdeutsch
Asked:
kdeutsch
2 Solutions
 
prairiedogCommented:
Based on your code snippet, which cell(s) are you talking about? You want to check if there is a "number" in a cell or a "no" in the cell?
0
 
burakiewiczCommented:
i believe you need to run this after the dataBind
Dim OneItem As DataGridItem
            For Each OneItem In myDataGrid.Items
                If OneItem.Cells(5).Text = "YES" Then
                    OneItem.Cells(5).BackColor = Drawing.Color.Red
                End If
            Next
or i think you can process it in the ondatabound event
0
 
MaxOvrdrv2Commented:
try this instead:

If Trim(LCase(OneItem.Cells(5).Text)).ToString = "yes" Then

Cheers!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rahkimfCommented:
I think its best to do this in the itemdatabound method.  Like this (Its in C#, but Im sure you will get the idea.)

            
public void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
	
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{				
			if(e.Item.Cells[5].Text == "YES")
			{
				e.Item.BackColor = System.Drawing.Color.Red;
			}				
			}
}

Open in new window

0
 
kdeutschAuthor Commented:
Hi ok tried this but does not change anything. Does the myDataGrid_ItemDataBound automatically call itself or do I need to call it somewhere.

If Not IsPostBack Then

            sql = "Select distinct a.strName, c.strProfile, c.strAppOrders, c.strAccessAgree, c.strAwareTrain, c.strCertified, c.strCECertified, c.strMinReqTrain " _
                & "from tblADAccounts as a Inner join tblIACompliance as c on a.strEmail = c.strEmail Order by c.strProfile, a.strName, c.strCertified, " _
                & "c.strMinReqTrain"

            myDataTable = New DataTable
            myDataTable = getData(sql)

            myDataGrid.DataSource = myDataTable
            myDataGrid.DataBind()

        End If
    End Sub

    Public Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            If e.Item.Cells(5).Text = "YES" Then
                e.Item.BackColor = System.Drawing.Color.Red
            End If
        End If
    End Sub
0
 
MaxOvrdrv2Commented:
again... use the Trim and LCase functions... you,d be surprised how many problems can be solved...

Datagrids tend to add spaces for formatting issues... and the LCase ensures that both your field comparisons are in lower case...

Cheers!
0
 
rahkimfCommented:
You need to put this in your datagrid.


<?xml:namespace prefix = asp />

You need to put this in your datagrid.

<asp:datagrid id="myDataGrid" OnItemDataBound="myDataGrid_ItemDataBound" runat="server" BorderWidth="1px" CellPadding="6" GridLines="Both" BorderColor="#404040" Font-Size="12px" Font-Names="Verdana,Arial,sans-serif" AutoGenerateColumns="False" cellspacing="0" HeaderStyle-HorizontalAlign="Center">
0
 
rahkimfCommented:
Sorry...posted that twice.  Its the second one that shows in bold what you need to do.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now