Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / February 2004

Tip: Looking for answers? Try searching our database.

changing row color depending on a cell value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Volkan Karabo?a - 20 Feb 2004 10:09 GMT
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] - 21 Feb 2004 03:13 GMT
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

> Hi all
>
[quoted text clipped - 7 lines]
> please help me!!
> thanks...
Volkan Karaboga - 23 Feb 2004 07:47 GMT
thank you very much. It works well...

> Hi Volkan,
>
[quoted text clipped - 61 lines]
> > please help me!!
> > thanks...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.