question

MohitJoshi-6258 avatar image
0 Votes"
MohitJoshi-6258 asked LanHuang-MSFT commented

Display link to specific column

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.

dotnet-aspnet-generaldotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

sreejukg avatar image
0 Votes"
sreejukg answered

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://docs.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://docs.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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT edited

Hi @MohitJoshi-6258,
For that kind of show/hide logic on specific row, you will need to use GridView.RowDataBound event.
https://docs.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://docs.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.



2.jpg (110.1 KiB)
1.jpg (99.7 KiB)
test1.gif (330.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

rijwanansari avatar image
0 Votes"
rijwanansari answered

Hi @MohitJoshi-6258

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MohitJoshi-6258 avatar image
0 Votes"
MohitJoshi-6258 answered LanHuang-MSFT commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MohitJoshi-6258,
How is the effect of your project now, is there any error?
I modified the if statement according to your needs, you can see my answer for the specific content.

 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;
                 }
             }
         }

Best regards,
Lan Huang

0 Votes 0 ·
sreejukg avatar image
0 Votes"
sreejukg answered

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.