-2

I am trying to add linkbutton with URL to gridview subtotal row. I'm using the below code and not able to add URL.

Can anyone please provide suggestions on how this can be handled?

Update 1:

    private void AddTotalRow(string labelText, string value)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
        row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
        row.Cells.AddRange(new TableCell[3] { new TableCell{ Text = labelText, HorizontalAlign = HorizontalAlign.Right },
     new TableCell{ Text = value, HorizontalAlign = HorizontalAlign.Right },
     HyperLinkCell(value, "http://www.google.com") });
    }

    protected  TableCell HyperLinkCell(string text, string url)
    {
        TableCell cell = new TableCell();
        HyperLink link = new HyperLink();
        try
        {
            link.Text = text;
            link.Font.Underline = true;
            link.Target = "_blank";
            link.NavigateUrl = url;
            link.Attributes.Add("style", "color:Black;");
            cell.Controls.Add(link);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return cell;
    }
Learner
  • 353
  • 9
  • 37
  • you have `string value` and you are passing it to `AddHyperLink` method. but `AddHyperLink` method has `TableCell` as first argument. So you can not pass string instead of TableCell there. That's why you are seeing this error. – Chetan May 10 '18 at 10:27
  • @ChetanRanpariya - Updated my post. Can you please look into it. Not able to pass URL. – Learner May 10 '18 at 10:31
  • While creating new LinkButton you are setting `Text = AddHyperLink`. AddHyperLink method returns HyperLink object which obviously you can not assign to Text coz it is a string type. – Chetan May 10 '18 at 10:32
  • You need to go back to the basic and understand the datatypes, assignments and error messages. Also you need to share your code in the question instead of image. we can not copy paste your code from image. – Chetan May 10 '18 at 10:35

2 Answers2

1

You want to add new cells to the table. But you are also trying to add LinkButton to the TableCell collection.

Also While creating new LinkButton you are setting Text = AddHyperLink. AddHyperLink method returns HyperLink object which obviously you can not assign to Text coz it is a string type.

Also you can not add LinkButton to the TableCell collection.

You need to change your code as following.

private void AddTotalRow(string labelText, string value)
{
     GridViewRow row = new GridViewRow(0,0, DataControlwRowType.DataRow, DataControlRowState.Normal);
     row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
     row.Cells.AddRange(new TableCell[4] { new TableCell(),
         new TableCell{ Text = labelText, HorizontalAlign = HorizontalAlign.Right }.
         new TableCell{ Text = value, HorizontalAlign = HorizontalAlign.Right),
         //Calling HyperLinkCell method which will return a TableCell with HyperLink in it.
         HyperLinkCell(value, "http://www.google.com")
        });

    gvData.Rows.Add(row);
 }

 protected TableCell (string text, string url)
 {
     //Create new Cell
     TableCell cell = new TableCell();
    //Create new HyperLink.
     HyperLink link = new HyperLink();
     try
     {
         link.Text = text;
         link.Font.UnderLine = true;
         link.Target = "_blank";
         link.NavigationUrl = url;
         link.Attributes.Add("style", "color:Black;");
         //Add hyperlink to the cell.
         cell.Controls.Add(link);
     }
     catch(Exception ex)
     {
     }
     //Return Cell with HyperLink.
     return cell;
 }

This would resolve the error as well as give you proper way to add cell with hyperlink in the gridviewrow.

Chetan
  • 6,711
  • 3
  • 22
  • 32
  • I tried and it didn't add the row. updated code in my post. – Learner May 10 '18 at 11:02
  • Are you getting any errors now? I updated my answer, please check now – Chetan May 10 '18 at 11:44
  • I 'm getting error in this line `gvData.Rows.Add(row);`. It says GridViewRowCollection does not contain a definition for add. – Learner May 10 '18 at 11:59
  • You can not add new row to GridView this way. You need to ask different question for that with proper details of what exactly you want to do. https://stackoverflow.com/questions/19485909/add-new-row-in-gridview-after-binding-c-asp-net – Chetan May 10 '18 at 12:24
  • If this answer has helped you to resolve the error you were getting then you can mark this as helpful answer and ask another question for the problem of adding new row to grid view. – Chetan May 10 '18 at 12:25
0

try the below:

protected HyperLink AddHyperLink(string cell, string strURL)
{
    HyperLink h1 = new HyperLink();
    TableCell cells = new TableCell();
    try
    {
        h1.Text = cell;
        h1.Font.Underline = true;
        h1.Target = "_blank";
        h1.NavigateUrl = strURL;
        h1.Attributes.Add("style", "color:black");
        cells.Controls.Add(h1);

    }
    catch(Exception ex)
    {

    }
    return h1;
}
Gagan Deep
  • 1,508
  • 9
  • 13