Click here to Skip to main content
14,921,143 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am Dynamically creating bound fields and hyperlink field for the gridview.

so once the data is bound the gridview displays the day which contains bound field as well as hyperlink.
my problem is when i click on hyperlinkfield it has to redirect to the other page carrying the Identity value of the table
i have also tried giving datakey names but unable to access them

here is my code

if (item.Text == "CompanyName")
                 {
                     //string id = gdvCompanies.SelectedDataKey.Value.ToString();
                     HyperLinkField hl = new HyperLinkField();
                     //hl.Click = new EventHandler(hl_Click);
                     hl.DataTextField = item.Value;
                     hl.HeaderText = item.Value;
                     //hl.Click += new EventHandler(clickMe);
                     hl.NavigateUrl = "EditCompany.aspx?&CID={1}";

                     gdvCompanies.Columns.Add(hl);
                 }
                 else
                 {
                     BoundField b = new BoundField();
                     b.DataField = item.Value;
                     b.HeaderText = item.Value;
                     gdvCompanies.Columns.Add(b);
                 }


how can i over come this.
Posted

1 solution

You use the RowDataBound event of your Gridview.

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    ...

    //get the DataItem
    DataRowView rowView = (DataRowView)e.Row.DataItem;
    if(!string.IsNullOrWhitespace(rowView["CID"].ToString())
        hl.NavigateUrl = "EditCompany.aspx?CID=" + rowView["CID"].ToString();

    ...
}
   
v2
Comments
anupama962010 2-Jul-11 3:25am
   
Thank You very much that was really helpful
anupama962010 2-Jul-11 4:34am
   
But i have a problem executing the above procedure.
My first column data in the gridview is not working. i mean the hyperlink field is not working for the first column hyperlinkfield
Jpuckett 5-Jul-11 9:15am
   
Then CID is null and the Hyperlink Navigate URL isn't being populated correctly. Create a default URL if CID is empty to populate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900