Home All Groups Group Topic Archive Search About
Author
1 Mar 2005 1:24 AM
Javier Pérez
Hello All,

Aplogise my english, im a spanish speaker.

Ive got a dataGrid where one their columns is a bit type. I would like where
one of this columns was true, the background color change to red color.

I tried to do this, but no way...

------------------------------------------------------------------------------------------------------



Create the event
    this.dtgSortAndPaging.ItemDataBound += new
    DataGridItemEventHandler(dtgSortAndPaging_ItemDataBound);


Create the function:
private void dtgSortAndPaging_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{
How I can work with de background propertie of de datagrid and change his
color??
}


Thanks!

Author
1 Mar 2005 1:54 AM
Ken Cox [Microsoft MVP]
Hi Javier,

Here's an example in VB.NET that might give you the idea.

Ken
Microsoft MVP [ASP.NET]
Toronto

  Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            DataGrid1.DataSource = CreateDataSource()
            DataGrid1.DataBind()
        End If
    End Sub

    Private Sub DataGrid1_ItemDataBound _
    (ByVal sender As Object, ByVal e As _
    System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or _
        e.Item.ItemType = ListItemType.Item Then
            Dim drow As DataGridItem
            Dim lbl As Label
            drow = e.Item
            lbl = CType(drow.Controls(0).Controls(1), Label)
            If lbl.Text = "True" Then
                e.Item.BackColor = Color.Beige
            Else
                e.Item.BackColor = Color.Red
            End If
        End If
    End Sub

    Function CreateDataSource() As DataTable
        Dim dt As New DataTable
        Dim dr As DataRow
        dt.Columns.Add(New DataColumn _
        ("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn _
        ("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn _
        ("CurrencyValue", GetType(Double)))
        dt.Columns.Add(New DataColumn _
        ("Boolean", GetType(Boolean)))
        Dim i As Integer
        For i = 0 To 4
            dr = dt.NewRow()
            dr(0) = i
            dr(1) = "Item " + i.ToString()
            dr(2) = 1.23 * (i + 1)
            dr(3) = (i = 4)
            dt.Rows.Add(dr)
        Next i
        Return dt
    End Function 'CreateDataSource

            <asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
                <columns>
                    <asp:templatecolumn>
                        <itemtemplate>
                            <asp:Label runat="server"  Text='<%#
DataBinder.Eval(Container, "DataItem.Boolean") %>'>
                            </asp:label>
                        </itemtemplate>
                    </asp:templatecolumn>
                </columns>
            </asp:datagrid>

Show quoteHide quote
"Javier Pérez" <jpe***@nethost.com.es> wrote in message
news:OYpvcyfHFHA.560@TK2MSFTNGP12.phx.gbl...
> Hello All,
>
> Aplogise my english, im a spanish speaker.
>
> Ive got a dataGrid where one their columns is a bit type. I would like
> where
> one of this columns was true, the background color change to red color.
>
> I tried to do this, but no way...
>
> ------------------------------------------------------------------------------------------------------
>
>
>
> Create the event
>    this.dtgSortAndPaging.ItemDataBound += new
>    DataGridItemEventHandler(dtgSortAndPaging_ItemDataBound);
>
>
> Create the function:
> private void dtgSortAndPaging_ItemDataBound(object sender,
> System.Web.UI.WebControls.DataGridItemEventArgs e)
>
> {
> How I can work with de background propertie of de datagrid and change his
> color??
> }
>
>
> Thanks!
>
>
>
Author
1 Mar 2005 2:00 AM
Elton Wang
Hi Javier,

Following code sample shows how to change a cell back
color according to its value:

ListItemType itemType = e.Item.ItemType;

if (itemType != ListItemType.Header && itemType !=
ListItemType.Pager && itemType != ListItemType.Footer &&
itemType != ListItemType.Seperator){

    DataRowView drv = (DataRowView)e.Item.DataItem;
    bool bData = (bool)drv[colIndex];
    TableCell cell = e.Item.Cells[colIndex];

    if (bData){
        cell.BackColor = Color.Red;
    }
}

By the way, it's better to code above logic in ItemCreated.

HTH

Elton Wang
el***@hotmail.com


>-----Original Message-----
>Hello All,
>
>Aplogise my english, im a spanish speaker.
>
>Ive got a dataGrid where one their columns is a bit type.
I would like where
>one of this columns was true, the background color change
to red color.
Show quoteHide quote
>
>I tried to do this, but no way...
>
>----------------------------------------------------------
--------------------------------------------
>
>
>
>Create the event
>    this.dtgSortAndPaging.ItemDataBound += new
>    DataGridItemEventHandler
(dtgSortAndPaging_ItemDataBound);
>
>
>Create the function:
>private void dtgSortAndPaging_ItemDataBound(object sender,
>System.Web.UI.WebControls.DataGridItemEventArgs e)
>
>{
>How I can work with de background propertie of de
datagrid and change his
Show quoteHide quote
>color??
>}
>
>
>Thanks!
>
>
>
>.
>
Author
3 Mar 2005 6:07 PM
Javier Pérez
Its run perfectly!!!!

Thanks very much!!!!


Show quoteHide quote
"Elton Wang" <anonym***@discussions.microsoft.com> escribió en el mensaje
news:263401c51e02$624f4fe0$a401280a@phx.gbl...
> Hi Javier,
>
> Following code sample shows how to change a cell back
> color according to its value:
>
> ListItemType itemType = e.Item.ItemType;
>
> if (itemType != ListItemType.Header && itemType !=
> ListItemType.Pager && itemType != ListItemType.Footer &&
> itemType != ListItemType.Seperator){
>
>    DataRowView drv = (DataRowView)e.Item.DataItem;
>    bool bData = (bool)drv[colIndex];
>    TableCell cell = e.Item.Cells[colIndex];
>
>    if (bData){
>        cell.BackColor = Color.Red;
>    }
> }
>
> By the way, it's better to code above logic in ItemCreated.
>
> HTH
>
> Elton Wang
> el***@hotmail.com
>
>
>>-----Original Message-----
>>Hello All,
>>
>>Aplogise my english, im a spanish speaker.
>>
>>Ive got a dataGrid where one their columns is a bit type.
> I would like where
>>one of this columns was true, the background color change
> to red color.
>>
>>I tried to do this, but no way...
>>
>>----------------------------------------------------------
> --------------------------------------------
>>
>>
>>
>>Create the event
>>    this.dtgSortAndPaging.ItemDataBound += new
>>    DataGridItemEventHandler
> (dtgSortAndPaging_ItemDataBound);
>>
>>
>>Create the function:
>>private void dtgSortAndPaging_ItemDataBound(object sender,
>>System.Web.UI.WebControls.DataGridItemEventArgs e)
>>
>>{
>>How I can work with de background propertie of de
> datagrid and change his
>>color??
>>}
>>
>>
>>Thanks!
>>
>>
>>
>>.
>>