GridView动态生成列之一

本文介绍了一种使用ASP.NET中GridView动态生成列的方法来实现数据预览的功能。通过上传Excel文件作为数据源,并利用RadioButtonList实现类别切换效果,展示不同类别的数据。文章提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 今天做了一个功能,根据页面不同的按钮预览关联的数据列表,之前用MultiView 实现了此功能,后来查找资料通过动态生成GridView列生成预览效果,

 现分享如下:

 数据源:Excel /Access/sql....

 环境:VS 2008

一、普通列显示:

     公共方法:

ExpandedBlockStart.gif代码
    /// <summary>
    
/// 绑定生成GridView
    
/// </summary>
    
/// <param name="gdv">要绑定的GridView</param>
    
/// <param name="dtblDataSource">GridView的数据源</param>
    
/// <param name="strDataKey">GridView的DataKeyNames</param>
    public static void GridViewBind(GridView gv, DataTable source, string dataKeys)
    {
        gv.Columns.Clear();

        gv.AutoGenerateColumns 
= false;
        gv.DataSource 
= source;
        gv.DataKeyNames 
= new string[] { dataKeys };

        
for (int i = 0; i < source.Columns.Count; i++)   //绑定普通数据列
        {
            BoundField bfColumn 
= new BoundField();
            bfColumn.DataField 
= source.Columns[i].ColumnName;
            bfColumn.HeaderText 
= source.Columns[i].Caption;
            
            gv.Columns.Add(bfColumn);
        }

        
//gv.Columns[1].Visible = false;

        CommandField field 
= new CommandField();  //绑定命令列
         field.ButtonType = ButtonType.Button;
        field.SelectText 
= "修改";
        field.ShowSelectButton 
= true;
        gv.Columns.Add(field);

        gv.DataBind();
    }

 二 aspx页面:GridViewDynamicBind.aspx,

   

ExpandedBlockStart.gif代码
    <form id="form1" runat="server">
    
<div>
            
<table width="100%" border="0" cellpadding="0" cellspacing="0">
        
<tr>
             
<td align="right">
                 
<table>
                     
<tr align="right">
                     
</tr>
                 
</table>
             
</td>
            
</tr>
            
<tr>
                
<td height="12"><img src="http://images.cnblogs.com/spacer.gif" alt="" width="1" height="1" border="0"/> </td>
            
</tr>           
            
<tr >
             
<td> 
                 
<table>
                    
<tr>
                       
<td><img src="http://images.cnblogs.com/common/general/arrow1.gif" alt="" width="9" height="10" border="0"/></td>
                       
<td>费用类型: </td>
                       
                        
<td>
                            
<asp:RadioButtonList ID="rblType" runat="server" RepeatDirection="Horizontal">
                            
</asp:RadioButtonList>
                        
</td>
                    
</tr>
                    
<tr>
                       
<td><img src="http://images.cnblogs.com/common/general/arrow1.gif" alt="" width="9" height="10" border="0"/></td>
                       
<td>文件: </td>
                       
                        
<td>
                            
<asp:FileUpload ID="fupload" runat="server" />
                            
<asp:Button ID="btnView" runat="server" Text="上传预览" onclick="btnView_Click" /><span style="FONT-SIZE: 13px; COLOR: #0000ff; FONT-FAMILY: ''cb'ce'cc'e5'">&nbsp; 说明:选择指定模板的数据文件</span>
                        
</td>
                    
</tr>
                 
</table>
             
</td>
           
</tr>
            
<tr>
               
<td height="12">
                   
<asp:GridView ID="gvlist" runat="server" 
                       onselectedindexchanged
="gvlist_SelectedIndexChanged">
                   
</asp:GridView>
                
</td>
            
</tr>
            
</table>
    
</div>
    
</form>

 

 GridViewDynamicBind.cs:

 

ExpandedBlockStart.gif代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Template_GridViewDynamicBind : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {

    }
    
protected void btnView_Click(object sender, EventArgs e)
    {
        //Helper.GridViewBind(
this.gvlist, this.getDataTable(), "供应商编号");  // 可为Helper公共类
      GridViewBind(this.gvlist, this.getDataTable(), "供应商编号");
    }

    
public DataTable getDataTable()
    {
        
try
        {
            
string filePath = this.fupload.PostedFile.FileName;
            DataTable dt 
= Helper.BatchImportExcelToDataTable("", filePath);
            
return dt;
        }
        
catch (Exception e)
        {
            
throw e;
        }

    }
    
protected void gvlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write(
this.gvlist.DataKeys[this.gvlist.SelectedIndex].Value);
    }
}

 

三、效果图:

功能说明:

       有一个RadioButtonList,是类别的自由切换(既所谓的切换效果,根据选择的不同,显示不同类别的数据.).

       我此处是为了截取部分功能通过上传Excel文件为数据源: DataTable dt = Helper.BatchImportExcelToDataTable("", filePath); 数据源自己可自由设置.

       并且输出: gvlist_SelectedIndexChanged,输出所选择行的Response.Write(this.gvlist.DataKeys[this.gvlist.SelectedIndex].Value);

 

四、基本功能已经完成,其他事件同理可得.

转载于:https://www.cnblogs.com/guxingfeng302/archive/2010/06/28/1766919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值