日期:2014-05-18 浏览次数:20482 次
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable("Table_Order"); dt.Columns.Add("GUID", System.Type.GetType("System.Guid")); dt.Columns.Add("名字", System.Type.GetType("System.String")); DataRow dr = dt.NewRow(); dr["GUID"] = guid1; dr["名字"] = "小明"; DataRow dr = dt.NewRow(); dr["GUID"] = guid2; dr["名字"] = "小红"; DataView dv = new DataView(dt); dynamicGenerateColumns(GridView1, dt);//动态生成列 GridView1.DataSource = dv; GridView1.DataKeyNames = new string[] { "GUID"};//设置键 gv_OrderInfo.DataBind(); } } // 动态生成GridView的Columns public static GridView dynamicGenerateColumns(GridView gv, DataTable dt) { // 把GridView的自动产生列设置为false,否则会出现重复列 gv.AutoGenerateColumns = false; // 清空所有的Columns gv.Columns.Clear(); // 遍历DataTable 的每个Columns,然后添加到GridView中去 foreach (DataColumn item in dt.Columns) { BoundField col = new BoundField(); col.HeaderText = item.ColumnName; col.DataField = item.ColumnName; col.Visible = true; gv.Columns.Add(col); } return gv; } protected void gv_OrderInfo_SelectedIndexChanged(object sender, EventArgs e)//点击界面中的超链接之后,可以取到相应的键值(GUID) { Label1.Text = GridView1.SelectedDataKey["GUID"].ToString(); }
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> </asp:GridView>
<asp:TemplateField HeaderText="name" > <ItemTemplate> <asp:LinkButton ID="nmae" runat="server" Text='<%# Eval("name")%>' CommandName="Edit" ></asp:LinkButton><br /> </ItemTemplate> </asp:TemplateField>
------解决方案--------------------