GridView control modifies, deletes examples, and modify the DropDownList control. Example running renderings:
GridViewup.aspx file code:
<%
@ Page Language
=
"
C #
"
Autoeventwireup
=
"
True
"
Codefile
=
"
GridViewup.aspx.cs
"
Inherits
=
"
GridView_gridviewUp
"
%>
DOCTYPE HTML PUBLIC "- // w3c // DTD XHTML 1.0 Transitional // En" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
>
<
HTML
XMLNS
= "http://www.w3.org/1999/xhtml"
>
<
HEAD
Runat
= "Server"
>
<
Title
>
Untitled Page
Title
>
HEAD
>
<
Body
>
<
FORM
id
= "Form1"
Runat
= "Server"
>
<
Div
>
<
TABLE
Cellpadding
= "0"
Cellspacing
= "0"
Border
= "0"
Width
= "80%"
Style
= "Font-size: 11px"
>
<
TR
>
<
TD
Align
= "center"
>
<
ASP: GridView
Id
= "GridView1"
Runat
= "Server"
Width
= "100%"
Cellpadding
= "4"
Forecolor
= "# 333333"
AutogenerateColumns
= "False"
Allowpaging
= "True"
PageSize
= "12"
OnrowcanceLingedit
= "GridView1_RowcanceLingEdit"
OnrowEditing
= "GridView1_Rowediting"
Onrowupdating
= "GridView1_Rowupdating"
OnrowDeleting
= "GridView1_rowDeleting"
Datakeynames
= "ID, name"
OnpageIndexchanging
= "GridView1_pageIndexchanging"
DataMember
= "Card, Price"
OnrowDATABOUND
= "GridView1_rowDatabase"
Gridlines
= "None"
>
<
Columns
>
<
ASP: Boundfield
Headertext
= "Number"
Datafield
= "id"
Readonly
= "True"
/>
<
ASP: Boundfield
Datafield
= "Name"
Headertext
= "Name"
Readonly
= "True"
/>
<
ASP: Templatefield
Headertext
= "ID number"
>
<
ItemTemplate
>
<%
#
EVal
(
"
Card
"
)
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: TEXTBOX
Id
= "TBCARD"
TEXT
= '<% #
EVAL ("Card")%
>
'Runat = "server" width = "140px" />
EditItemTemplate
>
<
ItemStyle
Width
= "150px"
/>
ASP: Templatefield
>
<
ASP: Templatefield
Headertext
= "Education"
>
<
ItemTemplate
>
<%
#
EVal
(
"
XueliteXT
"
)
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: Hiddenfield
Id
= "Hdfxueli"
Runat
= "Server"
Value
= '<% #
Eval ("Xueli")%
>
'/>
<
ASP: DropDownList
Id
= "DDLXUELI"
Runat
= "Server"
Width
= "90px"
/>
EditItemTemplate
>
<
ItemStyle
Width
= "100px"
/>
ASP: Templatefield
>
<
ASP: Templatefield
Headertext
= "Price"
>
<
ItemTemplate
>
<%
#
EVal
(
"
PRICE
"
)
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: TEXTBOX
Id
= "TBPRICE"
TEXT
= '<% #
EVAL ("price")%
>
'Runat = "server" width = "90px" />
EditItemTemplate
>
<
ItemStyle
Width
= "100px"
/>
ASP: TemplateField>
<
ASP: Boundfield
Headertext
= "Establishing time"
Datafield
= "Createdate"
Readonly
= "True"
/>
<
ASP: CommandField
Showdeletebutton
= "True"
ShoweditButton
= "True"
Headertext
= "Operation"
/>
Columns
>
<
Pagersettings
FirstPageText
= ""
LastPageText
= ""
NextPageText
= ""
PreviouspageText
= ""
/>
<
Rowstyle
HEIGHT
= "20px"
Backcolor
= "# F7f6f3"
Forecolor
= "# 333333"
/>
<
Footerstyle
Backcolor
= "# 5d7b9d"
Font-bold
= "True"
Forecolor
= "White"
/>
<
EditrowStyle
Backcolor
= "# 999999"
/>
<
SELECTEDROWSTYLE
Backcolor
= "# E2ded6"
Font-bold
= "True"
Forecolor
= "# 333333"
/>
<
Pagerstyle
Backcolor
= "# 284775"
Forecolor
= "White"
Horizontalalign
= "Center"
/>
<
HEADERSTYLE
Backcolor
= "# 5d7b9d"
Font-bold
= "True"
Forecolor
= "White"
/>
<
AlternatingRowStyle
Backcolor
= "White"
Forecolor
= "# 284775"
/>
ASP: GridView
>
TD
>
TR
>
TABLE
>
Div
>
FORM
>
Body
>
HTML
>
GridViewup.aspx.cs file code:
Using
System;
Using
System.data;
Using
System.configuration;
Using
System.collections;
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
Gridview_gridviewup: system.web.ui.page {
protected
Void
Page_load
Object
Sender, Eventargs E) {
IF
(
!
ISPostBack) {gridviewbind ();}}
protected
Void
GridView1_pageIndexchanging
Object
Sender, GridViewPageEventArgs E) {GridView1.pageIndex
=
E.NEWPAGEINDEX; GridViewBind ();
Private
Void
Gridviewbind () {
String
Conntr
=
ConfigurationManager.Connectionstrings [
"
Conntring
"
] .Connectionstring;
String
SQLSTR
=
"
SELECT *, CASE Xueli When '1' Ten 'Dr.' When '2' Ten 'Master' When '3' Ten 'BS' Else 'End As Xuelitext from Test01 WHERE Id <1000 and ID> 200
"
DataSet DS
=
New
DataSet ();
Try
{SQLCONNECTION CONN
=
New
SqlConnection (connStr);
IF
(conn.state.tostring ()
==
"
Closed
"
) Conn.open (); SqlDataAdapter Da
=
New
SqlDataAdapter (Sqlstr, Conn); da.fill (DS,
"
TEST01
"
);
IF
(conn.state.tostring ()
==
"
Open
"
CONN.CLOSE (); GridView1.DataSource
=
DS.TABLES [
0
] .DefaultView; gridview1.databind ();
Catch
(Exception ex) {response.write
"
Database error, wrong reason:
"
EX.MESSAGE); response.end ();}}
protected
Void
GridView1_rowDatabase (
Object
Sender, GridViewRowEventArgs E) {
IF
((DropDownList) E.Row.FindControl
"
DDLXUELI
"
))
! =
NULL
) {DropDownList DDLXUELI
=
(DropDownList) E.Row.FindControl
"
DDLXUELI
"
);
//
Generate the value of DropDownList, you can also get the data binding in the database
DDLXUELI.Items.clear (); ddlxueli.Items.add (
New
ListItem
"
Doctoral
"
,
"
1
"
))); Ddlxueli.Items.add (
New
ListItem
"
master's degree
"
,
"
2
"
))); Ddlxueli.Items.add (
New
ListItem
"
Bachelor's
"
,
"
3
"
));
//
//
Check DROPDOWNLIST
DDLXUELI.SelectedValue
=
(Hiddenfield) E.Row.FindControl
"
HDFXueli
"
)). Value;
//
}
protected
Void
GridView1_rowediting (
Object
Sender, GridViewediteventArgs E) {GridView1.editIndex
=
E.NEWEDITINDEX; GridViewBind ();
protected
Void
GridView1_rowcancelingit (
Object
Sender, GridViewCancelediteventArgs E) {GridView1.editIndex
=
-
1
GridViewBind ();
protected
Void
GridView1_ropdating
Object
Sender, GridViewUpdateEventArgs e) {
String
id
=
GridView1.DataKeys [E.Rowindex] .VALUES [
0
] .ToString ();
String
Card
=
(TextBox) GridView1.Rows [E.RowIndex] .findControl
"
TBCard
"
)).
String
Xueli
=
(DropDownList) GridView1.Rows [E.RowIndex] .findControl
"
DDLXUELI
"
)). SELECTEDVALUE;
String
PRICE
=
(TextBox) GridView1.Rows [E.RowIndex] .findControl
"
TBPRICE
"
)).
String
Conntr
=
ConfigurationManager.Connectionstrings [
"
Conntring
"
] .Connectionstring;
String
SQLSTR
=
"
Update test01 set card = '
"
Card
"
', Xueli ='
"
Xueli
"
', Price ='
"
PRICE
"
'Where id =
"
Id;
Try
{SQLCONNECTION CONN
=
New
SqlConnection (connStr); if
(conn.state.tostring ()
==
"
Closed
"
) Conn.open (); SQLCommand CommM
=
New
Sqlcommand (SQLSTR, Conn); Comm.ExecutenonQuery (); comm.dispose ();
IF
(conn.state.tostring ()
==
"
Open
"
); GridView1.editIndex
=
-
1
GridViewBind ();
Catch
(Exception ex) {response.write
"
Database error, wrong reason:
"
EX.MESSAGE); response.end ();}}
protected
Void
GridView1_rowdeleting
Object
Sender, GridViewDeleteEventArgs E) {
String
id
=
GridView1.DataKeys [E.Rowindex] .VALUES [
0
] .ToString ();
String
Conntr
=
ConfigurationManager.Connectionstrings [
"
Conntring
"
] .Connectionstring;
String
SQLSTR
=
"
Delete from test01 where id =
"
Id;
Try
{SQLCONNECTION CONN
=
New
SqlConnection (connStr);
IF
(conn.state.tostring ()
==
"
Closed
"
) Conn.open (); SQLCommand CommM
=
New
Sqlcommand (SQLSTR, Conn); Comm.ExecutenonQuery (); comm.dispose ();
IF
(conn.state.tostring ()
==
"
Open
"
); GridView1.editIndex
=
-
1
GridViewBind ();
Catch
(Exception ex) {response.write
"
Database error, wrong reason:
"
EX.MESSAGE); response.end ();}}}
SQL Server2000 generates table code:
Create
TABLE
[
DBO
]
.
[
TEST01
]
(
[
id
]
[
Decimal
]
(
18
,
0
)
Identity
(
1
,
1
)
NOT
NULL
,
[
Name
]
[
VARCHAR
]
(
50
COLLATE_PRC_CI_AS
NULL
,
[
Card]
[
VARCHAR
]
(
50
COLLATE_PRC_CI_AS
NULL
,
[
Xueli
]
[
VARCHAR
]
(
50
COLLATE_PRC_CI_AS
NULL
,
[
PRICE
]
[
Decimal
]
(
18
,
0
)
NULL
,
[
CreateDate
]
[
Datetime
]
NULL
)
On
[
Primary
]
Go
ALTER
TABLE
[
DBO
]
.
[
TEST01
]
Add
Constraint
[
DF_TEST01_CREATEDATE
]
DEFAULT
(
Getdate
())
For
[
CreateDate
]
,
Constraint
[
PK_TEST01
]
Primary
Key
Clustered
(
[
id
]
)
On
[
Primary
]
Go