Create and select CridView, edit, cancel, delete, and create cridview
Front-end:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/-- > Main front-end code :...... <asp: gridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" CellPadding = "4" ForeColor = "#333333" GridLines = "None" OnRowDeleting = "GridView1_RowDeleting" OnRowEditing =" gridView1_RowEditing "OnRowUpdating =" GridView1_RowUpdating "OnRowCancelingEdit =" inline "> <FooterStyle BackColor =" #990000 "Font-Bold =" True "ForeColor =" White "/> <Columns> <asp: boundField DataField = "ID card number" HeaderText = "User ID" ReadOnly = "True"/> <asp: BoundField DataField = "name" HeaderText = "User Name"/> <asp: boundField DataField = "employee gender" HeaderText = "gender"/> <asp: BoundField DataField = "Home Address" HeaderText = "Home Address"/> <asp: commandField HeaderText = "select" ShowSelectButton = "True"/> <asp: CommandField HeaderText = "edit" ShowEditButton = "True"/> <asp: commandField HeaderText = "delete" ShowDeleteButton = "True"/> </Columns> <RowStyle ForeColor = "#000066"/> <SelectedRowStyle BackColor = "#669999" Font-Bold =" true "ForeColor =" White "/> <PagerStyle BackColor =" White "ForeColor =" #000066 "HorizontalAlign =" Left "/> <HeaderStyle BackColor =" #006699 "Font-Bold = "True" ForeColor = "White"/> </asp: gridView>
Background:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/-- > Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. data. sqlClient; public partial class _ Default: System. web. UI. page {// clear moon http://blog.csdn.net/21aspnet SqlConnection sqlcon; SqlCommand sqlcom; string strCon = "Data Source = (local); Database = Database name; Uid = Account; Pwd = password"; protected void Page_Load (object sender, eventArgs e) {if (! IsPostBack) {bind () ;}} protected void gridviewinclurowediting (object sender, GridViewEditEventArgs e) {GridView1.EditIndex = e. newEditIndex; bind () ;}// delete protected void GridView1_RowDeleting (object sender, GridViewDeleteEventArgs e) {string sqlstr = "delete from table where id = '" + GridView1.DataKeys [e. rowIndex]. value. toString () + "'"; sqlcon = new SqlConnection (strCon); sqlcom = new SqlCommand (sqlstr, sqlcon); sqlcon. open (); sqlcom. executeNonQuery (); sqlcon. close (); bind () ;}// update protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e) {sqlcon = new SqlConnection (strCon ); string sqlstr = "update table set field 1 = '" + (TextBox) (GridView1.Rows [e. rowIndex]. cells [1]. controls [0]). text. toString (). trim () + "', Field 2 ='" + (TextBox) (GridView1.Rows [e. rowIndex]. cells [2]. controls [0]). text. toString (). trim () + "', Field 3 ='" + (TextBox) (GridView1.Rows [e. rowIndex]. cells [3]. controls [0]). text. toString (). trim () + "'where id = '" + GridView1.DataKeys [e. rowIndex]. value. toString () + "'"; sqlcom = new SqlCommand (sqlstr, sqlcon); sqlcon. open (); sqlcom. executeNonQuery (); sqlcon. close (); GridView1.EditIndex =-1; bind () ;}// cancel protected void GridView1_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e) {GridView1.EditIndex =-1; bind ();} // bind public void bind () {string sqlstr = "select * from table"; sqlcon = new SqlConnection (strCon); SqlDataAdapter myda = new SqlDataAdapter (sqlstr, sqlcon ); dataSet myds = new DataSet (); sqlcon. open (); myda. fill (myds, "table"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string [] {"id"}; // primary key GridView1.DataBind (); sqlcon. close ();}}