将GridView数据放入DataTable

来源:互联网 发布:投影边缘融合软件 编辑:程序博客网 时间:2024/05/16 12:41
<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

<script   runat= "server "> 
        void   Page_Load(object   sender,   EventArgs   e) 
        { 
                if   (!IsPostBack)   { 
                        CreateGridView(); 
                } 
        } 

        void   CreateGridView() 
        { 
                GridView   grd   =   new   GridView();                 
                hldContent.Controls.Add(grd); 
                grd.AutoGenerateColumns   =   false; 
                
                BoundField   boundField   =   new   BoundField(); 
                grd.Columns.Add(boundField); 
                boundField.HeaderText   =   "产品名称 "; 
                boundField.DataField   =   "ProductName ";                                 
                
                LoadProductData(grd); 
        } 

        void   LoadProductData(GridView   grd) 
        { 
                DataTable   dt   =   CreateProductTable(); 
                
                grd.DataSource   =   dt; 
                grd.DataBind(); 
        } 

        #region   sample   data 

        static   DataTable   CreateProductTable() 
        { 
                DataTable   tbl   =   new   DataTable( "Products "); 

                tbl.Columns.Add( "ProductID ",   typeof(int)); 
                tbl.Columns.Add( "ProductName ",   typeof(string)); 
                tbl.Columns.Add( "CategoryID ",   typeof(int)); 
                DataRow   row   =   tbl.NewRow(); 
                row[0]   =   1; 
                row[1]   =   "Chai "; 
                row[2]   =   1; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   2; 
                row[1]   =   "Chang "; 
                row[2]   =   1; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   3; 
                row[1]   =   "Aniseed   Syrup "; 
                row[2]   =   2; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   4; 
                row[1]   =   "Chef   Anton 's   Cajun   Seasoning "; 
                row[2]   =   2; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   5; 
                row[1]   =   "Chef   Anton 's   Gumbo   Mix "; 
                row[2]   =   2; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   47; 
                row[1]   =   "Zaanse   koeken "; 
                row[2]   =   3; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   48; 
                row[1]   =   "Chocolade "; 
                row[2]   =   3; 
                tbl.Rows.Add(row); 

                row   =   tbl.NewRow(); 
                row[0]   =   49; 
                row[1]   =   "Maxilaku "; 
                row[2]   =   3; 
                tbl.Rows.Add(row); 

                return   tbl; 
        } 

        #endregion 

</script> 

<html   xmlns= "http://www.w3.org/1999/xhtml "   > 
<head   runat= "server "> 
        <title> Untitled   Page </title> 
</head> 
<body> 
        <form   id= "form1 "   runat= "server "> 
        <div> 
        <asp:PlaceHolder   ID= "hldContent "   runat= "server "> </asp:PlaceHolder> 
        </div> 
        </form> 
</body> 
</html>