Gridview用法大全。

Gridview控件72技。

http://www.cnblogs.com/shiningrise/archive/2007/06/20/791130.html

其中的Gridview 分页功能,word导出,excel导出(图片不能导出),多项选择删除,gridview的编辑,修改,删除功能在最近的项目中使用,感觉挺好。另外那个excel导入功能上面的是不用fileupload控件的,偶稍微修改了一下。

前台页面代码如下:

Import.aspx
 1 <asp:GridView ID="GridView1" BorderColor="Black"  runat="server" AutoGenerateColumns="False"  Font-Size="12px" Width="530px" AllowSorting="True">
 2          
 3           <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
 4             <RowStyle HorizontalAlign="Center" />
 5             <PagerStyle HorizontalAlign="Center" />
 6         </asp:GridView>
 7 
 8   <asp:FileUpload ID="FileUpload1" runat="server" />
 9         <br />
10         <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
11         <asp:Button ID="Button2" runat="server" Text="批量导入" onclick="Button2_Click" />
12 
13 

后台页面代码如下:

 

Import.aspx.cs代码
 1 /// <summary>
 2     /// 导出到excel文档
 3     /// </summary>
 4     /// <param name="sender"></param>
 5     /// <param name="e"></param>
 6     protected void Button2_Click(object sender, EventArgs e)
 7     {
 8         if (FileUpload1.PostedFile != null)
 9         {
10             Random rd = new Random(1);
11 
12             //文件重命名
13             string filename = DateTime.Now.Date.ToString("yyyymmdd"+ DateTime.Now.ToLongTimeString().Replace(":"""+ rd.Next(9999).ToString() + ".xls";
14 
15             FileUpload1.PostedFile.SaveAs(@Server.MapPath("..\\UpLoad\\"+ filename);
16             Label1.Text = "文件名为" + filename;
17             string strCon;
18             strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\UpLoad"+ "\\" + filename +";Extended Properties=Excel 8.0;";
19             OleDbConnection con = new OleDbConnection(strCon);
20             OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
21             DataSet ds = new DataSet();
22             da.Fill(ds,"[Sheet1$]");
23             GridView1.DataSource = ds;
24             GridView1.DataBind();
25             SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
26             con1.Open();
27             int count = ds.Tables["[Sheet1$]"].Rows.Count;
28 
29             for (int i = 0; i < count; i++)
30             {
31                 string name;
32                 int sex;
33                 DateTime birth;
34                 string edu;
35                 string pcard;
36                 string ccard;
37                 string other1;
38                 DateTime ddate;
39                 int depid;
40                 string photo;
41                 string other2;
42                 string other3;
43                 string other4;
44 
45                 name = ds.Tables["[Sheet1$]"].Rows[i][1].ToString();
46                 if (Convert.IsDBNull(ds.Tables["[Sheet1$]"].Rows[i][2]))//对象不能从DBNull 转换为其他类型
47 
48                 {
49                     return ;
50 
51                 }
52                 else
53                 {
54 
55                     sex = Convert.ToInt32(ds.Tables["[Sheet1$]"].Rows[i][2]);
56                 }
57                 birth = Convert.ToDateTime(ds.Tables["[Sheet1$]"].Rows[i][3]);
58                 edu = ds.Tables["[Sheet1$]"].Rows[i][4].ToString();
59                 pcard = ds.Tables["[Sheet1$]"].Rows[i][5].ToString();
60                 ccard = ds.Tables["[Sheet1$]"].Rows[i][6].ToString();
61                 other1 = ds.Tables["[Sheet1$]"].Rows[i][7].ToString();
62                 ddate = Convert.ToDateTime(ds.Tables["[Sheet1$]"].Rows[i][8]);
63                 depid = Convert.ToInt32(ds.Tables["[Sheet1$]"].Rows[i][9]);
64                 photo = ds.Tables["[Sheet1$]"].Rows[i][10].ToString();
65                 other2 = ds.Tables["[Sheet1$]"].Rows[i][11].ToString();
66                 other3 = ds.Tables["[Sheet1$]"].Rows[i][12].ToString();
67                 other4 = ds.Tables["[Sheet1$]"].Rows[i][13].ToString();
68                 string sql = "insert into CardInfo(Name,Sex,Birthday,Education,Pcard,Ccard,Other1,Ddate,DepId,Photo) values ('" + name + "','" + sex + "','" + birth + "','" + edu + "','" + pcard+ "','" + ccard + "','" + other1 + "','" + ddate + "','" + depid +"','"+photo+"','"+other2+"','"+other3+"','"+other4+"')";
69                 SqlCommand cmd = new SqlCommand(sql, con1);
70                 cmd.ExecuteNonQuery();
71             }
72             Response.Write("数据库更新成功");
73             con1.Close();
74         }
75     }
76 
原文地址:https://www.cnblogs.com/gxg2008540/p/1909067.html