Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > changing row color depending on a cell value

Reply
Thread Tools

changing row color depending on a cell value

 
 
Volkan Karaboša
Guest
Posts: n/a
 
      02-20-2004
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...


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      02-21-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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...
>
>


 
Reply With Quote
 
 
 
 
Volkan Karaboga
Guest
Posts: n/a
 
      02-23-2004
thank you very much. It works well...



"Ken Cox [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:uzX$biC%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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...
> >
> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing font color from current font color to black color Kamaljeet Saini Ruby 0 02-13-2009 04:58 PM
To populate textfields on a page with the cell contents of a dynamic table depending on the row selected! eureka Javascript 1 01-30-2007 08:06 PM
How to change the style for the row while mouse over the cell of that row? Cylix Javascript 0 06-13-2006 11:06 AM
repeater control - change the background color depending on a value bill ASP .Net Web Controls 2 03-20-2006 08:00 PM
I need to have a tooltip appear over a cell showing data from another cell in the same row. Thomasa Gregg ASP .Net Datagrid Control 1 06-09-2004 08:04 PM



Advertisments