C#動態建立Gridview及批量插入到數據庫

這裏介紹兩種動態建立Gridview的方法:html

(一)、有時須要應付上頭領導的檢查,因此就弄一些靜態的Gridview來顯示數據,這種方法的優勢就是不用鏈接數據庫,比較方便,可是代碼靈活性不高,全部數據由程序員在後臺代碼寫死。前端

  一、首先要在前臺添加一個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>

  二、在後臺首先動態建立一個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         }

 

(二)、有時咱們在寫作項目時,可能客戶會要求一種商品,可是有多種屬性,這時若是咱們一個個的添加,就太浪費咱們的生命了,由於咱們能夠採起動態添加的方式,極大的節約了咱們的時間,代碼以下:ide

  一、前端:spa

 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>
View Code

  二、後臺:3d

 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 }
View Code

  效果以下:code


因爲界面太大,因此分爲了兩張圖,此處訂單信息錄入與訂單詳細信息添加兩部分均可以是一對多、多對多的關係,只要最後點擊批量提價就能夠一次性的插入到數據庫當中了orm

相關文章
相關標籤/搜索