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 / Windows Forms / WinForm Controls / October 2004

Tip: Looking for answers? Try searching our database.

How do i color a column in a DataGrid?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aaa - 28 Oct 2004 16:09 GMT
I have to admit I have never seen a more convaluted Object Model all i want
to do is color one column in a grid...thats it.

I have downloaded several  examples from the web but none that I have been
able to integrate into my project is there a straight forward way to color
one column in a DataGrid...PLEASE!
JS - 28 Oct 2004 19:55 GMT
Sadly it appears there is not.
You are going to have to either
1) inherit a custom grid column style or
2) Buy a third party control. (And if you can figure out a way to get a
refung from MS for the datagrid, let me know ;)   )

I feel the same way. In the end I bit the bullet and used the inherited grid
style.
A good example of variations on the technique can be is found at
http://www.syncfusion.com/faq/winforms/search/745.asp

What the article doesn't list is the code to add the custom style to your
grid.
Once you create the custom grid syle, you can test it using this code:
Drop a datagrid on a form and add this code somewhere.

       Dim dc As DataColumn
       Dim dt As New DataTable("person")
       Dim dr As DataRow

       dc = New DataColumn("name", GetType(String))
       dt.Columns.Add(dc)
       dc = New DataColumn("age", GetType(Int32))
       dt.Columns.Add(dc)

       dr = dt.NewRow
       dr(0) = "fred smith"
       dr(1) = 55
       dt.Rows.Add(dr)

       dr = dt.NewRow
       dr(0) = "june jones"
       dr(1) = 34
       dt.Rows.Add(dr)

       dr = dt.NewRow
       dr(0) = "bob brown"
       dr(1) = 73
       dt.Rows.Add(dr)

       Dim ts As New DataGridTableStyle
       ts.MappingName = dt.TableName

       Dim coltext As New DataGridTextBoxColumn
       coltext.MappingName = dt.Columns(0).ColumnName
       coltext.Width = 60
       coltext.HeaderText = "Person's Name"
       ts.GridColumnStyles.Add(coltext)

       Dim colcustom As New CustomGridColStyle
       colcustom.MappingName = dt.Columns(1).ColumnName
       coltext.HeaderText = "Person's Age"
       coltext.Width = 40
       ts.GridColumnStyles.Add(colcustom)

       Me.DataGrid1.TableStyles.Clear()
       Me.DataGrid1.TableStyles.Add(ts)

       DataGrid1.DataSource = dt

Good luck. It takes a while to get the hang of.

Jonathan Steinberg
BRCorp
jsteinberg@NOTBRCorp.com

> I have to admit I have never seen a more convaluted Object Model all i want
> to do is color one column in a grid...thats it.
>
> I have downloaded several  examples from the web but none that I have been
> able to integrate into my project is there a straight forward way to color
> one column in a DataGrid...PLEASE!

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



©2008 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.