Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > .NET Framework 1.x
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 12th, 2007, 03:03 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default change row color of DataGrid in VB.Net (Windows ap

i'm using VB.NET. and its a windows application.

in my program i need to display a table in datagrid. so i created the table using DataTable and i'm calling that dataTable to dataGrid. and its working fine...

but according to one of the field in that table i need to change the back color of that row. one of the field in that table called "Active", it will have value as True or False. so if Active =True then i need that corresponding row to be turned Pink. and if Active = False then i need that corresponding row to be white.

and i search lots in net... but didnt get much help at all...

this the code i'm using now to create table and displaying it inside Datagrid.
Code:
 Dim Table1 As DataTable = New DataTable("Orders")
        Dim Row As DataRow

        Dim EmpName As DataColumn = New DataColumn("EmpName")
       Table1.Columns.Add(EmpName)

        Dim EmployeeID As DataColumn = New DataColumn("EmpID")
        EmployeeID.DataType = System.Type.GetType("System.Int32")
        Table1.Columns.Add(EmployeeID)

        Dim CardNo As DataColumn = New DataColumn("CardNo")
        CardNo.DataType = System.Type.GetType("System.Int32")
        Table1.Columns.Add(CardNo)

        Dim Active As DataColumn = New DataColumn("Active")
        Table1.Columns.Add(Active)

        Dim i As Integer = 0

        Dim strSQL As String = "Select LastName, FirstName, EmpID, CardNo, Active from Employees order by LastName asc"
        myConnection.Open()
        Dim myCommand As New OleDbCommand(strSQL, myConnection)
        Dim myReader As OleDbDataReader = myCommand.ExecuteReader

       While myReader.Read
            Row = Table1.NewRow()
            Row("EmpName") = myReader(0) & "," & myReader(1)
            Row("EmpID") = myReader(2)
            Row("Card No") = myReader(3)
            If myReader(4) = 0 Then
                Row("Active") = "True"
            ElseIf myReader(4) = 1 Then
                Row("Active") = "False"
            End If
            Table1.Rows.Add(Row)
         End While

        Dim objDataView As New DataView(Table1)
        DataGrid1.DataSource = objDataView
        myConnection.Close()
how can i change the back color of rows in DataGrid when Active=True... and i need to change only that particular row's back color were Active=True...

if you have any idea how to do this please help me... and if you can provide an example, then it will be great help for me...

thanks in advance.
Reply With Quote
  #2 (permalink)  
Old October 15th, 2007, 12:49 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

while searching i found out a link. and this is that link i got...

http://www.syncfusion.com/FAQ/Window...44c.aspx#q745q

here its using DataSet and it will check each Columns and if first alphabet in column is greater than "F" then it will change the colour of that column. but not the entire row.

in my program i need to change the colour of row according to the Field name Active. but i don't need to display the field Active. only the remaining fields i need to display and active is used to check if Active is True, then Row colour should be Pink and if Active is False then row colour is white.

and i tried using DataTable instead of DataSet in that example but wont change the colour at all... but if we use DataSet then it will change colour.

i'm new to programming... so you have any idea how to do this please let me know... and if you can provide any help then it will be great helpfull for me.

thanks in advance.
Reply With Quote
  #3 (permalink)  
Old December 31st, 2007, 03:56 AM
jomet
Guest
 
Posts: n/a
Default


Use the RowDataBound Event... of GridView

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

     If e.Row.RowType = DataControlRowType.DataRow Then


         'retrieve the dataitem, and cast it to the appropriate type

         'if you bound the GridView to a SqlDataSource then your dataitem is of type DataRowView

         Dim drv As DataRowView = TryCast(e.Row.DataItem, DataRowView)

         If drv("Active").ToString().Equals("True") Then

             e.Row.BackColor = System.Drawing.Color.Pink
     Else
             e.Row.BackColor = System.Drawing.Color.White

         End If
     End If

 End Sub

I dont got time to test it
from one of my old resource i took this
bye

jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid row color changing - windows vb.net app badgolfer VS.NET 2002/2003 7 April 18th, 2007 02:32 PM
how can i change row color in datagrid? berzegerol VS.NET 2002/2003 3 October 31st, 2006 09:08 AM
datagrid row color change berzegerol Visual Studio 2005 0 October 18th, 2006 06:33 AM
change row color vivek_pon VS.NET 2002/2003 3 April 13th, 2006 01:42 PM
change the color of a row in a Datagrid bimal General .NET 1 October 6th, 2004 03:12 PM



All times are GMT -4. The time now is 03:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.