Click here to Skip to main content
15,897,273 members
Articles / Programming Languages / C#

Formatting the Datarow based on a single cell value and Custom Table Styles.

Rate me:
Please Sign up or sign in to vote.
4.00/5 (12 votes)
28 Apr 20032 min read 122.6K   1.9K   24  
An article that demonstrates row formatting and adding custom styles to the designer.
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace ColoredRowTestApp
{

	public class FormatEventArgs : EventArgs 
	{ 
		public int RowNum ; 
		public Brush BackBrush; 
		public bool strikeThrough = false ; 
		
		
		
		public FormatEventArgs( int Rownum) 
		{ 
			this.RowNum = Rownum ;
			BackBrush = new SolidBrush(Color.White);
			

		} 


	} 

	public delegate void CellPaint(object o, ref FormatEventArgs e);
	/// <summary>
	/// Summary description for FormattedTextBoxColumn.
	/// </summary>
	public class FormattedTextBoxColumn :  DataGridTextBoxColumn
	{
		public CellPaint _handle ; 
		public CellPaint PaintHandle { 
			get 
			{ 
				return _handle ;
			} 
			set 
			{ 
				_handle = value; 
			} 
		}  
		public FormattedTextBoxColumn()
		{
			//
			// TODO: Add constructor logic here
			//
			_handle = null ; 
		}
		protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum) 
		{
			object data = (object) GetColumnValueAtRow(Source, RowNum);
			String strData = data.ToString() ; 
			FormatEventArgs e = new FormatEventArgs(RowNum) ;

			if( _handle != null) 
				_handle(new object(),ref e) ; 
			
			g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

			System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif , (float)8.25 );
			g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y );

		}

		protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum,bool AlignToRight) 
		{
			object data = (object) GetColumnValueAtRow(Source, RowNum);
			String strData = data.ToString() ; 
			FormatEventArgs e = new FormatEventArgs(RowNum) ;
			
			if( _handle != null) 
				_handle(new object(),ref e) ; 
			

			g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

			System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif , (float)8.25 );
			g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y );

		}

		protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight) 
		{
			Object data = ( Object ) GetColumnValueAtRow(Source, RowNum);	
			String strData ; 
			
			strData = data.ToString() ; 

			FormatEventArgs e = new FormatEventArgs(RowNum) ;
 
			if( _handle != null) 
				_handle(new object(),ref e) ; 
			
		
			g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
			
			FontStyle fs = FontStyle.Regular ; 
			if( e.strikeThrough == true ) 
			{ 
				fs = FontStyle.Strikeout ; 
			}
			System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.25 ,fs);
			g.DrawString( strData ,font ,Brushes.Green ,Bounds.X ,Bounds.Y );

		}

		protected  void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight,bool changeColor) 
		{
			object data = (object) GetColumnValueAtRow(Source, RowNum);
			String strData = data.ToString() ; 
			FormatEventArgs e = new FormatEventArgs(RowNum) ;
		
			if( _handle != null) 
				_handle(new object(),ref e) ; 
			

			
			g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

			System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif , (float)8.25 );
			g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y );

		}
		
		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions