Display link to specific column

Mohit Joshi 1 Reputation point
2022-02-18T05:04:44.8+00:00

I am developing an Application using ASP.NET C#. In it I am populating some Data in Gridview. I want a link on some specific rows based on values of that rows. I don't want link for all the rows but only form some specific rows. I kindly request to provide me exact steps in detail for same ASAP.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,503 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Sreeju Nair 12,361 Reputation points
    2022-02-18T05:55:05.71+00:00

    You can use TemplateField in gridview to control how that column will render the content. When you use TemplateColumn, you can get the data in that row by using

    <%# Eval("ColumnName")%> or <%# Bind("ColumnName") %>
    Refer: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs

    You may use your custom markup to generate the column content. However sometimes you may need to check condition and advanced stuffs, It is possible to call a method with the value and that method returns the contents to display.

    refer: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs#step-4-showing-the-number-of-days-the-employee-has-worked-for-the-company

    Hope this helps

    0 comments No comments

  2. Lan Huang-MSFT 29,746 Reputation points Microsoft Vendor
    2022-02-18T08:27:11.327+00:00

    Hi @Mohit Joshi ,
    For that kind of show/hide logic on specific row, you will need to use GridView.RowDataBound event.
    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowdatabound?redirectedfrom=MSDN&view=netframework-4.8
    1.Use a TemplateField instead of BoundField.
    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.templatefield?redirectedfrom=MSDN&view=netframework-4.8
    2.Place HyperLink control inside.
    3.Then retrieve the HyperLink control inside RowDataBound event.
    Example Code
    176210-1.jpg
    code behind

     protected void Page_Load(object sender, EventArgs e)  
            {  
                SqlConnection conn = new SqlConnection(@"****");  
                if (!IsPostBack)  
                {  
                    DataTable dts = new DataTable();  
                    string str = "select * from Test1";  
                    SqlDataAdapter adp = new SqlDataAdapter(str, conn);  
                    adp.Fill(dts);  
                    GV.DataSource = dts;  
                    GV.DataBind();  
                }  
            }  
            protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)  
            {  
                if (e.Row.RowType == DataControlRowType.DataRow)  
                {  
                    var myHyperLink = e.Row.FindControl("MyHyperLink") as HyperLink;  
                    if(myHyperLink.Text == "Mrs.DEF")  
                    {  
                        myHyperLink.NavigateUrl = "~/Default.aspx";  
                    }  
                    else  
                    {  
                        myHyperLink.NavigateUrl = null;  
                    }  
                }  
            }  
    

    176311-test1.gif
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Rijwan Ansari 751 Reputation points MVP
    2022-02-19T03:13:54.923+00:00

    Hi @Mohit Joshi

    You can use gridview rodatabound.

    Sample:

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)  
    {  
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {  
            // Get client_name from Database.  
            if (Condition)  
            {  
                LinkButton lb1 = (LinkButton)e.Row.FindControl("LinkButton1");  
                lb1.Visible = false; // based on your requirement  
            }  
        }  
    }  
    

    You can check this link https://stackoverflow.com/questions/8410713/how-to-add-dynamic-hyperlink-in-grid-view-according-to-certain-condition

    0 comments No comments

  4. Mohit Joshi 1 Reputation point
    2022-02-21T05:15:29.057+00:00

    All the answers above are not useful for me because I want to achieve different result. I will just elaborate my problem in detail.
    Suppose I have 4 rows in a Gridview for some column as follows: -

    Sr Name
    01 Mr. ABC
    02 Mrs. DEF
    03 Ms. GHI
    04 Miss JKL

    Now from above data I want to show link only for value of 'Mrs. DEF'. I don't want to show link on any other value not even on Sr of DEF. I want a perfect solution to achieve this ASAP.


  5. Sreeju Nair 12,361 Reputation points
    2022-02-21T06:45:26.847+00:00

    Based on your requirement, The solution is to use TemplateField. You may refer the links in my previous Post to understand the details of how this works. To address the specific case you pointed out, I made a sample. I created a gridview as below.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Sr"/>
            <asp:BoundField DataField="Name" HeaderText="Name"/>
            <asp:TemplateField HeaderText="Name - Customized" >
                <ItemTemplate>
                      <%# AddLink(Eval("Name").ToString()) %>                
                </ItemTemplate>Fie
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    See, in the template column, I am calling a method and passing the value of "Name" column as a parameter. Let me show you the code from code behind.

    The following is the Page_Load method.

    protected void Page_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            DataTable table = new DataTable();
            table.TableName = "MyTable";
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name", typeof(string));
    
            table.Rows.Add(2, "Mrs. ABC");
            table.Rows.Add(3, "Mrs. DEF");
            table.Rows.Add(3, "Ms. GHI");
            table.Rows.Add(3, "Miss JKL");
    
            ds.Tables.Add(table);
    
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    

    // Basically I am just defining a Dataset with the the sample data you provided and binding it to the Gridview. You may do it by binding gridview to your datasource. Now for the Addlink method, which I initiated from the TemplateField, see the code below.

    protected string AddLink(string name)
        {
            if(name == "Mrs. DEF")
            {
                return $"<a href='https://www.asp.net'>{name}</a>";
            }
            else
            {
                return name;
            }
    
        }
    

    You may replace the Addlink method with any complex logic you have, you can also pass multiple parameters from templatefield to the codebehind method...

    Hope this helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.