Create and select CridView, edit, cancel, delete, and create cridview

Source: Internet
Author: User

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 ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.