Reputation: 141
I have a grid where columns are populated dynamically. I have a column called ID which will have hyperlink enabled and hyperlinks needs to be disabled if cells value is null or empty.
For ex:
If a cell value returns 0
or null values for that ID column then I need to disable the hyperlink for those cells in that columns. The below code adds hyperlink successfully and top of it I need to check for null
or 0
values in the cell.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string strHeaderRow = GridView1.HeaderRow.Cells[i].Text;
if (strHeaderRow == "ID")
{
string strMktURL = "http://www.address.com";
HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);
}
}
}
}
protected HyperLink AddHyperLink(TableCell cell, string strURL)
{
HyperLink hl = new HyperLink();
hl.Text = cell.Text;
hl.Font.Underline = true;
hl.Target = "_blank";
hl.NavigateUrl = strURL;
hl.Attributes.Add("style", "color:Black;");
cell.Controls.Add(hl);
return hl;
}
Please suggest how this can be achieved.
Upvotes: 0
Views: 706
Reputation: 7213
Just check for your cell value is null
or empty and don't make it hyperlink.
Also I will suggest to make your AddHyperLink()
void, if you will not use its returned value.
TableCell
stores its value into string Text
property, so:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strMktURL = "http://www.address.com";
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
string strHeaderRow = GridView1.HeaderRow.Cells[i].Text;
if (strHeaderRow == "ID")
{
if(!string.IsNullOrEmpty(e.Row.Cells[i].Text))
{
HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);
}
}
}
}
}
Upvotes: 1