Home All Groups Group Topic Archive Search About

Conditionally set dataGrid Rows backcolor

Author
22 Feb 2005 12:07 AM
Martin
Hello!
I have created dinamically some datagrids, but I need to set the datagrids
row backcolor to a specific color depending of its contents and without using
AlternatingItemStyle because this does it every 1.5 rows and I need it every
2 rows.

The datagrids are inside of a dinamically created table, so everything is
programatically created except the aspTable.
Thanks.

Author
22 Feb 2005 2:55 AM
Ken Cox [Microsoft MVP]
Hi Martin,

You should be able to dig into the datagrid in the ItemDataBound event and
check for the contents of a particular cell. At that point, you can change
the backcolour. Here's some sample code. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

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


   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

Show quoteHide quote
"Martin" <Mar***@discussions.microsoft.com> wrote in message
news:3EE1D07A-8BB9-4C8A-84E3-BB3E5117F551@microsoft.com...
> Hello!
> I have created dinamically some datagrids, but I need to set the datagrids
> row backcolor to a specific color depending of its contents and without
> using
> AlternatingItemStyle because this does it every 1.5 rows and I need it
> every
> 2 rows.
>
> The datagrids are inside of a dinamically created table, so everything is
> programatically created except the aspTable.
> Thanks.
>
Author
22 Feb 2005 5:05 PM
Martin
The source code of your example is static, but
you guide me to find the solution

My code is something like this.

  Protected WithEvents MainTable As System.Web.UI.WebControls.Table

  Private sub createDynamicGrid (ds as dataset)
    Dim dGrid As New DataGrid
    dGrid = FormatDataGrid() 'Format the CellPadding, CellSpacing, Font, 
AutoGenerateColumns = False, Add Bound Columns etc..
    dGrid.datasource = ds
    dGrid.databind()

    AddHandler dGrid.ItemDataBound, AddressOf Grid_ItemCommand
    AddGridtoASPTable(dGrid)

  End sub


  Private Sub Grid_ItemCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
      If e.Item.ItemType = ListItemType.Item Then
          If (e.Item.Cells(0).Text.ToLower = " ") Then
            e.Item.Cells(i).BackColor = Color.FromName("Blue")
          End If
        End If
  End Sub

  private sub AddGridToASPTable(dGrid as datagrid)
     Dim tCell As TableCell
     Dim tRow As TableRow
     tRow = New TableRow
     tCell = New TableCell
     tCell.Controls.Add(dGrid)
     tRow.Cells.Add(tCell)
     MainTable.Rows.Add(tRow)
  end sub

Thanks for help me Ken :)