Google
WWW Yariv Hammer's Code Site

Thursday, October 20, 2005

How to use DataGridColumnStyles programatically

Introduction
Sometimes we need to do things in a DataGrid which is not supported automatically. For example: set a column in the DataGrid as Read-only. Or choose what columns in the DataTable to see.
In those cases we need to use DataGridTableStyle and DataGridColumnStyle.
As an introduction I will give a short example of how to use those in code.

Example
We have a DataTable called Transactions which describes transactions in a bank account. This table has three columns: ID, Amount, Balance. The Amount column represents the amount of money in the transaction. The Balance column represents the total money left in the account, it is calculated and should be read-only. The ID column is the primary key in the table and should not be shown to the user.

Code
Here is how I code this using DataGridTableStyle and DataGridColumnStyle:
----------------------------------------

private void Form1_Load(object sender, System.EventArgs e)
{
   //Create table
   DataTable dt = new DataTable("Transactions");
   dt.Columns.Add(new DataColumn("ID",typeof(Int32)));
   dt.Columns.Add(new DataColumn("Amount",typeof(Int32)));
   dt.Columns.Add(new DataColumn("Balance",typeof(Int32)));

   //Initialize objects
   DataGridTableStyle transactionTable = new DataGridTableStyle();
   DataGridTextBoxColumn amountColumn = new DataGridTextBoxColumn();
   DataGridTextBoxColumn balanceColumn = new DataGridTextBoxColumn();

   //Setting a DataView as the DataSource
   dataGrid1.BeginInit();
   DataView dv = new DataView(dt);
   dataGrid1.DataSource = dv;

   // transactionTable
   dataGrid1.TableStyles.AddRange(new DataGridTableStyle[] {transactionTable});
   transactionTable.DataGrid = dataGrid1;
   transactionTable.GridColumnStyles.AddRange(new DataGridColumnStyle[] {amountColumn,balanceColumn});
   transactionTable.MappingName = "Transactions"; // This is the name of the table you are binding to.

   // amountColumn
   amountColumn.HeaderText = "Amount"; // This is the text that is written in the Column Header. Leave empty to show the name of the column.
   amountColumn.MappingName = "Amount"; // This is the name of the    column.amountColumn.ReadOnly = false;
   amountColumn.Width = 75;

   // balanceColumn
   balanceColumn.HeaderText = "Balance";
   balanceColumn.MappingName = "Balance"; // This is the name of the column.
   balanceColumn.ReadOnly = true;
   balanceColumn.Width = 75;

   dataGrid1.EndInit();
}

------------------------------------
Few things are interesting:
  • MappingName - The name of the table/column as it appears in the data source.
  • HeaderText - You can format the header text of the column!!! You cannot do this without DataGridColumnStyle.
  • ReadOnly - You can set an entire column as read-only. The user will not be able to edit the column.
  • Width - You can set the default width of the column.
  • Very important what is NOT in the code: No ColumnStyle for the ID column. So this column will not be shown to the user (although it exists in the DataSource).
In the next article I will show how to paint a cell in the column based on a condition.

1 Comments:

At 4:47 AM, May 12, 2008, Blogger Omar said...

Hello Mr. Yariv,
My name is Omar, I'm new to VB.Net. I was reading you article on your blog, I am wondering if you could help me with your expertice. I'm currently facing a problem. I'm trying to create code for a linklabel that is formed in 1 of the columns (col1,col2 & col3) in a datagrid that querried from a MySql database.and this datagrid is refreshed by a timer. Upon clicking the link label, it should open another windows form (frmMain2)

I'd really appreciate any help.
Thanks & Regards
Omar

 

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.