changing row color depending on a cell value

Discussion in 'ASP .Net Datagrid Control' started by Volkan Karaboða, Feb 20, 2004.

  1. Hi all

    I want to change datagrid's row color depending on a cell value

    How can I do this?
    I think that I must write some code in bind method but I dont know How I do
    this.

    please help me!!
    thanks...
     
    Volkan Karaboða, Feb 20, 2004
    #1
    1. Advertising

  2. Hi Volkan,

    You need to catch the ItemDataBound event when the row is bound to the
    datarow. At that point, you can test if you are dealing with a regular row.
    If so, get an instance of the cell you're after and change its colour value.
    The sample people should get you started.

    Let us know if this helps?

    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.Item Or _
    e.Item.ItemType = ListItemType.AlternatingItem Then
    If e.Item.Cells(0).Text = "4" Then
    e.Item.Cells(0).BackColor = Color.Red
    e.Item.Cells(3).BackColor = Color.Red
    End If
    End If
    End Sub
    Function CreateDataSource() As ICollection
    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 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = True
    dt.Rows.Add(dr)
    Next i
    Dim dv As New DataView(dt)
    Return dv
    End Function 'CreateDataSource

    "Volkan Karaboða" <> wrote in message
    news:%...
    > Hi all
    >
    > I want to change datagrid's row color depending on a cell value
    >
    > How can I do this?
    > I think that I must write some code in bind method but I dont know How I
    > do
    > this.
    >
    > please help me!!
    > thanks...
    >
    >
     
    Ken Cox [Microsoft MVP], Feb 21, 2004
    #2
    1. Advertising

  3. thank you very much. It works well...



    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:uzX$biC%...
    > Hi Volkan,
    >
    > You need to catch the ItemDataBound event when the row is bound to the
    > datarow. At that point, you can test if you are dealing with a regular

    row.
    > If so, get an instance of the cell you're after and change its colour

    value.
    > The sample people should get you started.
    >
    > Let us know if this helps?
    >
    > 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.Item Or _
    > e.Item.ItemType = ListItemType.AlternatingItem Then
    > If e.Item.Cells(0).Text = "4" Then
    > e.Item.Cells(0).BackColor = Color.Red
    > e.Item.Cells(3).BackColor = Color.Red
    > End If
    > End If
    > End Sub
    > Function CreateDataSource() As ICollection
    > 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 8
    > dr = dt.NewRow()
    > dr(0) = i
    > dr(1) = "Item " + i.ToString()
    > dr(2) = 1.23 * (i + 1)
    > dr(3) = True
    > dt.Rows.Add(dr)
    > Next i
    > Dim dv As New DataView(dt)
    > Return dv
    > End Function 'CreateDataSource
    >
    > "Volkan Karaboða" <> wrote in message
    > news:%...
    > > Hi all
    > >
    > > I want to change datagrid's row color depending on a cell value
    > >
    > > How can I do this?
    > > I think that I must write some code in bind method but I dont know How I
    > > do
    > > this.
    > >
    > > please help me!!
    > > thanks...
    > >
    > >

    >
     
    Volkan Karaboga, Feb 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franck
    Replies:
    7
    Views:
    3,448
    Franck
    Oct 12, 2005
  2. sck10
    Replies:
    2
    Views:
    1,201
    sck10
    Jan 11, 2007
  3. Thomasa Gregg
    Replies:
    1
    Views:
    166
    Eliyahu Goldin
    Jun 9, 2004
  4. bill
    Replies:
    2
    Views:
    1,127
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    447
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page