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

[Solved] RadGrid GridBoundColumn Hyperlink Issue

1 Answer 228 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahesh Babu
Top achievements
Rank 1
Mahesh Babu asked on 29 Jul 2009, 04:32 AM
Hi,

I have a radGrid to which I need to give links to few columns and I am doing as given below:

protected void radGrid_PreRender(object sender, EventArgs e)
        {
            foreach (GridDataItem dataItem in radGrid.Items)
            {
                foreach (GridColumn col in radGrid.MasterTableView.RenderColumns)
                {
                    if (col.ColumnType == "GridBoundColumn")
                    {
                        if (col.UniqueName == "CardNumber")
                        {
                            string celltxt = dataItem[col.UniqueName].Text;
                            LinkButton link = new LinkButton();
                            link.Text = celltxt;
                            link.PostBackUrl = string.Format("CardDetails?CardNo={0}", link.Text);
                            dataItem[col.UniqueName].Controls.Clear();
                            dataItem[col.UniqueName].Controls.Add(link);
                        }
                    }
                }
            }
        }


My issue is the postbackurl for the linkbutton is not dependent on a single/current cell value. Its depends on other cells as well. This can be something as given below:

link.PostBackUrl = string.Format("CardDetails?CardNo={0}?AccountID={1}", link.Text,?????);

I dont know how to get AccountID value which is there on the other cell and to my luck it can be invisible in many cases. When I try to access a dataItem["AccountID"] which is invisible gridboundcolumn, it is throwing  "Cannot find a cell bound to column name 'AccountID'"}    System.Exception {Telerik.Web.UI.GridException}  exception.

Any ideas on this???

Is there anyway to give URL's something like this  <a href="cardgroupview.aspx?id=<%# Eval("CardGroupID") %>"><%# Eval("CardGroupName")%></a> at the time of adding GridBoundColumns to radGrid at runtime.


Thanks a lot.

Mahesh

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Jul 2009, 06:55 AM
Hello Mahesh,

You can add the Invisible column's datafiels to the DataKeyNames collection of the grid and then retrieve it in the code behind to pass the value to the url of the Hyperlink. Try out the code below:
aspx:
 <telerik:RadGrid ID="RadGrid" DataSourceID="SqlDataSource1" runat="server" 
 OnPreRender="RadGrid_PreRender"
       <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="AccountID" > 

c#:
protected void radGrid_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in radGrid.Items) 
        {             
            HyperLink hl = new HyperLink(); 
            hl.Text = item["CardNumber"].Text; 
            hl.NavigateUrl = String.Format("CardDetails.aspx?CardNo={0}& AccountID={1}", item["CardNumber"].Text, item.GetDataKeyValue("AccountID").ToString()); 
            item["CardNumber"].Controls.Add(hl); 
        }  
    } 

Thanks
Princy.
Tags
Grid
Asked by
Mahesh Babu
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or