Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
There's no sense being exact about something if you don't even know what you're talking about. --John von Neumann

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:05:47 PM CT Jan 07, 2007
Posted:11:54 PM CT Jan 01, 2007

Adding Columns to DataGrid Programmatically

BoundColumn and HyperLinkColumn

Author: Steve Lautenschlager

SnippetDatabaseC#Visual Studio.NET 1.1
    Prev     1    2     Next  

 Summary

Controlling the data you add to datagrids can be one of the most cumbersome and annoying things you'll do in ASP.NET, but here I present how simple it can be to add basic text or link columns. This is a great starting point if you are new to DataGrids. With a little work, you'll find the datagrid is an extremely powerful tool.

Keep your eye out for .NET 2.0 where this is all simplified dramatically.
For the following example, drag a DataGrid control onto your ASP.NET page named DataGrid1. Then call the following method from the Page_Load event handler.

Snippet 1: Adding BoundColumn and HyperLinkColumn to DataGrid

public void TestHyperLinkColumn()
{
	// First add a simple bound column
	BoundColumn nameColumn = new BoundColumn();
	nameColumn.DataField = "ProductName";
	nameColumn.DataFormatString = "{0}";
	nameColumn.HeaderText = "Product";

	// Now add the HyperLink column
	HyperLinkColumn linkColumn = new HyperLinkColumn();
	linkColumn.DataTextField = "ProductName";
	linkColumn.DataTextFormatString = "{0} Details";
	linkColumn.DataNavigateUrlField = "ProductID";
	linkColumn.DataNavigateUrlFormatString = "/MyApp/ProductDetails.aspx={0}";
	linkColumn.HeaderText = "Details";

	// Add the link in a BoundColumn
	// where the text can be the same for all rows
	BoundColumn blinkColumn = new BoundColumn();
	blinkColumn.DataField = "ProductID";
	blinkColumn.DataFormatString = "<a href='/MyApp/ProductDetails.aspx={0}'>Details</a>";
	blinkColumn.HeaderText = "Details";

	DataGrid1.Columns.Add(nameColumn);
	DataGrid1.Columns.Add(linkColumn);
	DataGrid1.Columns.Add(blinkColumn);
	DataGrid1.AutoGenerateColumns = false;

	DataTable dt = GetNorthwindProductTable();
	DataGrid1.DataSource = dt;
	DataGrid1.DataBind();

}
Note that I added three columns. The first was a simple text column (BoundColumn) with the product name. For the second column I added a link to a product details page using the HyperLinkColumn. For the third column I showed an alternate way of adding a link column if the link text can be the same for all rows, such as "Details". Just added the HTML link tag as text to the BoundColumn. It will be rendered as an HTML link when you view the page.
    Prev     1    2     Next  

Add New Comment
Adding Columns to DataGrid Programmatically
Srinivas Reddy04 Jul 07, 5:53Reply 
re: Adding Columns to DataGrid Programmatically
Steve Lautenschlager15 Sep 07, 22:21Reply 
re: Adding Columns to DataGrid Programmatically
Spencer Sullivan01 May 08, 20:08Reply 
Adding Columns to DataGrid Programmatically
Shal25 Jun 08, 13:17Reply 
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve is the founder and creator of Cambia Research. Developing and maintaining the site combines his passions for technology, writing and education.
Steve holds a Ph.D. in particle physics from Duke University, has worked at CERN, the European center for particle physics (where the web was born) and in Microsoft's web division with microsoft.com, msnbc.com and other web properties. Steve is a web consultant specializing in Microsoft.NET technologies. Read more here.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com