5
Answers

Disable hyperlink if cell value null in gridview

vignesh t

vignesh t

7y
146
1
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.
 
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. 
Answers (5)
1
Manav Pandya

Manav Pandya

NA 7.1k 24k 7y
Hello 
 
You can write like this :
 
  1. if (strHeaderRow == "ID")  
  2. {  
  3.     if(strHeaderRow !=null || strHeaderRow != "" )  
  4.     {  
  5.         string strMktURL = "http://www.address.com";  
  6.         HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);  
  7.         hlColumns.visible=true;  
  8.     }  
  9.     else  
  10.     {  
  11.         hlColumns.visible=false;  
  12.     }  
  13. }  
 
 
Thanks 
 
0
Manav Pandya

Manav Pandya

NA 7.1k 24k 7y
ID Column you are using never be null bcoz its text , 
 
Do not convert headertext to text , and than try to check weather its null or not 
0
vignesh t

vignesh t

NA 31 836 7y
I debugged and each time when it comes into the loop it only gives the headercell text and doesn't look for that respective column cell.
0
Manav Pandya

Manav Pandya

NA 7.1k 24k 7y
Try to put debug point and let me show screenshot for that 
 
or else try :
  1. if (strHeaderRow != null || strHeaderRow != "" )    
  2. {    
  3.     string strMktURL = "http://www.address.com";    
  4.     HyperLink hlColumns = AddHyperLink(e.Row.Cells[i], strMktURL);    
  5.     if(strHeaderRow == "ID")    
  6.     {    
  7.         hlColumns.visible=true;    
  8.     }          
  9. }    
  10. else    
  11. {    
  12.        hlColumns.visible=false;    
  13. }  
Thanks 
0
vignesh t

vignesh t

NA 31 836 7y
I tried it and still it shows hyperlink for cell with null values. Also strHeaderRow will have text of header row and I need to check its respective columns values and I don't think that's happening.