Programming is like sex, one mistake and you have to support it for the rest of your life. --Michael Sinz

Adding Columns to DataGrid Programmatically

BoundColumn and HyperLinkColumn

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"];

}
 

Version: 6.0.20200920.1535