开发者

How to format hyperlink in a datatable cell?

开发者 https://www.devze.com 2023-02-16 11:30 出处:网络
Why doesn\'t string.Format work with hyperlink in datatable rows? Example: mydt.Rows[j][i] = string.Format(\"<a href开发者_运维技巧=\'#\'>{0}</a>\",tmp);

Why doesn't string.Format work with hyperlink in datatable rows?

Example: mydt.Rows[j][i] = string.Format("<a href开发者_运维技巧='#'>{0}</a>",tmp);

This gives a string in the cell.

Best regards, GK


what do you expect?

your datatable is not able to store "hyperlinks" just valuetypes (string, int, datetime ...)

string.Format has nothing todo with hyperlinks. its a String-Formatting-Feature.

how do you visualize your datatable? wpf, asp.net, asp mvc?, win forms?

have you tried this?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Width="212px">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("first")%>' NavigateUrl='<%# Eval("second") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


protected void Page_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("first");
            table.Columns.Add("second");
            DataRow dr = table.NewRow();
            dr["first"] = "abc";
            dr["second"] = "http://www.abc.com";

            //or dr[0], dr[1]

            table.Rows.Add(dr);

            this.GridView1.DataSource = table;
            GridView1.DataBind();
        }
0

精彩评论

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