asp.net GridView
前台
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="产品ID" AllowSorting="True" PageSize="20" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowEditing="GridView1_RowEditin" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:CommandField ShowSelectButton="true" HeaderText="选择" /> <asp:CommandField ShowEditButton="true" HeaderText="编辑" /> <asp:BoundField DataField="产品名称" HeaderText="name" /> <asp:BoundField DataField="供应商ID" HeaderText="供应商ID" /> <asp:CheckBoxField DataField="中止" HeaderText="中止" /> </Columns> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <SortedAscendingCellStyle BackColor="#FDF5AC" /> <SortedAscendingHeaderStyle BackColor="#4D0000" /> <SortedDescendingCellStyle BackColor="#FCF6C0" /> <SortedDescendingHeaderStyle BackColor="#820000" /> </asp:GridView>
后台
public partial class WebForm3 : System.Web.UI.Page { public static SqlConnection conn=SqlHelper.GetConnection(); public static DataSet ds = new DataSet(); public static string strUpdate="UPDATE [产品] SET [供应商ID]=@gysID, [中止]=@zz WHERE [产品ID] =@ID"; public SqlCommand cmd=new SqlCommand(strUpdate,conn); protected void bind() { ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, "select * from [产品]"); GridView1.DataSource = ds; GridView1.DataBind(); Response.Write(ds.Tables[0].Rows.Count.ToString()); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { conn = SqlHelper.GetConnection(); bind(); } } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } protected void GridView1_RowEditin(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { if (conn.State!=ConnectionState.Open) conn.Open(); cmd = new SqlCommand(strUpdate, conn); // cmd.Parameters.AddWithValue("@name",((TextBox) (GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString()); SqlParameter gysID = new SqlParameter("@gysID", SqlDbType.Int); SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int); SqlParameter zz = new SqlParameter("@zz", SqlDbType.Bit); gysID.Value = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString()); zz.Value = Convert.ToBoolean(((CheckBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked.ToString()); cmd.Parameters.Add(gysID); cmd.Parameters.Add(zz); ID.Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); cmd.Parameters.Add(ID); cmd.ExecuteNonQuery(); GridView1.EditIndex = -1; bind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bind(); } }