• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小醉仙--专注于ASP.NET开发技术、网站策划、电子商务
昨日不悔,今日勿失,明日莫忧!
博客园    首页    新随笔    联系   管理    订阅  订阅

GridView绑定数组

GridView绑定数组

 

前台: 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSorting="GridView1_Sorting" AllowSorting="true"> 
          
<Columns> 
            
<asp:BoundField DataField="id" HeaderText="id"/> 
          
</Columns> 
         
</asp:GridView> 
后台:   
protected void Page_Load(object sender, EventArgs e) 
    
{ 
        
if (!IsPostBack) 
        
{ 
                        BindGridView(); 
                   }
 
    }
 
 
void BindGridView() 
    
{ 
        
this.GridView1.DataSource = GetTable(); 
        
this.GridView1.DataBind(); 
    }
 
    DataTable GetTable() 
    
{ 
        DataTable dt 
= new DataTable(); 
        dt.Columns.Add(
"id", typeof(string)); 
        
string[] str = new string[20];//把这个数组改成你的就好了 
        for (int i = 0; i  < str.Length; i++) 
        
{ 
            str[i] 
= i.ToString(); 
        }
 
        
for (int j = 0; j  < str.Length; j++) 
        
{ 
            DataRow row 
= dt.NewRow(); 
            row[
"id"] = str[j]; 
            dt.Rows.Add(row); 
        }
 
        
return dt; 
    }
 
posted on 2008-04-01 11:15  小醉仙  阅读(2830)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3