Gridview dataBound 从代码后面添加到列数据的超链接

Gridview dataBound add hyperlink to column data from code behind

使用网格视图从后面的代码绑定它:

我想将一个特定的列数据绑定到一个 hyper link 所以当它被点击时它应该进行下载。

怎么做?

下面是我的代码:

for (int i = 0; i <= tbl.Columns.Count - 1; i++) 
{
    Telerik.Web.UI.GridBoundColumn boundfield = new Telerik.Web.UI.GridBoundColumn();

    if (tbl.Columns[i].ColumnName.ToString() == "Row") 
    {

        LinkButton lkbtn = new LinkButton();
        lkbtn.CommandName = i;
        lkbtn.CommandArgument = "dwnld";
        lkbtn.Font.Underline = true;
        lkbtn.Text = tbl.Columns(i).ColumnName.ToString();

        boundfield.DataField = tbl.Columns(i).ColumnName.ToString()
        boundfield.HeaderText = tbl.Columns(i).ColumnName.ToString();

        GridView2.MasterTableView.Columns.Add(boundfield);

    }
}

为什么不使用带 link 按钮的网格模板列。

<telerik:GridTemplateColumn>
       <ItemTemplate>
          <asp:LinkButton ID="btnDownload" OnClick="btnDownload_Click" runat="server">Download Something</asp:LinkButton>
        </ItemTemplate>
   </telerik:GridTemplateColumn>


protected void btnDownload_Click(object sender, EventArgs e)
{
  LinkButton lbBtn = sender as LinkButton;
  GridDataItem item = (GridDataItem)(sender as LinkButton).NamingContainer;
  // Use item to get other details
...
...
}