如果你想要一個真正動態的網格,那麼你可以在代碼中構建網格。這有點棘手,但並非不可能。這是一個獨立的例子,你可以看看。
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
class MyItemTemplate : System.Web.UI.ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
Label label = new Label();
label.ID = "MyLabel";
container.Controls.Add(label);
}
}
protected void Page_Load(object sender, EventArgs e)
{
GridView gv;
if (!this.IsPostBack)
{
gv = new GridView();
gv.AutoGenerateColumns = false;
gv.ID = "MyGrid";
var column = new TemplateField();
column.HeaderText = "My Item";
column.ItemTemplate = new MyItemTemplate();
gv.Columns.Add(column);
MyDiv.Controls.Add(gv);
gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
}
var dataSource = new string[] { "a", "b", "c" };
gv = this.Page.FindControl("MyGrid") as GridView;
gv.DataSource = dataSource;
gv.DataBind();
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
var label = e.Row.FindControl("MyLabel") as Label;
if (label != null)
label.Text = ((string)e.Row.DataItem).ToUpper();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="MyDiv" runat="server">
</div>
</form>
</body>
</html>
感謝您的回覆。兩個問題1)這是應該動態創建的列。實際的GridView應該以標準方式修復和創建。 2)我需要在單元格內顯示圖像 – user935811
1)本示例動態創建列2)您創建的列可以有一個佔位符,您可以在其中添加圖像。 –
謝謝埃裏克,我是asp.net的新手....你有#2的例子嗎? – user935811