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 General / September 2007

Tip: Looking for answers? Try searching our database.

Set the row background color according to a cell value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dreamkeys@gmail.com - 26 Sep 2007 17:57 GMT
Hi,

At this time I'm able to set the background color of a cell depending
on it's value. I'm able to achieve this by extending the
DataGridTextBoxColumn class and overriding the Paint method:

[code]
       protected override void Paint(System.Drawing.Graphics g,
System.Drawing.Rectangle bounds, CurrencyManager source, int rowNum,
System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool
alignToRight)
       {
           if (internalHighlightInformation != null)
           {
               object objAtRow = GetColumnValueAtRow(source, rowNum);

                //(Values A, C and X are just an example, this values
are set at run-time)
               if (objAtRow = "B" || objAtRow = "C" || objAtRow =
"X")
               {

                   backBrush =  new SolidBrush(Color.Red);
                   foreBrush = new SolidBrush(Color.White);
               }
           }

           base.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight);
       }
[/code]

My problem now is, how can I do this to the full row? At this stage I
know the row number, and the cell, but I don't have any access to the
cells that exist in the same row :( (or do I?)

Any ideas? I just cannot figure out how to do this...

Thanks ahead for all the help.

Best Regards,

L. Pinho
Trust Me; I'm from the government - 26 Sep 2007 19:52 GMT
Use the GridViewRowEventArgs event
I realize you're a C# guy, and the sample I'm going to show you is VB.Net,
but it should point you in the correct direction:
http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=GVConditionalRowC
oloring


David Wier
http://aspnet101.com
http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
bloated markup

> Hi,
>
[quoted text clipped - 39 lines]
>
> L. Pinho
dreamkeys@gmail.com - 27 Sep 2007 09:25 GMT
On Sep 26, 7:52 pm, "Trust Me; I'm from the government"
<s...@here.com> wrote:
> Use the GridViewRowEventArgs event
> I realize you're a C# guy, and the sample I'm going to show you is VB.Net,
[quoted text clipped - 50 lines]
>
> > L. Pinho

Hi,
thanks for the reply

I'm trying to do this at a windows forms level, I don't seem to find
the DoColor event/method anywhere, is a part of which object?

Thanks
Bob Powell [MVP] - 30 Sep 2007 13:14 GMT
This can be accomplished by handling the RowPrePaint event and setting the
background color of the cell accordingly.

void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)

{

if(e.RowIndex>-1)

{

someclass sc = (someclass)dataGridView1.Rows[e.RowIndex].DataBoundItem;

this.dataGridView1.Rows[e.RowIndex].Cells[0].Style.BackColor = sc.Color;

}

}

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Hi,
>
[quoted text clipped - 39 lines]
>
> L. Pinho

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.