Issue dated - 22nd March 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

Programmers are never satisfied with what is shipped with a development environment. They always require something more than what is available to create an application using that environment. For example, instead of normal menus, we may want menus to appear sliding. Instead of the blue coloured menu selection bar, we may want to change it to suit the current skin of the application window. In short, developers try to customise the available controls to suit their requirements. If you are thinking of customising the DataGrid control to give it a different look then this article will prove helpful to you.

In this article we will change the background and foreground color of the currently selected row in the DataGrid control. We will also rearrange the sequence of the columns before displaying the data in the table.

Create a WinForm application and place the DataGrid control on it. Open the Server Explorer window. Go to the table, which you want to provide as data source. Drag and drop the table on the DataGrid control. If it requires a password to access the data source, a message box will be popped up to ask you whether you want the password to get displayed in source code or not. Say yes to it otherwise the connection string will lack the password and a connection to the data source will not get established. The following references will get added as data members of the Form1 class.

private.SqlCommand sqlSelectCommand1;
private SqlCommand sqlInsertCommand1;
private SqlConnection sqlConnection1;
private SqlDataAdapter sqlDataAdapter1;

The command string for SELECT and INSERT statements as well as the connection string also get created.

Now right-click on the SqlDataAdapter1 component and select ‘Generate DataSet’ from the pop-up menu. The ‘Generate DataSet’ dialog box appears. Change the DataSet name to ds1 and click ‘OK’. This adds a DataSet reference to the component tray.

Add the Load event handler and write the following statement in it.

sqlDataAdapter1.Fill ( ds1 ) ;

The InitializeComponent( ) method contains the code to connect to the data source. Check whether the connection string is constructed properly and then run the program. You will see the datagrid control filled with data as shown in the following figure.

The control in our form shows records of the ‘bank’ database. For the field having datatype bit (or boolean) DataGrid control displays a checkbox. Records having value 1 (or true) in that field appear checked.

Let us now start the customisation work. Firstly, we will arrange the order in which columns should appear. By default DataGrid shows the columns in order specified in the SQL query. If we want to change this order we can change the query.

However, what if the order of columns is dependent on an action taken by the user? This can be managed by using the DataGridTableStyle class. Don’t confuse this class with the table (datasource) displayed in the DataGrid control. The DataGridTableStyle class represents the grid drawn in the control.

Therefore, to customise the appearance of the grid for a DataTable, we can use the DataGridTableStyle class. To associate the name of the table with a table style the MappingName property of the DataGridTableStyle class is initialised with the table name i.e. the name of the data source. Name of our table happens to be ‘account’.

The DataGrid class has a property called TableStyles. It returns a reference to the object of the GridTableStylesCollection class. An object of the GridTableStylesCollection class holds a collection of the objects of DataGridTableStyle class. Each object can specify different table styles.

The DataGridTableStyle class has a property called GridColumnStyles. It returns a reference to an object of the GridColumnStylesCollection class. This collection holds objects of the DataGridColumnStyle class. We can customise the appearance of a column through the DataGridColumnStyle class. To associate a column style with a column in the data table, we must initialise the MappingName property of the DataGridColumnStyle class with the column name.

We cannot instantiate the DataGridColumnStyle class because it is an abstract class. Instead we use the derived classes DataGridTextBoxColumn or the DataGridBoolColumn. The DataGridBoolColumn makes the column look and behave like a check box. This class is useful when we have a field of type bool in the data source.

We will now write the code to customise the columns, add the column styles in the columns style collection maintained in the table style object, and then add the table style to the datagrid control. This code is to be written in the Load event handler.

DataGridTableStyle ts = new DataGridTableStyle( ) ;
ts.MappingName = “account” ;
DataGridBoolColumn bcol = new DataGridBoolColumn( ) ;
bcol.MappingName = “ATM” ;
bcol.Width = 30 ;
bcol.AllowNull = false ;
ts.GridColumnStyles.Add ( bcol ) ;
DataGridTextBoxColumn column = new DataGridTextBoxColumn( ) ;
column.MappingName = “accno” ;
column.HeaderText = “Account No.” ;
column.Width = 80 ;
ts.GridColumnStyles.Add ( column ) ;
column = new DataGridTextBoxColumn( ) ;
column.MappingName = “name” ;
column.HeaderText = “Name” ;
column.Width = 110 ;
ts.GridColumnStyles.Add ( column ) ;
column = new DataGridTextBoxColumn( ) ;
column.MappingName = “bal” ;
column.HeaderText = “Balance” ;
column.Width = 110 ;
ts.GridColumnStyles.Add ( column ) ;
dataGrid1.TableStyles.Add ( ts ) ;

We have changed the order of columns as ATM, accno, name and bal. Here, we have used the properties like HeaderText and Width to change the column heading and width of the columns respectively. The Add( ) method is used to add the column and table style to the respective collections. Now run the program. You will see the columns arranged in the order set in the table style.

Next week we will customise the background and foreground color of the selected row.

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.