开发者

Link in a DataRow (Datatable)

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" 
            ShowHead开发者_开发问答er="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.


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.


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();


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