This is a migrated thread and some comments may be shown as answers.

itemdatabound - data from previous row

1 Answer 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
B
Top achievements
Rank 1
B asked on 10 Nov 2008, 08:45 AM
I have to calculate the NavigateUrl of a GridHyperLinkColumn in Gridview.

When I do this i get the result of the previous Datarecord.
So the first Row is empty, the second row gets the link of the first row and so on.
Do I use the wrong event? Whats wrong?

Code
        protected void rgFiles_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem ) 
            { 
                Telerik.Web.UI.GridHyperLinkColumn ghlc = (Telerik.Web.UI.GridHyperLinkColumn) e.Item.OwnerTableView.Columns[0]; 
                System.IO.FileInfo fi = (System.IO.FileInfo)e.Item.DataItem; 
                string s = Server.MapPath("~/upload"); 
                ghlc.NavigateUrl = "~/upload"+fi.DirectoryName.Replace(Server.MapPath("~/upload"),"")+"/"+fi.Name; 
            } 
            
        } 

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 11 Nov 2008, 12:47 PM
Hello,

The behavior you are experiencing is due to the fact that inside the ItemDataBound event the current row is already populated, therefore setting a value to the GridHyperLinkColumn at this point will not be applied on the current but on the next item. In order to change the HyperLink's value you should directly access the HyperLink control instead. Similar to the following:

    protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            HyperLink link = ((GridDataItem) e.Item)["GridHyperLinkColumn"].Controls[0] as HyperLink;  
            link.NavigateUrl = "telerik.com";  
        }  
    } 


Kind regards,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
B
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or