user1636130
user1636130

Reputation: 1683

C# GridViews - Make the cells in one column link to another page when clicked

I have a grid view which is populated from a database.

I need to make it so that each of the cells in a particular column, when clicked will link through to a second page and pass the cell's text value as a query string (ie link to http://mysite.com/page.aspx?CellText='blah blah blah'). Sounds simple I know but I just can't figure it out!

This is what my page looks like at the moment:

<asp:GridView id="gdvTargets" 
          runat="server" 
          AutoGenerateEditButton="true" 
          OnRowEditing="gdvTargets_RowEdit"
          OnRowUpdating="gdvTargets_RowUpdating" 
          OnRowUpdated="gdvTargets_RowUpdated" 
          OnRowCancelingEdit="gdvTargets_RowCancelingEdit"

          cssClass="grid"
          >
<HeaderStyle CssClass="GridHeader"></HeaderStyle>

And my c# code behind it:

 protected void Page_Load(object sender, EventArgs e)
    {
year = Request.QueryString["YearSelector"];
targetType = Request.QueryString["TargetType"];

      gdvTargets.DataSource = this.Ets.Api.Util.Db.GetDataTable("EXEC sp_getTargets " + year + ", " + targetType).Return;
      gdvTargets.DataBind();

    }

This results in a table which looks something like this:

ID        System        Target 1       Target 2       Target3       Target 4
0         ALL           10             20             15             10
2         Machine1      4              7              7              7
5         Xfgb 45       6              13             8              3

I would like the system column to be clickable and link through to a new page but take the name of the system with it. Hope this makes sense! Thanks for your help :D

Upvotes: 2

Views: 7120

Answers (3)

chridam
chridam

Reputation: 103365

Add HyperLinkField to your Gridview and set the following properties:

<asp:HyperLinkField DataNavigateUrlFields="System" 
                    DataNavigateUrlFormatString="page.aspx?CellText={0}" 
                    DataTextField="System" />

DataTextField : Text to Display in the Column
DataNavigateUrlFormatString : Url to Navigate along query string with 0 index
DataNavigateUrlFields : Fields to pass in the query string. if you want to pass more than 1 field, separate it by commas.

Upvotes: 0

Alex
Alex

Reputation: 6149

What you can do is the following:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
            e.Row.Cells[0].Text = "<a href=''>" + e.Row.Cells[0].Text + "</a>";
}

Replace 0 by your column index

Upvotes: 4

Dave Zych
Dave Zych

Reputation: 21887

Use the OnRowCommand event. In your markup, set up a TemplateField with a LinkButton, and set the CommandName and CommandArguments of the link button to the necessary information. I.e., you can name the CommandName "link" (or whatever you wish) and the CommandArgument the id of a table row, or the url to the page, etc.

When the row command event is fired, check what command it is and do what you wish inside of that.

public void gridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "Link")
    {
        int key = int.Parse(e.CommandArgument.ToString());
        Response.Redirect(string.Format(
            "http://mySite/index.aspx?id={0}", key));
    }
}

Upvotes: 1

Related Questions