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

[Solved] RadGrid links to GridBoundColumns at runtime

3 Answers 199 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 27 Jul 2009, 06:36 AM
Hi,

I have a radgrid which is bound to a datatable. I want to assign navigation urls to values of columns in that radgrid. Is there anyway to perform this logic at runtime.

I am able to do this at design time like this:

 <telerik:GridTemplateColumn HeaderText="Account Name" UniqueName="Account" SortExpression="Account" ItemStyle-Width="100px" >
                                        <ItemTemplate>
                                        <a href="accountview.aspx?id=<%# Eval("AccountID") %>"><%# Eval("AccountName")%></a>
                                        </ItemTemplate>
                                        <ItemStyle Width="100px" Font-Underline="true"></ItemStyle>
                                    </telerik:GridTemplateColumn>



But my issue is, i am generating these columns at runtime. so, I need to assign urls at runtime only. Please let me know any way to do this at runtime.

Thanks,
Mahesh

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Jul 2009, 08:28 AM
Hi Mahesh,

Try the following code snippet in order to add the links from code behind.

ASPX:
 
  . . . 
<MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="AccountID, AccountName"
  . . . 

C#:
 
protected void Page_Load(object sender, EventArgs e) 
    GridTemplateColumn tempCol = new GridTemplateColumn(); 
    RadGrid1.MasterTableView.Columns.Add(tempCol); 
    tempCol.UniqueName = "AccountLink"
    tempCol.HeaderText = "Account"
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        HyperLink hl = new HyperLink(); 
        hl.NavigateUrl = "Default.aspx?id=" + item.GetDataKeyValue("AccountID").ToString(); 
        hl.Text = item.GetDataKeyValue("AccountName").ToString(); 
        item["CustomerIDLink"].Controls.Add(hl); 
    } 

Shinu.
0
Mahesh Babu
Top achievements
Rank 1
answered on 27 Jul 2009, 09:07 AM
Is there any way to add this to a GridBoundColumn at runtime???
0
Shinu
Top achievements
Rank 2
answered on 28 Jul 2009, 10:10 AM
Hi Mahesh,

I tried following code in order to achieve this by using GridBoundColumn instead of GridTemplateColumn. Give a try with following code snippet.

ASPX:
 
  . . . 
<MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="AccountID, AccountName"
  . . . 

C#:
 
protected void Page_Load(object sender, EventArgs e) 
    GridBoundColumn boundcolumn = new GridBoundColumn(); 
    RadGrid1.MasterTableView.Columns.Add(boundcolumn); 
    boundcolumn.UniqueName = "AccountIDHyperLink"
    boundcolumn.HeaderText = "AccountIDHyperLink"
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        HyperLink hl = new HyperLink(); 
        hl.NavigateUrl = "Default.aspx?id=" + item.GetDataKeyValue("AccountID").ToString(); 
        hl.Text = item.GetDataKeyValue("AccountName").ToString(); 
        item["AccountIDHyperLink"].Controls.Add(hl); 
    } 

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