今天做了一个功能,根据页面不同的按钮预览关联的数据列表,之前用MultiView 实现了此功能,后来查找资料通过动态生成GridView列生成预览效果,
现分享如下:
数据源:Excel /Access/sql....
环境:VS 2008
一、普通列显示:
公共方法:
代码 /// <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,
代码 < 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:
代码 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
相关资源:Gridview动态添加列 根据需要添加列和进行增删改操作