Formatting Hyperlink columns in datagrid control
-
Does anyone know how to custom format the hyperlink columns in datagrid control? I want to display only selected values in the cells, as hyperlink if some condition is met. VB,ASP, C#, ASP.NET, VB.NET, Oracle, SQL Server. -------------------------------------------------- Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
-
Does anyone know how to custom format the hyperlink columns in datagrid control? I want to display only selected values in the cells, as hyperlink if some condition is met. VB,ASP, C#, ASP.NET, VB.NET, Oracle, SQL Server. -------------------------------------------------- Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
That type of column is rendered as HyperLink control. To manipulate what is show in the row do implement the DataGrid's ItemDataBound event. Here is the small example:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
// data item represented by the DataGridItem object
DataRowView drv = e.Item.DataItem as DataRowView;
if (drv == null) return;// drv.Row represents the ROW // read value from "WebsiteID" column (type of int) int i = (int) drv.Row\["WebsiteID"\]; // calculate something... bool b = (i % 2) == 1; // get HyperLink column object HyperLink hl = e.Item.Cells\[0\].Controls\[0\] as HyperLink; // set NavigateUrl if (!b) hl.NavigateUrl = "";
}
}In this example, the first DataGrid's column (index = 0) is a HyperLink column (accessor: e.Item.Cells[0]). That type of column has only one controls in the colection, type of HyperLink.
There is a good example in MSDN, see "DataGridItemEventArgs class, about DataGridItemEventArgs class" topic. -- Mariusz 'mAv' Wójcik master e-software engineer (BPC)