Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Sometimes, we need to leave the AutoGenerateColumns = true since, we don't know how many columns will be returned from the Database. Another technique is to use ITemplate interface and make your custom Template Columns during runtime. All the columns that are generated automatically are BoundColumns. Sometimes you want to display the column as a link which will open a new window. In the code below I have added the required HTML to the rows of the DataTable and later linked the bound the table to GridView control. Check out the following code snippet:

 foreach (DataRow r in userRows)
{                  
    
    distinctExamName = (
string)r["Name"] + "_" + r["UserID"].ToString();
   
 sb.Append("<a href='#'");
    sb.Append("onclick = '");
    sb.Append("DisplayExam(");
    sb.Append(Convert.ToInt32(r["UserID"]));
    sb.Append(","); 
    sb.Append(Convert.ToInt32(r["RoleID"])); 
    sb.Append(")'");
    sb.Append(">");
    sb.Append(r["Score"]);
    sb.Append("</a>");
    newRow[distinctExamName] = sb.ToString();
    sb = 
new StringBuilder();                                         
}

Even though you have attached the HTML to the row it will not be displayed as a link. For this, you can use the Row_DataBound event of the GridView control.

 protected void gvGrades_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            
for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)
            {               
                
if (drv.ToString().IndexOf("<a") == 0)
                {
                    e.Row.Cells.Text = Server.HtmlDecode
                    (e.Row.Cells.Text);
                }                              
               
            }
        }
    }

Now, when the GridView is rendered you will see the links displayed in the column. I have attached a Java Script function to the link but you can pretty much do anything you desire.  

Thanks to webswapp for the tip.

powered by IMHO 1.3

Posted on Sunday, April 23, 2006 8:18 PM | Back to top


Comments on this post: Making Links in GridView with AutoGenerateColumns = "True"

# re: Making Links in GridView with AutoGenerateColumns = &quot;True&quot;
Requesting Gravatar...
this is realy good I was looking for it. You made some mistakes in it. like e.Row.Cells.Text should be e.Row.Cells[i].Text & if condition should be
if(e.Row.Cells[i].Text.Contains("<a"))
Left by Saurabh on Jul 18, 2006 11:48 AM

# re: Making Links in GridView with AutoGenerateColumns = &quot;True&quot;
Requesting Gravatar...
Cool... Was looking for some time for the same fragment of codes. Though littered with some mistakes, thanks for the idea. :)
Left by Ifrit on Mar 21, 2007 11:14 AM

# How to generate an Line in the Gridview
Requesting Gravatar...
Pl tell How to generate an Line in the Gridview
sathish.s
Left by Sathish Pothanur on Apr 15, 2008 1:38 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net