含有dropdownlist的gridview增删改查

 

含有dropdownlist的gridview增删改查

分类: asp.net

GridView控件修改、删除例子,修改时含有DropDownList控件。
示例运行效果图:

GridViewUp.aspx文件代码:

 

[html] view plaincopy
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewUp.aspx.cs" Inherits="gridview_GridViewUp" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>无标题页</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.         <div>  
  11.             <table cellpadding="0" cellspacing="0" border="0" width="80%" style="font-size: 11px">  
  12.                 <tr>  
  13.                     <td align="center">  
  14.                         <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"  
  15.                             AutoGenerateColumns="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit"  
  16.                             OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"  
  17.                             DataKeyNames="id,name" OnPageIndexChanging="GridView1_PageIndexChanging" DataMember="card,price" OnRowDataBound="GridView1_RowDataBound" GridLines="None">  
  18.                             <Columns>  
  19.                                 <asp:BoundField HeaderText="身份证号" DataField="card" Visible=false />  
  20.                                 <asp:BoundField HeaderText="编号" DataField="id" ReadOnly="True" />  
  21.                                 <asp:BoundField DataField="name" HeaderText="姓名" ReadOnly="True" />  
  22.                                 <asp:TemplateField HeaderText="身份证号">  
  23.                                     <ItemTemplate>  
  24.                                         <%# Eval("card") %>  
  25.                                     </ItemTemplate>  
  26.                                     <EditItemTemplate>  
  27.                                         <asp:TextBox ID="TBCard" Text='<%# Eval("card") %>' runat="server" Width="140px" />  
  28.                                     </EditItemTemplate>  
  29.                                     <ItemStyle Width="150px" />  
  30.                                 </asp:TemplateField>  
  31.                                 <asp:TemplateField HeaderText="学历">  
  32.                                     <ItemTemplate>  
  33.                                         <%# Eval("description")%>  
  34.                                     </ItemTemplate>  
  35.                                     <EditItemTemplate>  
  36.                                         <asp:HiddenField ID="HDFXueli" runat="server" Value='<%# Eval("xueli") %>' />  
  37.                                         <asp:DropDownList ID="DDLXueli" runat="server" Width="90px" />  
  38.                                     </EditItemTemplate>  
  39.                                     <ItemStyle Width="100px" />  
  40.                                 </asp:TemplateField>  
  41.                                 <asp:TemplateField HeaderText="价格">  
  42.                                     <ItemTemplate>  
  43.                                         <%# Eval("price") %>  
  44.                                     </ItemTemplate>  
  45.                                     <EditItemTemplate>  
  46.                                         <asp:TextBox ID="TBPrice" Text='<%# Eval("price") %>' runat="server" Width="90px" />  
  47.                                     </EditItemTemplate>  
  48.                                     <ItemStyle Width="100px" />  
  49.                                 </asp:TemplateField>  
  50.                                 <asp:BoundField HeaderText="建立时间" DataField="createdate" ReadOnly="True" />  
  51.                                 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />  
  52.                             </Columns>  
  53.                             <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />  
  54.                             <RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />  
  55.                             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  56.                             <EditRowStyle BackColor="#999999" />  
  57.                             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  58.                             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  59.                             <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  60.                             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  61.                         </asp:GridView>  
  62.                     </td>  
  63.                 </tr>  
  64.             </table>  
  65.         </div>  
  66.     </form>  
  67. </body>  
  68. </html>  


 

