Issue dated - 5th April 2004

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
NEWS ANALYSIS
COLUMNS
TECH FORUM

THE C# COLUMN

BETWEEN THE BYTES
TECHNOLOGY
SPECIALS <NEW>
Symantec Report
Security Headquarters
JobsDB
MINDPRINTS
HMA BANKBIZ
EC SERVICES
ARCHIVES/SEARCH
IT APPOINTMENTS
Openings At Jobstreet.com
WRITE TO US
SUBSCRIBE/RENEW
CUSTOMER SERVICE
ADVERTISE
ABOUT US

 Network Sites
  IT People
  Network Magazine
  Business Traveller
  Exp. Hotelier & Caterer
  Exp. Travel & Tourism
  Exp. Pharma Pulse
  Exp. Healthcare Mgmt.
  Express Textile
 Group Sites
  ExpressIndia
  Indian Express
  Financial Express

 
Front Page > TechSpace > Story Print this Page|  Email this page

The C# Column

Grooming the DataGrid Control - II

Last week we customised the DataGrid control by rearranging the order of columns and applying styles to them. We will now see how to change the background and foreground colours of the cells.

The DataGridTextBoxColumn class is responsible for drawing every cell in the DataGrid control. The background and foreground brushes are passed to the Paint event handler in this class. So, to colour the cells with desired brushes, we can change the brushes in the Paint event handler. For this, we must derive a class from the DataGridTextBoxColumn class. We will add an event to this class that is fired when a cell is about to be painted. The handler of this event receives the current row and current column. Here is the derived class, formatcolumn.

class formatcolumn : DataGridTextBoxColumn
{
public event formateventhandler format ;
protected override void Paint ( Graphics g,
Rectangle bounds, CurrencyManager source, int row, Brush backbrush,
Brush forebrush, Boolean alignToRight )
{
int col =
DataGridTableStyle.GridColumnStyles.
IndexOf ( this ) ;
formateventargs e = new formateventargs ( row, col ) ;
format ( this, e ) ;
backbrush = e.Backbrush ;
forebrush = e.Forebrush ;
base.Paint ( g, bounds, source, row,
backbrush, forebrush, alignToRight ) ;
}
}

The class declares the event named format of type formateventhandler. Declare the formateventhandler delegate outside the classes as shown below:

public delegate void formateventhandler( object sender, formateventargs e ) ;

This delegate specifies that the handler of the format event would accept two parameters, first the reference of type object and second reference of type formateventargs. We cannot pass a reference to the predefined EventArgs class because we have to pass the row and column to the format event handler. We would see the formateventargs class later.

In the formatcolumn class we have overridden the Paint event handler. To this handler, the brushes along with the current row are passed. We have obtained the current column using the IndexOf( ) method. After this, we have fired the format event. The format event handler would initialise the brushes with the desired colours. We have obtained these colours from the formateventargs class by calling its properties Backbrush and Forebrush. We have then called the base class’s Paint handler for doing the painting job.

The formateventargs class contains the constructor and few properties. This class is given below.

public class formateventargs : EventArgs
{
private Brush backbrush, forebrush ;
private int r, c ;
public formateventargs ( int row, int col )
{
Row = row ;
Col = col ;
}
public int Row
{
get
{
return r ;
}set
{
r = value ;
}
}
public int Col
{
get
{
return c ;
}
set
{
c = value ;
}
}
public Brush Backbrush
{
get
{
return backbrush ;
}
set
{
backbrush = value ;
}
}
public Brush Forebrush
{
get
{
return forebrush ;
}
set
{
forebrush = value ;
}
}
}

The only thing remaining is handling the event. Add the event handler in the Form1 class. The handler is given below.

private void column_formathandler ( object sender,
formateventargs e )
{
if ( e.Row == dataGrid1.CurrentRowIndex )
{
e.Backbrush = Brushes.Aqua ;
e.Forebrush = Brushes.Red ;
}
else
{
e.Backbrush = new SolidBrush (
dataGrid1.BackColor ) ;
e.Forebrush = new SolidBrush (
dataGrid1.ForeColor ) ;
}
}

This handler gets called for every cell of the grid. So, we have checked if the cell that is getting painted belongs to the currently selected row.

Now modify the Load event handler as shown below. Here, instead of DataGridTextBoxColumn, we have created reference of type formatcolumn class. We have also added the format event to each column added to the control.

private void Form1_Load ( object sender, System.EventArgs e )

{

sqlDataAdapter1.Fill ( ds1 ) ;

DataGridTableStyle ts = new DataGridTableStyle( ) ;

ts.MappingName = “account” ;

DataGridBoolColumn bcol = new

DataGridBoolColumn( ) ;

bcol.MappingName = “ATM” ;

bcol.HeaderText = “” ;

bcol.Width = 30 ;

bcol.AllowNull = false ;

ts.GridColumnStyles.Add ( bcol ) ;

formatcolumn column = new formatcolumn( ) ;

column.MappingName = “accno” ;

column.HeaderText = “Account No.” ;

column.Width = 80 ;

column.format += new formateventhandler

( column_formathandler ) ;

ts.GridColumnStyles.Add ( column ) ;

column = new formatcolumn( ) ;

column.MappingName = “name” ;

column.HeaderText = “Name” ;

column.Width = 110 ;

column.format += new formateventhandler

( column_formathandler ) ;

ts.GridColumnStyles.Add ( column ) ;

column = new formatcolumn( ) ;

column.MappingName = “bal” ;

column.HeaderText = “Balance” ;

column.Width = 110 ;

column.format += new formateventhandler

( column_formathandler ) ;

ts.GridColumnStyles.Add ( column ) ;

dataGrid1.TableStyles.Add ( ts ) ;

}

Execute the program and select a row. The selected row appears as shown in the following figure.

Yashavant Kanetkar, one of the first Express Computer columnists, is an established software expert, speaker and author with several best-sellers to his credit, including titles like “Let Us C” and the “Fundas” series. Contact him at kanetkar@dcubesoft.com
<Back to top>


© Copyright 2003: Indian Express Group (Mumbai, India). All rights reserved throughout the world. This entire site is compiled in
Mumbai by The Business Publications Division of the Indian Express Group of Newspapers.
Please contact our Webmaster for any queries on this site.