C#动态创建Gridview及批量插入到数据库
这里介绍两种动态创建Gridview的方法:
(一)、有时需要应付上头领导的检查,所以就弄一些静态的Gridview来显示数据,这种方法的优点就是不用连接数据库,比较方便,但是代码灵活性不高,所有数据由程序员在后台代码写死。
1、首先要在前台添加一个gridview即可
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="动态添加.test1" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 </head> 9 <body> 10 <form id="form1" runat="server"> 11 <div> 12 <asp:GridView ID="GridView1" runat="server"> 13 </asp:GridView> 14 </div> 15 </form> 16 </body> 17 </html>
2、在后台首先动态创建一个DataTable表,然后动态创建并添加DataColumn列,随后动态创建行并把数据依次添加进去,此处提供了两种方法:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 DataTable dt = new DataTable(); 4 //添加列 5 dt.Columns.Add(new DataColumn("葡萄来源", typeof(string))); 6 dt.Columns.Add(new DataColumn("品种", typeof(string))); 7 dt.Columns.Add(new DataColumn("市场名", typeof(string))); 8 dt.Columns.Add(new DataColumn("销售价格", typeof(string))); 9 dt.Columns.Add(new DataColumn("销售量", typeof(string))); 10 dt.Columns.Add(new DataColumn("销售人员", typeof(string))); 11 //添加行 12 /*for (int i = 0; i < 9; i++) 13 { 14 DataRow row = dt.NewRow(); 15 row["葡萄来源"] = "湖南"; 16 row["品种"] = "内毕罗"; 17 row["市场名"] = "湖南"; 18 row["销售价格"] = "20"; 19 row["销售量"] = "100"; 20 row["销售人员"] = "张三"; 21 dt.Rows.Add(row); 22 }*/ 23 dt.Rows.Add(new object[] { "湖南", "内毕罗", "湖南", "20", "100", "张三" }); 24 GridView1.DataSource = dt; 25 GridView1.DataBind(); 26 }
(二)、有时我们在写做项目时,可能客户会要求一种商品,但是有多种属性,这时如果我们一个个的添加,就太浪费我们的生命了,因为我们可以采取动态添加的方式,极大的节约了我们的时间,代码如下:
1、前端:
1 <body> 2 <form id="form1" runat="server"> 3 <div style="width: 800px; margin: 0 auto;"> 4 <asp:Panel ID="Panel1" runat="server" GroupingText="订单信息录入" Font-Size="16px"> 5 <br /> 6 <table style="width: 600px; margin: 0 auto;"> 7 <tr> 8 <td>市场网点:</td> 9 <td><asp:TextBox ID="txtMarketPlace" runat="server"></asp:TextBox></td> 10 <td>销售经理:</td> 11 <td><asp:TextBox ID="txtSalesMan" runat="server"></asp:TextBox></td> 12 </tr> 13 <tr> 14 <td>要货时间:</td> 15 <td><asp:TextBox ID="txtOrderTime" runat="server"></asp:TextBox></td> 16 <td></td> 17 <td></td> 18 </tr> 19 </table> 20 </asp:Panel> 21 <hr /> 22 <asp:Panel ID="Panel2" runat="server" GroupingText="订单详细信息添加" Font-Size="16px"> 23 <br /> 24 <table style="width: 550px; margin: 0 auto;"> 25 <tr> 26 <td>品种:</td> 27 <td><asp:DropDownList ID="ddlvariety" runat="server" Width="153px"> 28 <asp:ListItem>红地球</asp:ListItem> 29 </asp:DropDownList></td> 30 <td>包装名称:</td> 31 <td><asp:DropDownList ID="ddlpackage" runat="server" Width="153px"> 32 <asp:ListItem>彩包</asp:ListItem> 33 <asp:ListItem>周转筐</asp:ListItem> 34 <asp:ListItem>一次性筐</asp:ListItem> 35 <asp:ListItem>手提箱</asp:ListItem> 36 <asp:ListItem>天地盖箱</asp:ListItem> 37 </asp:DropDownList></td> 38 </tr> 39 <tr> 40 <td>数量(Kg):</td> 41 <td><asp:TextBox ID="txtAmount" runat="server"></asp:TextBox></td> 42 <td>规格(Kg/件):</td> 43 <td><asp:TextBox ID="txtstandard" runat="server"></asp:TextBox></td> 44 </tr> 45 </table> 46 <br /> 47 <center> 48 <asp:Button ID="btnSubmit" runat="server" Text="添 加" 49 onclick="btnSubmit_Click" /> 50 </center> 51 </asp:Panel> 52 </div> 53 <hr /> 54 <div style="width: 800px; margin: 0 auto;"> 55 <asp:Panel ID="Panel3" runat="server" GroupingText="已添加的订单详细信息" Visible="false"> 56 <br /> 57 <asp:GridView ID="GridView1" runat="server" AllowPaging="false" AutoGenerateColumns="False" 58 CellPadding="4" EmptyDataText="暂无记录" EnableModelValidation="True" ForeColor="#333333" 59 GridLines="None" RowStyle-HorizontalAlign="Center" 60 Width="100%" onrowdeleting="GridView1_RowDeleting"> 61 <Columns> 62 <asp:TemplateField HeaderText="序号"> 63 <ItemTemplate> 64 <%#(Container.DataItemIndex+1).ToString() %> 65 </ItemTemplate> 66 </asp:TemplateField> 67 <asp:BoundField DataField="ProductName" HeaderText="品种" /> 68 <asp:BoundField DataField="PackageName" HeaderText="包装" /> 69 <asp:BoundField DataField="Amount" HeaderText="数量(kg)" /> 70 <asp:BoundField DataField="Standard" HeaderText="规格(kg/件)" /> 71 <asp:BoundField DataField="UnitCount" HeaderText="件数" /> 72 <asp:CommandField ShowDeleteButton="True" HeaderText="删除" /> 73 </Columns> 74 <EditRowStyle BackColor="#999999" VerticalAlign="NotSet" /> 75 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 76 <PagerStyle BackColor="#A18B77" ForeColor="White" HorizontalAlign="Center" /> 77 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" /> 78 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="true" ForeColor="#333333" /> 79 <HeaderStyle BackColor="#5D7B9D" Font-Bold="true" Font-Size="18px" ForeColor="White" 80 Height="25px" /> 81 <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 82 </asp:GridView> 83 <br /> 84 <center> 85 <asp:Button ID="btnAllAdd" runat="server" Text="批量提交" 86 onclick="btnAllAdd_Click" /> 87 </center> 88 </asp:Panel> 89 </div> 90 </form> 91 </body>
2、后台:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data; 8 9 namespace 动态添加 10 { 11 public partial class test : System.Web.UI.Page 12 { 13 //动态创建一个表 14 private static DataTable dt = new DataTable(); 15 //动态创建列,此处创建了五列,其中序号列在前台设置为自动增长的 16 private static DataColumn ProductName = new DataColumn("ProductName", typeof(string)); 17 private static DataColumn Amount = new DataColumn("Amount", typeof(float)); 18 private static DataColumn Standard = new DataColumn("Standard", typeof(float)); 19 private static DataColumn UnitCount = new DataColumn("UnitCount", typeof(float)); 20 private static DataColumn PackageName = new DataColumn("PackageName", typeof(string)); 21 protected void Page_Load(object sender, EventArgs e) 22 { 23 dt.Columns.Clear();//把列清除 24 dt.Rows.Clear();//把行清除 25 //把创建好的列自动添加到已创建好的表中 26 dt.Columns.Add(ProductName); 27 dt.Columns.Add(Amount); 28 dt.Columns.Add(Standard); 29 dt.Columns.Add(UnitCount); 30 dt.Columns.Add(PackageName); 31 } 32 33 protected void btnSubmit_Click(object sender, EventArgs e) 34 { 35 //动态添加行 36 DataRow row = dt.NewRow(); 37 //赋值 38 row["ProductName"] = ddlvariety.SelectedValue; 39 row["PackageName"] = ddlpackage.SelectedValue; 40 row["Amount"] = Convert.ToDouble(txtAmount.Text.Trim()); 41 row["Standard"] = Convert.ToDouble(txtstandard.Text.Trim()); 42 row["UnitCount"] = Convert.ToDouble(row["Amount"]) / Convert.ToDouble(row["Standard"]); 43 dt.Rows.Add(row); 44 GridView1.DataSource = dt.DefaultView;//绑定数据源 45 GridView1.DataBind(); 46 Panel3.Visible = true; 47 } 48 49 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 50 { 51 if (dt.Rows.Count > 0) 52 { 53 DataRow temprow = dt.Rows[e.RowIndex];//获得当前行的索引 54 dt.Rows.Remove(temprow); 55 } 56 if (dt.Rows.Count <= 0) 57 { 58 Panel3.Visible = false; 59 } 60 GridView1.DataSource = dt.DefaultView; 61 GridView1.DataBind(); 62 } 63 64 protected void btnAllAdd_Click(object sender, EventArgs e) 65 { 66 //TODO:批量插入数据库,此处代码省略 67 } 68 } 69 }
效果如下:
由于界面太大,所以分为了两张图,此处订单信息录入与订单详细信息添加两部分都可以是一对多、多对多的关系,只要最后点击批量提价就可以一次性的插入到数据库当中了