我有一個dt在c#代碼中的數據表,它有列[0]數據類型是int.So當我達到表中的第7個值時,我需要轉換爲超鏈接並將其添加回數據表。如何將數據表的行值更新爲超鏈接?
int x = int.Parse(dt.Rows[7][0].ToString());
dt.Row[7][0] = "<a href=\"http:www.google.com\">" + x + "</a>";
但它給我的錯誤,不能接受字符串值爲整數。如何過來這個?
我有一個dt在c#代碼中的數據表,它有列[0]數據類型是int.So當我達到表中的第7個值時,我需要轉換爲超鏈接並將其添加回數據表。如何將數據表的行值更新爲超鏈接?
int x = int.Parse(dt.Rows[7][0].ToString());
dt.Row[7][0] = "<a href=\"http:www.google.com\">" + x + "</a>";
但它給我的錯誤,不能接受字符串值爲整數。如何過來這個?
你是如何創建數據表的?該列可能已被鍵入爲int,因此它不能接受字符串值。
一個更好的辦法可能是改變數據綁定控件來顯示鏈接
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);
}
}
}
}
糾正我,我錯了。添加一個字符串類型的額外列。
dt.Columns.Add("LinkColumn");
...
dt.Rows[7]["LinkColumn"]=string.Format("<a href='#'>{0}</a>",x);
謝謝雅各布。奇怪!我無法在IE 9中格式化我的帖子。 – adatapost 2011-01-21 05:08:41
您不能將字符串值分配給int類型列。 – adatapost 2011-01-21 05:03:37