We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Change bgcolor of a specific row in a datagrid

Thread ID:

Created:

Updated:

Platform:

Replies:

1745 Nov 22,2002 06:24 AM UTC Mar 29,2003 09:48 PM UTC WinForms 10
loading
Tags: FAQ
Tom
Asked On November 22, 2002 06:24 AM UTC

Hi there, I want to display a datagrid with values from a dataset. That works, but now, I want to color the row red if a certain field is empty so users know that they have to change that row. Can anyone help me? Thanks in advance Tom

Administrator [Syncfusion]
Replied On November 25, 2002 08:49 AM UTC

George Shepherd's Windows Forms FAQ contains an entry entitled: How do I color a individual cell depending upon its value or some external method? Check it out at: http://www.syncfusion.com/faq/winforms/search/745.asp

Tom
Replied On November 26, 2002 07:15 AM UTC

There is a difference: I want to color a whole row depending on one integer value from column1. If that integer > 0, the whole row must colored red. The example checks every cell. And checking an integer doesn't work in the example. My integer fields (ms access autonumber in example) won't be colored at all. > George Shepherd's Windows Forms FAQ contains an entry entitled: > > How do I color a individual cell depending upon its value or some external method? > > Check it out at: > http://www.syncfusion.com/faq/winforms/search/745.asp >

Administrator [Syncfusion]
Replied On November 29, 2002 09:34 AM UTC

I think it is the same thing. To color a row, you have to process the cells 1 at the time, because that is how the datagrid draws them. It does not say I am going to paint the background of such and such row. It says I am going to paint a cell in such and such row. So, the task is, given any cell, does it belong to a row where the cell in column1 is less than zero. If so, give it a special background, if not, don't. So, if you use the last sample in that faq that fires an event for you to handle, your event handler should set the backcolor for the cells that belong to a row where the value in column1 is less than zero. Attached is a sample that does this. It is not quite the same as the FAQ sample, but uses the same idea.

Tom
Replied On November 29, 2002 12:47 PM UTC

