By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,105 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,105 IT Pros & Developers. It's quick & easy.

change row color of DataGrid in VB.Net (Windows application)

100+
P: 115
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.
Expand|Select|Wrap|Line Numbers
  1.  Dim Table1 As DataTable = New DataTable("Orders")
  2.         Dim Row As DataRow
  3.  
  4.         Dim EmpName As DataColumn = New DataColumn("EmpName")
  5.        Table1.Columns.Add(EmpName)
  6.  
  7.         Dim EmployeeID As DataColumn = New DataColumn("EmpID")
  8.         EmployeeID.DataType = System.Type.GetType("System.Int32")
  9.         Table1.Columns.Add(EmployeeID)
  10.  
  11.         Dim CardNo As DataColumn = New DataColumn("CardNo")
  12.         CardNo.DataType = System.Type.GetType("System.Int32")
  13.         Table1.Columns.Add(CardNo)
  14.  
  15.         Dim Active As DataColumn = New DataColumn("Active")
  16.         Table1.Columns.Add(Active)
  17.  
  18.         Dim i As Integer = 0
  19.  
  20.         Dim strSQL As String = "Select LastName, FirstName, EmpID, CardNo, Active from Employees order by LastName asc"
  21.         myConnection.Open()
  22.         Dim myCommand As New OleDbCommand(strSQL, myConnection)
  23.         Dim myReader As OleDbDataReader = myCommand.ExecuteReader
  24.  
  25.        While myReader.Read
  26.             Row = Table1.NewRow()
  27.             Row("EmpName") = myReader(0) & "," & myReader(1)
  28.             Row("EmpID") = myReader(2)
  29.             Row("Card No") = myReader(3)
  30.             If myReader(4) = 0 Then
  31.                 Row("Active") = "True"
  32.             ElseIf myReader(4) = 1 Then
  33.                 Row("Active") = "False"
  34.             End If
  35.             Table1.Rows.Add(Row)
  36.          End While
  37.  
  38.         Dim objDataView As New DataView(Table1)
  39.         DataGrid1.DataSource = objDataView
  40.         myConnection.Close()
  41.  
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.
Oct 12 '07 #1
Share this Question
Share on Google+
2 Replies


kenobewan
Expert 2.5K+
P: 4,871
I think there was a recent similar problem that used dhtml, try searching the site.
Oct 13 '07 #2

100+
P: 115
while searching i found out a link. and this is that link i got...

http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.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.
Oct 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.