There are two ways to write error-free programs; only the third works. --Alan J. Perlis

Adding Columns to DataGrid Programmatically

BoundColumn and HyperLinkColumn

By steve on Monday, January 1, 2007.
Updated Saturday, January 12, 2013.
Viewed 117,144 times. (14 times today.)
DeveloperTips and Tutorials.NET ControlsASP.NETC#SQL Server


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.AutoGenerateColumns = false;

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


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.

For those trying to implement this, you'll notice the GetNorthwindProductTable(). This could be anything that returns a DataTable, but here's a quick example for how to get pull records from the Northwind database.

Snippet 2: Fill a DataSet from a Microsoft SQL Server Database using SQLDataAdapter and DataSet classes

private DataTable GetNorthwindProductTable()
    string connectionString = "workstation id=SERVERMASTER;packet size=4096;" 
    	+ "integrated security=SSPI;data source=SERVERMASTER;"
    	+ "persist security info=False;initial catalog=Northwind";
    string query = "select * from Products";
    SqlDataAdapter da = new SqlDataAdapter(query, connectionString);
    DataSet ds = new DataSet();
    return ds.Tables["Table"];

Back to Top

Share This Page:

comments powered by Disqus
Share This Page:
Welcome to my blog about software development and the Microsoft stack.

I've been a full time .NET developer for fifteen years, but I didn't start my professional life as a programmer ... more