Hi Clay, Thanks!!! This works! (I am pretty new to c#) Can you (or someone else) help me with another problem? Thanks to you, I colored the rows with wrong values in it on Form1. I want to display a new Form2 with textboxes when the user clicks on a row of the datagrid. I know how to determine which row is clicked and I know how to open a new form, but I don't know how to display the data in Form2. Anyone a solution? Thanks in advance Tom > I think it is the same thing. > > To color a row, you have to process the cells 1 at the time, because that is how the datagrid draws them. It does not say I am going to paint the background of such and such row. It says I am going to paint a cell in such and such row. So, the task is, given any cell, does it belong to a row where the cell in column1 is less than zero. If so, give it a special background, if not, don't. > > So, if you use the last sample in that faq that fires an event for you to handle, your event handler should set the backcolor for the cells that belong to a row where the value in column1 is less than zero. > > Attached is a sample that does this. It is not quite the same as the FAQ sample, but uses the same idea. >

Ron Fluegge
Replied On March 22, 2003 09:19 PM UTC

The attached sample works great. However, I have been trying to implement it as a VB program by replacing the Form1.cs with a Form1.vb. The CustomColumnStyles.cs remains unmodified. Everything seems to "translate" fine except for cs.SetCellFormat += new FormatCellEventHandler(ColorRow); I have tried everything I could find, but keep getting errors. What I have tried includes: AddHandler cs.SetCellFormat, AddressOf DGColorRow.NewCellEventHandler With and without parameters, etc. Can't make it work. Any suggestions would be very much appreciated. > Attached is a sample that does this. It is not quite the same as the FAQ sample, but uses the same idea. >

Ron Fluegge
Replied On March 22, 2003 09:22 PM UTC

The earlier reply should have been: AddHandler cs.SetCellFormat, AddressOf DGColorRow.FormatCellEventHandler > The attached sample works great. However, I have been trying to implement it as a VB program by replacing the Form1.cs with a Form1.vb. The CustomColumnStyles.cs remains unmodified. > > Everything seems to "translate" fine except for > > cs.SetCellFormat += new FormatCellEventHandler(ColorRow); > > I have tried everything I could find, but keep getting errors. > > What I have tried includes: > > AddHandler cs.SetCellFormat, AddressOf DGColorRow.NewCellEventHandler > > With and without parameters, etc. > > Can't make it work. Any suggestions would be very much appreciated. > > > > Attached is a sample that does this. It is not quite the same as the FAQ sample, but uses the same idea. > > >

Ron Fluegge
Replied On March 24, 2003 08:09 PM UTC

After reading more about delegates and events, I changed the line: cs.SetCellFormat += new FormatCellEventHandler(ColorRow); to (in VB.Net) AddHandler cs.SetCellFormat, AddressOf ColorRow and it works fine "except" for the use of e.ForeBrush. When the ColorRow "sub" sets the e.ForeBrush = Me.RowBrushText, the cell "alignment" is changed. When using e.BackBrush = ..., the "cell" alignments stay as defined in the VB code with cs.Alignment = HorizontalAlignment.Center, for example. I'll keep working on it and provide any fixes or "insight".

jrdn
Replied On March 29, 2003 09:12 PM UTC

not only does assigning to e.forebrush nix alignment settings, all data format settings (date format or currency format) get canceled as well. Does anyone know how to maintain data/text formatting while assigning text color? thanks > After reading more about delegates and events, I changed the line: > > cs.SetCellFormat += new FormatCellEventHandler(ColorRow); > > to (in VB.Net) > > AddHandler cs.SetCellFormat, AddressOf ColorRow > > and it works fine "except" for the use of e.ForeBrush. > > When the ColorRow "sub" sets the e.ForeBrush = Me.RowBrushText, the cell "alignment" is changed. When using e.BackBrush = ..., the "cell" alignments stay as defined in the VB code with cs.Alignment = HorizontalAlignment.Center, for example. > > I'll keep working on it and provide any fixes or "insight". >

jrdn
Replied On March 29, 2003 09:45 PM UTC

I figured out a way to keep the formatting: comment out all of the drawstring part of the paint function and simply assign the forebrush if not null. The question then is: whats the point of calling drawstring when assigning to forebrush? attached is my updated custom column style source > not only does assigning to e.forebrush nix alignment settings, all data format settings (date format or currency format) get canceled as well. Does anyone know how to maintain data/text formatting while assigning text color? > thanks > > > > After reading more about delegates and events, I changed the line: > > > > cs.SetCellFormat += new FormatCellEventHandler(ColorRow); > > > > to (in VB.Net) > > > > AddHandler cs.SetCellFormat, AddressOf ColorRow > > > > and it works fine "except" for the use of e.ForeBrush. > > > > When the ColorRow "sub" sets the e.ForeBrush = Me.RowBrushText, the cell "alignment" is changed. When using e.BackBrush = ..., the "cell" alignments stay as defined in the VB code with cs.Alignment = HorizontalAlignment.Center, for example. > > > > I'll keep working on it and provide any fixes or "insight". > > >

jrdn
Replied On March 29, 2003 09:48 PM UTC

I guess that attachment didnt work. here's my version of Paint: //used to fire an event to retrieve formatting info //and then draw the cell with this formatting info protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight) { DataGridFormatCellEventArgs e = null; bool callBaseClass = true; //fire the formatting event if(SetCellFormat != null) { int col = this.DataGridTableStyle.GridColumnStyles.IndexOf(this); e = new DataGridFormatCellEventArgs(rowNum, col, this.GetColumnValueAtRow(source, rowNum)); SetCellFormat(this, e); if(e.BackBrush != null) backBrush = e.BackBrush; if (e.ForeBrush != null) foreBrush = e.ForeBrush; /** //if these properties set, then must call drawstring if(e.ForeBrush != null || e.TextFont != null) { if(e.ForeBrush == null) e.ForeBrush = foreBrush; if(e.TextFont == null) e.TextFont = this.DataGridTableStyle.DataGrid.Font; g.FillRectangle(backBrush, bounds); Region saveRegion = g.Clip; Rectangle rect = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height); using(Region newRegion = new Region(rect)) { g.Clip = newRegion; int charWidth = (int) Math.Ceiling(g.MeasureString("c", e.TextFont, 20, StringFormat.GenericTypographic).Width); string s = this.GetColumnValueAtRow(source, rowNum).ToString(); int maxChars = Math.Min(s.Length, (bounds.Width / charWidth)); try { g.DrawString(s.Substring(0, maxChars), e.TextFont, e.ForeBrush, bounds.X, bounds.Y + 2); } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); } //empty catch finally { g.Clip = saveRegion; } } callBaseClass = false; } **/ if(!e.UseBaseClassDrawing) { callBaseClass = false; } } if(callBaseClass) base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight); //clean up if(e != null) { if(e.BackBrushDispose) e.BackBrush.Dispose(); if(e.ForeBrushDispose) e.ForeBrush.Dispose(); if(e.TextFontDispose) e.TextFont.Dispose(); } }

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;