我正在尝试动态添加HyperLinkColumns到我的GridView.我有以下代码:
HyperLinkColumn objHC = new HyperLinkColumn(); objHC.DataNavigateUrlField = "title"; objHC.DataTextField = "Link text"; objHC.DataNavigateUrlFormatString = "id, title"; objHC.DataTextFormatString = "{2}"; GridView1.Columns.Add(objHC);
这不起作用,所以..我怎么能将HyperLinkColumn添加到我的GridView?
您可能希望在绑定行时添加它:
protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e) { HyperLink hlControl = new HyperLink(); hlControl.Text = e.Row.Cells[2].Text; //Take back the text (let say you want it in cell of index 2) hlControl.NavigateUrl = "http://www.stackoverflow.com"; e.Row.Cells[2].Controls.Add(hlControl);//index 2 for the example }