Link in a DataRow (Datatable)

喜你入骨 提交于 2019-12-12 18:39:32

问题


I'm building a DataTable dynamically and I'm trying to add a "link" in the DataRow(s) that I'm adding to the DataTable. The DataTable is bound to a GridView after it's creation.

Something like that :

   DataTable dataTable = new DataTable();
   foreach (Item item in items)
    {
        DataRow row = dataTable.NewRow();
        dataTable.Columns.Add(new DataColumn("col"));

        row["col"] = "<a href='http://www.google.com'>Link here</a>";

        dataTable.Rows.Add(row);

    }

Then I bind it to a GridView :

        <asp:GridView ID="grdView" Runat="server" border="0" EnableViewState="true" style="width:100%;"
            AutoGenerateColumns="true" AllowPaging="false" PagerSettings-Visible="false" 
            ShowHeader="true" ShowFooter="true" CellPadding="0" CellSpacing="0"
            Visible="True">
        </asp:GridView>

But the HTML in the Column is encoded when I bind it to the GridView. Is there a way to add a HyperLink object there or something like that?

P.S. It's not in the example but the columns are added dynamically (it means that I don't know before the rendering how many columns I'll have)

UPDATE #1

I have access to the GridView when I create the columns. I was able to do something like that :

    dataTable.Columns.Add(new DataColumn("col"));

    BoundField bf = new BoundField();
    bf.HtmlEncode = false;
    bf.DataField = "col";
    grd.Columns.Add(bf);

   row["col"] = "<a href='http://www.google.com'>Link here</a>";

But it displays 2 coloumns "col"...

UPDATE #3 : I used a DataGrid instead. It doesn't encode HTML when inserted in "plain text" in the data rows.


回答1:


If you are returning html code from your query, just use htmlEncode=False on your boundfield. Also set AutoGenerateColumns="false" on your gridview, that's why you're getting double columns on your gridview.




回答2:


It's been an long time since this was asked, but I arrived here being one of the first google results, so I would like to tell you that I have resolved this adding a "HyperLinkField":

HyperLinkField myLink = new HyperLinkField();
myLink.HeaderText = "Link Here";
myLink.DataTextField = "Click here";
myLink.DataNavigateUrlFields = new string[] { "field1", "field2", "field3" };
myLink.DataNavigateUrlFormatString = "NewPage.aspx?id={0}&otherId={1}&otherId2={2}";

myGridView.Columns.Add(myLink);

//Finally bind the data...
myGridView.DataBind();



回答3:


sorry disregard...I didnt see the dynamic table constraint Why do you add a template column to the GridView and then add a hyperlink. Sorry this code wont seem to format correctly in this space

    <asp:TemplateColumn>                                                                        <ItemTemplate>
<asp:HyperLink runat="server" ID="hypLink" Text="test" Target='<%# DataBinder.Eval(Container.DataItem, "moved_ref_amt").ToString() %>'></asp:HyperLink>                                                                        </ItemTemplate>                                                                            </asp:TemplateColumn>



回答4:


GridView has a GridView_RowDataBound event which allow you to dynamically inject controls in to rows, format the content, etc.

You will have access to the table's row element (DataItem) and will be able to parse that and format the grid row as you need.

For a good intro example see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx?ppud=4

Edit: You are getting duplicate columns because you have AutoGenerateColumns="true" as well as the template for the column.



来源:https://stackoverflow.com/questions/4109021/link-in-a-datarow-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!