1 <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" BorderColor="Black" 2 onrowcancelingedit="GridView1_RowCancelingEdit" 3 onrowediting="GridView1_RowEditing" 4 onrowdatabound="GridView1_RowDataBound" onrowupdating="GridView1_RowUpdating"> 5 <Columns> 6 <asp:TemplateField HeaderText ="ID"> 7 <ItemTemplate> 8 <%#Eval("CustomerID") %> 9 </ItemTemplate> 10 <EditItemTemplate> 11 <%#Eval("CustomerID") %> 12 </EditItemTemplate> 13 </asp:TemplateField> 14 <asp:TemplateField HeaderText ="Name"> 15 <ItemTemplate> 16 <%#Eval("CompanyName") %> 17 </ItemTemplate> 18 <EditItemTemplate> 19 <asp:TextBox ID="TextBox1" runat="server" Text ='<%#Eval("CompanyName") %>'></asp:TextBox> 20 </EditItemTemplate> 21 </asp:TemplateField> 22 <asp:TemplateField HeaderText ="Contact"> 23 <ItemTemplate> 24 <%#Eval("ContactName") %> 25 </ItemTemplate> 26 <EditItemTemplate> 27 <asp:TextBox ID="TextBox2" runat="server" Text ='<%#Eval("ContactName") %>'></asp:TextBox> 28 </EditItemTemplate> 29 </asp:TemplateField> 30 <asp:TemplateField HeaderText ="Address"> 31 <ItemTemplate> 32 <%#Eval("Address") %> 33 </ItemTemplate> 34 <EditItemTemplate> 35 <asp:TextBox ID="TextBox3" runat="server" Text ='<%#Eval("Address") %>'></asp:TextBox> 36 </EditItemTemplate> 37 </asp:TemplateField> 38 <asp:TemplateField HeaderText ="City"> 39 <ItemTemplate> 40 <%#Eval("City") %> 41 </ItemTemplate> 42 <EditItemTemplate> 43 <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"> 44 </asp:DropDownList> 45 </EditItemTemplate> 46 </asp:TemplateField> 47 <asp:TemplateField HeaderText ="编辑"> 48 <ItemTemplate> 49 <asp:Button ID="Button1" runat="server" Text="编辑" CommandName="Edit" /> 50 </ItemTemplate> 51 <EditItemTemplate> 52 <asp:Button ID="Button2" runat="server" Text="修改" CommandName="Update" /> 53 <asp:Button ID="Button3" runat="server" Text="取消" CommandName="Cancel" /> 54 </EditItemTemplate> 55 </asp:TemplateField> 56 </Columns> 57 </asp:GridView>
2 protected void Page_Load(object sender, EventArgs e) 3 { 4 if (!IsPostBack) 5 { 6 databind(); 7 } 8 } 9 public void databind() 10 { 11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString()); 12 SqlCommand cmd = new SqlCommand("SELECT * FROM CUSTOMERS", con); 13 SqlDataAdapter da = new SqlDataAdapter(cmd); 14 DataSet ds = new DataSet(); 15 da.Fill(ds); 16 this.GridView1.DataSource = ds.Tables[0]; 17 this.GridView1.DataKeyNames = new string[] { "CustomerID", "City" }; 18 this.GridView1.DataBind(); 19 } 20 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 21 { 22 this.GridView1.EditIndex = e.NewEditIndex; 23 databind(); 24 } 25 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 26 { 27 this.GridView1.EditIndex = -1; 28 databind(); 29 } 30 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 31 { 32 foreach (TableCell item in e.Row.Cells) 33 { 34 item.Attributes.Add("style", "border-color:black"); 35 36 } 37 if (e.Row.RowType == DataControlRowType.DataRow) 38 { 39 DropDownList dr = (DropDownList)e.Row.FindControl("DropDownList1"); 40 if (dr != null) 41 { 42 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString()); 43 SqlCommand cmd = new SqlCommand("Select distinct City from Customers", con); 44 SqlDataAdapter da = new SqlDataAdapter(cmd); 45 DataSet ds = new DataSet(); 46 da.Fill(ds); 47 dr.DataSource = ds.Tables[0]; 48 dr.DataTextField = "City"; 49 dr.DataValueField = "City"; 50 dr.DataBind(); 51 dr.SelectedItem.Text = this.GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString(); 52 } 53 } 54 } 55 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 56 { 57 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString()); 58 SqlCommand cmd = new SqlCommand("Update Customers set CompanyName=@CompanyName,ContactName=@ContactName,City=@City where CustomerID=@CustomerID", con); 59 SqlParameter[] ps = new SqlParameter[4]; 60 ps[0] = new SqlParameter("@CompanyName",SqlDbType.NVarChar,40); 61 ps[1] = new SqlParameter("@ContactName",SqlDbType.NVarChar,30); 62 ps[2] = new SqlParameter("@City",SqlDbType.NVarChar,15); 63 ps[3] = new SqlParameter("@CustomerID",SqlDbType.NChar,5); 64 ps[0].Value = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim(); 65 ps[1].Value = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text.Trim(); 66 ps[2].Value = ((DropDownList)this.GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedItem.Text; 67 ps[3].Value = this.GridView1.DataKeys[e.RowIndex].Values[0]; 68 cmd.Parameters.AddRange(ps); 69 if (con.State == ConnectionState.Closed) 70 { 71 con.Open(); 72 } 73 cmd.ExecuteNonQuery(); 74 this.GridView1.EditIndex = -1; 75 databind(); 76 }