Hanuman
Hanuman

Reputation: 1

how to update a data table row value to hyperlink?

I have a data table in dt in c# code and it has column[0] datatype is int.So when ever I reach 7th value in the table I need to convert to hyperlink and add it back to data table.

int x = int.Parse(dt.Rows[7][0].ToString());
dt.Row[7][0] = "<a href=\"http:www.google.com\">" + x + "</a>";

but it is givin me the error that can not accept string value to integer. How to over come this?

Upvotes: 0

Views: 1956

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

Correct me I'm wrong. Add an extra column of string type.

dt.Columns.Add("LinkColumn");
...
dt.Rows[7]["LinkColumn"]=string.Format("<a href='#'>{0}</a>",x);

Upvotes: 2

djeeg
djeeg

Reputation: 6765

How are you creating the data table? The column has probably been typed to int, so it can not accept string values.

A better way might be to change the data bound control to show the link

protected override void OnInit(EventArgs e) {
    base.OnInit(e);
    DataBound += new EventHandler(GridView1_DataBound);
}

void GridView_RowDataBound(object sender, GridViewRowEventArgs e) {
    GridView gridview = (GridView)sender;
    if (e.Row.RowType == DataControlRowType.DataRow) {
        for (int i = 0; i < gridview.Columns.Count; i++) {
            DataControlField obj1 = gridview.Columns[i];
            if (obj1.GetType() == typeof(BoundField)) {
                BoundField field = (BoundField)obj1;
                string datafield = field.DataField;
                object value = DataBinder.Eval(e.Row.DataItem, datafield);
                Literal c = new Literal();
                c.Text = "";
                e.Row.Cells[i].Controls.Add(c);
            }
        }
    }
}

Upvotes: 0

Related Questions