GridViewUp.aspx.cs文件代码:

 

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Data.SqlClient;  
  12.   
  13. public partial class gridview_GridViewUp : System.Web.UI.Page  
  14. {  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (!IsPostBack)  
  18.         {  
  19.             GridViewBind();  
  20.         }  
  21.     }  
  22.   
  23.     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  24.     {  
  25.         GridView1.PageIndex = e.NewPageIndex;  
  26.         GridViewBind();  
  27.     }  
  28.   
  29.     private void GridViewBind()  
  30.     {  
  31.         string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;  
  32.         string SqlStr = "SELECT a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";  
  33.         DataSet ds = new DataSet();  
  34.   
  35.         try  
  36.         {  
  37.             SqlConnection conn = new SqlConnection(connStr);  
  38.             if (conn.State.ToString() == "Closed") conn.Open();  
  39.             SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);  
  40.             da.Fill(ds, "test01");  
  41.             if (conn.State.ToString() == "Open") conn.Close();  
  42.   
  43.             GridView1.DataSource = ds.Tables[0].DefaultView;  
  44.             GridView1.DataBind();  
  45.         }  
  46.         catch (Exception ex)  
  47.         {  
  48.             Response.Write("数据库错误,错误原因:" + ex.Message);  
  49.             Response.End();  
  50.         }  
  51.     }  
  52.   
  53.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  54.     {  
  55.         if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)  
  56.         {          
  57.             DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");  
  58.   
  59.             // 生成 DropDownList 的值,绑定数据  
  60.             string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;  
  61.             string SqlStr = "SELECT * from xueli";  
  62.             DataSet ds = new DataSet();  
  63.   
  64.             SqlConnection conn = new SqlConnection(connStr);  
  65.             if (conn.State.ToString() == "Closed") conn.Open();  
  66.             SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);  
  67.             da.Fill(ds, "xueli");  
  68.             if (conn.State.ToString() == "Open") conn.Close();  
  69.   
  70.             ddlxueli.DataSource = ds.Tables[0].DefaultView;  
  71.             ddlxueli.DataTextField = "description";  
  72.             ddlxueli.DataValueField = "code";  
  73.             ddlxueli.DataBind();  
  74.             //  
  75.   
  76.             // 选中 DropDownList  
  77.             ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;  
  78.             //  
  79.         }  
  80.     }  
  81.   
  82.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  83.     {  
  84.         GridView1.EditIndex = e.NewEditIndex;  
  85.         GridViewBind();  
  86.     }  
  87.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  88.     {  
  89.         GridView1.EditIndex = -1;  
  90.         GridViewBind();  
  91.     }  
  92.   
  93.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  94.     {  
  95.         string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();  
  96.         string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;  
  97.         string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;  
  98.         string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;  
  99.   
  100.         string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;  
  101.         string SqlStr = "update test01 set card='" + card + "',xueli='" + xueli + "',price='" + price + "' where id=" + id;  
  102.   
  103.         try  
  104.         {  
  105.             SqlConnection conn = new SqlConnection(connStr);  
  106.             if (conn.State.ToString() == "Closed") conn.Open();  
  107.             SqlCommand comm = new SqlCommand(SqlStr, conn);  
  108.             comm.ExecuteNonQuery();  
  109.             comm.Dispose();  
  110.             if (conn.State.ToString() == "Open") conn.Close();  
  111.   
  112.             GridView1.EditIndex = -1;  
  113.             GridViewBind();  
  114.         }  
  115.         catch (Exception ex)  
  116.         {  
  117.             Response.Write("数据库错误,错误原因:" + ex.Message);  
  118.             Response.End();  
  119.         }  
  120.     }  
  121.   
  122.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  123.     {  
  124.         string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();  
  125.         string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;  
  126.         string SqlStr = "delete from test01 where id=" + id;  
  127.   
  128.         try  
  129.         {  
  130.             SqlConnection conn = new SqlConnection(connStr);  
  131.             if (conn.State.ToString() == "Closed") conn.Open();  
  132.             SqlCommand comm = new SqlCommand(SqlStr, conn);  
  133.             comm.ExecuteNonQuery();  
  134.             comm.Dispose();  
  135.             if (conn.State.ToString() == "Open") conn.Close();  
  136.   
  137.             GridView1.EditIndex = -1;  
  138.             GridViewBind();  
  139.         }  
  140.         catch (Exception ex)  
  141.         {  
  142.             Response.Write("数据库错误,错误原因:" + ex.Message);  
  143.             Response.End();  
  144.         }  
  145.     }  
  146. }  


sql server生成表代码:
[sql] view plaincopy
 
  1. CREATE TABLE [dbo].[test01] (  
  2.     [id] [decimal](18, 0) IDENTITY (1, 1) NOT NULL ,  
  3.     [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,  
  4.     [card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,  
  5.     [xueli] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,  
  6.     [price] [decimal](18, 0) NULL ,  
  7.     [createdate] [datetime] NULL   
  8. ON [PRIMARY]  
  9. GO  
  10.   
  11. ALTER TABLE [dbo].[test01] ADD   
  12.     CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],  
  13.     CONSTRAINT [PK_test01] PRIMARY KEY CLUSTERED   
  14.     (  
  15.         [id]  
  16.     ) ON [PRIMARY]   
  17. GO  
  18.   
  19. CREATE TABLE [dbo].[xueli](  
  20.     [id] [int] IDENTITY(1,1) NOT NULL,  
  21.     [code] [int] NULL,  
  22.     [description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,  
  23. CONSTRAINT [PK_xueli] PRIMARY KEY CLUSTERED   
  24. (  
  25.     [id] ASC  
  26. )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]  
  27. ON [PRIMARY]  
  28.   
  29. xueli表中的数据:  
  30. id    code    description  
  31. 1    1    博士  
  32. 2    2    硕士  
  33. 3    3    学士  
posted @ 2015-04-04 08:38  大虎1  阅读(173)  评论(0编辑  收藏  举报