2

I have an ASP.NET web app that processes some data from other sites and displays the information in a gridview. I don't know how many rows that gridview is going to have, nor how many columns.

In this gridview I have a templatefield which I used to add a Checkbox. The rest of the columns are added using a DataTable that I bind to this gridview. The problem now is that after 10 columns, I have an URL which I want to display as a button, or a link. After this link I have x nr of columns.

How do I add this URL which is resting between static columns and dynamic columns, in a GridView with dynamic rows ?

I tried writing a href=link.. in the DataTable but it displays it as text. I found an article that suggested something with HtmlDecode, but for that to work I would need to use boundfields which set the htmlencode = false..or something like that.

Is there any way of doing this ? or should I just move the link in the itemtemplate that has the checkbox as well and try to set it there ?

enter image description here

klashar
  • 2,519
  • 2
  • 28
  • 38
Cosmos24Magic
  • 219
  • 4
  • 17
  • Could you post a few pictures of your current view, and maybe what you'd like it to look like? That would help clarify a lot. – Cullub Feb 24 '17 at 13:05
  • it would also be helpful to see how you bind the data –  Feb 24 '17 at 13:09
  • Added an image. The top table is what I have now. The bottom one is what I would like it to be. Hope it helps – Cosmos24Magic Feb 24 '17 at 13:29

2 Answers2

2

You can use the MatchGrid_RowDataBound method to look over the columns and add the buttons as you need.
Here is an example of how to add a button on RowDataBound:

How do I programmatically add a button to a gridview and assign it to a specific code-behind function?

Community
  • 1
  • 1
Balbinator
  • 220
  • 2
  • 9
  • I tried doing it like this in RowDataBound: LinkButton artLink = new LinkButton(); artLink.OnClientClick = "window.open('" + currentUrl + "');"; artLink.Text = "View article on " + e.Row.Cells[3].Text; e.Row.Cells[12].Controls.Add(artLink); //The link showed up but it only works once. After I click it, it dissappears – Cosmos24Magic Feb 24 '17 at 14:18
1

You can use the OnRowDataBound for that. It will insert extra cells into the GridView. You could also insert extra column into the DataTable before binding it to the GridView.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //header
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //add 6 cells
        for (int i = 1; i <= 6; i++)
        {
            TableHeaderCell headerCell = new TableHeaderCell();

            if (i == 6)
            {
                headerCell.Text = "URL";
            }
            else
            {
                headerCell.Text = "Static " + i;
            }

            //add the new cell to the gridview
            e.Row.Cells.AddAt(i, headerCell);
        }
    }

    //normal row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the current row to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //add 6 cells
        for (int i = 1; i <= 6; i++)
        {
            TableCell cell = new TableCell();

            if (i == 6)
            {
                cell.Text = string.Format("<a target=\"_blank\" href=\"{0}\">{0}</a>", row["myURL"]);
            }
            else
            {
                cell.Text = "Enter stuff here...";
            }

            //add the new cell to the gridview
            e.Row.Cells.AddAt(i, cell);
        }
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • I have done like you suggested and the button shows up in the GridView, but when I click on it, it dissappears. Do you know what I can do so that it doesn't update the table ? I'm guessing I should do something like if(!Page.IsPostBack){..}, but I don't exactly know where I should put this – Cosmos24Magic Feb 27 '17 at 07:07
  • Manage to make the button not postback by adding "return=false" in the OnClientClick(). It works. Thank you ! – Cosmos24Magic Feb 27 '17 at 07:26
  • I've just found out that elements added dynamically on RowDataBound, only persist until the next postback. I have some other buttons on my page, which shouldn't postback anything. I have some export to excel buttons for example and when I click on them the dynamically added button dissappears. What could I do to stop this behaviour ? Do I need to move all my buttons in javascript ? If I could, I would move the "add button" part in Page Load so this wouldn't happen, but I can't. I need it to by dynamic. Any solutions ? Thanks ! – Cosmos24Magic Feb 28 '17 at 07:55
  • 1
    You need to recreate dynamic controls on every postback. So move the binding of the gridview outside `if (!IsPostBack)` check. – VDWWD Feb 28 '17 at 08:21