Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing the color of a cell of a datagrid depending on a value

Status
Not open for further replies.

harneel

Programmer
Jan 22, 2004
7
US
hi,

I'm using a datagrid and need to change the background color of the CELL depending on the value of the cell. Any ideas??
 
I think you can use the ItemDataBound event on the datagrid.

Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
If e.Item.Cells(21).Text = &quot;No&quot; Then e.Item.Cells(21).BackColor = 0
End If
End Sub

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
Hi,

I need to check the values of a column and then change the color according to the value. I need to specify the column but not the row.
 
Jennifer has the correct solution from what you've described. The value's of a column can change depending on row so you can't just check a column's values. You can check a cell in a column, which is what jennifer has done.

I've had a similiar problem before. I needed each column to have a different color. The ItemDataBound event is where you want to change these values and you must do it on a per cell basis not column.

Hope that made things a little clearer. If not post back and I am sure someone will have a solution for you.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
hi,

I copied the row and it's giving an error
Value of type 'String' cannot be converted to 'System.Drawing.Color'. The error is showing on hte line where i give the color.
 
Type this
System.Drawing.Color.Aqua;

Or whatever color you want besides aqua. Using the Intellisense you can simply pick the color you want out of the list box. You could also create a color variable and set the RGB values for that variable uniquely then just assign the back color to your variable.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hi jzelhart,

I tried your solution and got the following error:
Handles clause requires a WithEvents Variable.

I am new to asp.net and I did not understand what that message mean .
thanks for your help
Al
 
What language are you using C# has a different syntax than VB. Which is what Jennifer used.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I am following the above instructions. I am not getting any errors but, every single cells text property evaluates to "", although the grid shows up fine with text in every column,row,cell.

Can anyone tell me what may be doing wrong?
The If statement:
If e.Item.Cells(0).Text = "1" Then
never evaluates to true and in debug, checking everycell value (ie: ? e.item.cells(1).text, ? e.item.cells(2).text, etc...) all return "" as the value...

Help!

Code:
 Private Sub dgPrinters_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPrinters.ItemDataBound
        If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
            'If Printer is in use then color the row Red
            If e.Item.Cells(0).Text = "1" Then
                e.Item.BackColor = System.Drawing.Color.Red
            End If
        End If
    End Sub



PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
This is the HTML for the column I want to evaluate.

Code:
<asp:TemplateColumn HeaderText="InUse">				<ItemTemplate>
	<%# databinder.eval(container.dataitem, "InUse") %>
	</ItemTemplate>
</asp:TemplateColumn>

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Once again, answered my own (stupid) question. Sorry to have wasted anyone's time. but maybe posting this will help someone else in the future.

the column needs to be a databound then everything works fine.

<asp:BoundColumn HeaderText="InUse" DataField="InUse" Visible=False/>

Thanks.

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Here is c# that works. I do not see how your code goes through the whole grid. Sorry I can not convert it to vb. I hope it helps.
Code:
void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        TableCell cell;
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
            foreach(System.Web.UI.Control control in e.Item.Controls)
            {
                cell = control as TableCell;
                if (cell.Text == "3")
                {
                     cell.BackColor = Color.FromName("red");
                }

            }
            break;
        }
    }
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top