The best performance improvement is the transition from the nonworking state to the working state. --John Ousterhout

Adding Columns to DataGrid Programmatically

BoundColumn and HyperLinkColumn

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

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.

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();
    da.Fill(ds);
    
    return ds.Tables["Table"];

}
Back to Top

Share This Page:


Subscribe for free and read it when and where you want!

Get the latest tips and tools in your Inbox.

Your email address will NEVER be shared and you can unsubscribed at any time using the link at the bottom of every email.

Click Here to Subscribe

(Was this post helpful? If not, please leave a comment and let me know what you would like to hear about.)


 
comments powered by Disqus
Follow:
Share This Page:


Get the latest tips and tools in your Inbox. It's 100% free.



Click Here to Subscribe
Welcome!
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