//
Current features: editing, sorting, single option, multiple option, merge cell, sequence number, linkage pull down box, delete, super connection, mouse move change color, parity color distinction, sort, paging
1.html code
<
HTML
>
<
HEAD
>
<
Title
>
DataGridtest
Title
>
<
Meta
Content
= "Microsoft Visual Studio .NET 7.1"
Name
= "Generator"
>
<
Meta
Content
= "C #"
Name
= "Code_Language"
>
<
Meta
Content
= "JavaScript"
Name
= "VS_DEFAULTCLIENTScript"
>
<
Meta
Content
= "http://schemas.microsoft.com/intellisense/ie5"
Name
= "vs_targetschema"
>
<
Script
Language
= "JavaScript"
>
function fun_option (obj) {document.Form1.hiduserid.value = obj.value;} function chkAll_true () {var chkall = document.all [ "chkAll"]; var chkother = document.getElementsByTagName ( "input"); for ( VAR i = 0; I
}}}} {ALERT ('You NEED TO SELECT A ROW IN THE LIST Before SELECTING DELETE./r/nplease select a row and try again.');} Else f (Window.confirm ("Are you Sure to delete All these)) {var deleteid = '<% = btndelete.clientID%>'; window.document.all (deleteid) .click ();} else {return false;}} < /
Script
>
HEAD
>
<
Body
>
<
FORM
id
= "Form1"
Method
= "POST"
Runat
= "Server"
>
<
TABLE
id
= "Table1"
HEIGHT
= "100%"
Cellspacing
= "1"
Cellpadding
= "1"
Width
= "100%"
Border
= "1"
>
<
TR
>
<
TD
> <
ASP: DropDownList
id
= "DROPDOWNLIST1"
Runat
= "Server"
AutoPostback
= "True"
>
ASP: DropDownList
> <
ASP: DropDownList
id
= "DROPDOWNLIST2"
Runat
= "Server"
AutoPostback
= "True"
>
ASP: DropDownList
> <
ASP: DropDownList
id
= "DROPDOWNLIST3"
Runat
= "Server"
>
ASP: DropDownList
> <
ASP: TEXTBOX
id
= "TextBox1"
Runat
= "Server"
>
ASP: TEXTBOX
> <
ASP: Checkbox
id
= "CheckBox1"
Runat
= "Server"
TEXT
= "Can you see?
>
ASP: Checkbox
> <
ASP: Button
id
= "Button1"
Runat
= "Server" text
= "Save"
>
ASP: Button
> <
ASP: Button
id
= "Button2"
Runat
= "Server"
TEXT
= "Export Excel"
>
ASP: Button
>
TD
>
TR
>
<
TR
HEIGHT
= "30"
>
<
TD
> <
INPUT
id
= "chkall"
Onclick
= "chkall_true ()"
Type
= "Checkbox"
>
All-selection / cancellation
<
ASP: Button
id
= "btndelete"
Runat
= "Server"
TEXT
= "Delete"
>
ASP: Button
>
TD
>
TR
>
<
TR
Valign
= "TOP"
>
<
TD
Width
= "100%"
Colspan
= "5"
> <
ASP: DATAGRID
id
= "DataGrid1"
Runat
= "Server"
OndeleteCommand
= "Delete"
Forecolor
= "Black"
Gridlines
= "None"
Cellpadding
= "2"
Backcolor
= "Lightgoldenrodyellow"
Borderwidth
= "1px"
Bordercolor
= "TAN"
ALLOWSORTING
= "True"
AutogenerateColumns
= "False"
Datakeyfield
= "UserID"
OnupdateCommand
= "Update"
Oncancelcommand
= "Cancel"
OneditCommand
= "Edit"
PageSize
= "5"
Allowpaging
= "True"
>
<
Footerstyle
Backcolor
= "TAN"
>
Footerstyle
>
<
SelectEdItemStyle
Forecolor
= "Ghostwhite"
Backcolor
= "DARKSLATEBLUE"
>
SelectEdItemStyle
>
<
AlternatingItemStyle
Backcolor
= "Palegoldenrod"
>
AlternatingItemStyle
>
<
HEADERSTYLE
Font-bold
= "True"
Backcolor
= "TAN"
>
HEADERSTYLE>
<
Columns
>
<
ASP: TemplateColumn
Headertext
= "Sequence"
>
<
HEADERSTYLE
Width
= "10px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<
INPUT
id
= Radio
Onclick
= fun_option (this)
Type
= Radio
Value
= '<% #
DataBinder.eval (Container.DataItem, "UserID")%
>
'Name = Radio>
ItemTemplate
>
<
Footertemplate
>
Footertemplate
>
ASP: TemplateColumn
>
<
ASP: TemplateColumn
Headertext
= "Sequence"
>
<
HEADERSTYLE
Width
= "10px"
>
HEADERSTYLE
>
<
Footertemplate
>
Footertemplate
>
ASP: TemplateColumn
>
<
ASP: TemplateColumn
>
<
HEADERSTYLE
Width
= "10px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<
ASP: Checkbox
id
= "ChKEXPORT"
Runat
= "Server"
>
ASP: Checkbox
>
ItemTemplate
>
ASP: TemplateColumn
>
<
ASP: BoundColumn
Datafield
= "UserID"
Sortexpression
= "UserID"
Readonly
= "True"
Headertext
= "UserID"
>
<
HEADERSTYLE
Width
= "70px"
>
HEADERSTYLE
>
ASP: BoundColumn
>
<
ASP: TemplateColumn
Sortexpression
= "Username"
Headertext
= "Name"
>
<
HEADERSTYLE
Width
= "70px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<
a
HREF
= 'FORM1.ASPX? ID = <% #
DataBinder.eval (Container.DataItem, "UserID")%
>
& name
=
<%
# DataBinder.eval (Container.DataItem, "UserName")
%>
'Target = "_ blank"> <%
# DataBinder.eval (Container.DataItem, "UserName")
%>
a
>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: TEXTBOX
id
= Username
TEXT
= '<% #
DataBinder.eval (Container.DataItem, "UserName")%
>
'Width = "100%" runat = "server">
ASP: TEXTBOX
>
EditItemTemplate
>
ASP: TemplateColumn
>
<
ASP: TemplateColumn
Sortexpression
= "province"
Headertext
= "Province"
>
<
HEADERSTYLE
Width
= "70px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<%
# DataBinder.eval (Container.DataItem, "province")
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: DropDownList
id
= "province"
Width
= "100%"
Runat
= "Server"
AutoPostback
= "True"
OnseledIndexchanged
= "DDLPROVINCE_SELECTEDEXCHANGED"
>
ASP: DropDownList
>
EditItemTemplate
>
ASP: TemplateColumn
>
<
ASP: TemplateColumn
Sortexpression
= "city"
Headertext
= "County"
>
<
HEADERSTYLE
Width
= "70px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<%
# DataBinder.eval (Container.DataItem, "City")
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: DropDownList
id
= "city"
Runat
= "Server"
Width
= "100%"
AutoPostback
= "True"
OnseledIndexchanged
= "DDLCITY_SELECTEDEXCHANGED"
>
ASP: DropDownList
>
EditItemTemplate
>
ASP: TemplateColumn
> <
ASP: TemplateColumn
Sortexpression
= "Area"
Headertext
= "Township"
>
<
HEADERSTYLE
Width
= "70px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<%
# DataBinder.eval (Container.DataItem, "Area")
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: DropDownList
id
= "Area"
Runat
= "Server"
Width
= "100%"
>
ASP: DropDownList
>
EditItemTemplate
>
ASP: TemplateColumn
>
<
ASP: TemplateColumn
Sortexpression
= "Enabled"
Headertext
= "Can you see?
>
<
HEADERSTYLE
Width
= "70px"
>
HEADERSTYLE
>
<
ItemTemplate
>
<%
# DataBinder.eval (Container.DataItem, "enabled")
%>
ItemTemplate
>
<
EditItemTemplate
>
<
ASP: Checkbox
id
= Ch NBENABLED
Runat
= "Server"
Checked
= '<% #
DataBinder.eval (Container.DataItem, "enabled")%
>
"
ASP: Checkbox
>
EditItemTemplate
>
ASP: TemplateColumn
>
<
ASP: EditcommandColumn
Buttype
= "Pushbutton"
UpdateText
= "Update"
Headertext
= "Edit"
Canceltext
= "Cancel"
EditText
= "Edit"
>
ASP: EditcommandColumn
>
<
ASP: ButtonColumn
TEXT
= "Delete"
Buttype
= "Pushbutton"
Headertext
= "Delete"
CommandName
= "Delete"
>
ASP: ButtonColumn
>
Columns
>
<
Pagerstyle
NextPageText
= "Next page"
PrevpageText
= "Previous page"
Horizontalalign
= "Right"
Forecolor
= "DARKSLATEBLUE"
Backcolor = "Palegoldenrod"
Mode
= "Numericpages"
>
Pagerstyle
>
ASP: DATAGRID
>
TD
>
TR
>
<
TR
HEIGHT
= "30"
>
<
TD
>
TD
>
TR
>
TABLE
>
<
INPUT
id
= "HIDUSERID"
Type
= "hidden"
Runat
= "Server"
>
FORM
>
Body
>
HTML
>
2.CS code
Using
System.data.sqlclient;
Using
System.Text;
public
Class
DataGridtest: System.Web.ui.page
{Protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.HtmlControls.HtmlInputHidden hiduserid; protected System.Web.UI.WebControls.Button btndelete; protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.DropDownList DropDownList2; protected System.Web.UI.WebControls.DropDownList DropDownList3; protected System.Web.UI.WebControls.CheckBox CheckBox1; protected System.Web.UI.WebControls.TextBox TextBox1; protected System. Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Button Button2; private string constring = ""; (! Page.IsPostBack) private void Page_Load (object sender, System.EventArgs e) {if {ListProvince (); ListRea (); btndelete.attributes.add ("onclick", "Return delete ();"); sortex PRESSION = ""; sort = "dec"; databind ();}} DataBind # Region DataBind Private Void Database () {string SQL = "SELECT A.USERID, A.USERNAME, B.PROVINCE, C.CITY, D. Area, a.enabled, b.ProvinceId, C.CityId, D.areAid from testgrid a "; sql =" Left join province b on a.provinceid = b.Province "; sql =" Left join city c on a.cityID = C.CITYID "; SQL =" Left Join Area D on A.areAid = D.areaid "; DataSet DS = getDataSet (SQL); DT = DS.TABLES [0]; DT.DEFAULTVIEW.SORT =" Userid ASC " ;
dt.AcceptChanges (); this.DataGrid1.DataSource = dt; this.DataGrid1.DataBind (); DataTable dt1 = new DataTable (); dt1 = (DataTable) (DataGrid1.DataSource);} #endregion Web Form Designer generated code # region Web Form Designer generated code override protected void OnInit (EventArgs e) {// // CODEGEN: This call is required by the ASP.NET Web Form Designer // InitializeComponent (); base.OnInit (e); this.DataGrid1. .ItemDataBound = new DataGridItemEventHandler (DataGrid1_ItemDataBound); this.DataGrid1.SortCommand = new DataGridSortCommandEventHandler (DataGrid1_SortCommand); this.DataGrid1.ItemCreated = new DataGridItemEventHandler (DataGrid1_ItemCreated); this.DataGrid1.PageIndexChanged = new DataGridPageChangedEventHandler (DataGrid1_PageIndexChanged); this.btndelete.Click = New system.eventhandler (this.btndelete_clic k); this.DropDownList1.SelectedIndexChanged = new System.EventHandler (this.DropDownList1_SelectedIndexChanged); this.DropDownList2.SelectedIndexChanged = new System.EventHandler (this.DropDownList2_SelectedIndexChanged); this.Button1.Click = new System.EventHandler (this .Button1_click; this.button2.click = new system.eventhandler (this.button2_click);} / ** ////
private void InitializeComponent () {this.DropDownList1.SelectedIndexChanged = new System.EventHandler (this.DropDownList1_SelectedIndexChanged); this.DropDownList2.SelectedIndexChanged = new System.EventHandler (this.DropDownList2_SelectedIndexChanged); this.Button1.Click = new System. EventHandler (this.Button1_Click); this.Button2.Click = new System.EventHandler (this.Button2_Click); this.Load = new System.EventHandler (this.Page_Load);} #endregion GetDataSet # region GetDataSet private DataSet GetDataSet ( string sql) {constring = System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]; SqlDataAdapter sda = new SqlDataAdapter (sql, constring); DataSet ds = new DataSet (); sda.Fill (ds); return ds;} # EndRegion DataGrid1_itemdatabase # Region DataGrid1_ItemDatabaseD Private Void DataGr ID1_itemdatabase (Object Sender, DataGriditeMeventargs E) {E.Item.cells [1] .Text = Convert.TOSTRING (E.Item.itemindex 1); // Generate the serial number IF (E.Item.itemType! = ListItemType.Header) {If (E.Item.itemindex% 2 == 0) {E.Item.attributes.add ("onmouseout", "this.style.backroundcolor = 'palegoldenrod'");} else {E.Item.attributes.add ("OnMouseout", "this.style.backroundcolor = 'lightgoldenrodyellow'");} E.Item.attributes.add ("
OnMouseOver "," this.style.BackgroundColor = '# c1d2ee' ");} Switch (E.Item.itemType) {CASE ListItemType.Item: Case ListItemType.alternatingItem: E.Item.attributes.add (" OnDBLClick "," Alert ('" E.Item.itemindex "); "); Break; Case ListItemType.Header: E.Item.cells [0] .COLUMNSPAN = 2; // Merge unit E.Item.cells [1] .Visible = false; Break; Case ListItemType.editItem: for (int i = 4; i // Secondary IF (E.Item.itemType == ListItemType.editItem) {if (DDLPROVINECE.SELECTEDIDEX! = - 1) {string sqlcity = "select * from city where father = '" DDLPROVINCE.SELECTEDVALUE "'"; DropDownList ddlcity = (DropDownList) e.Item.FindControl ( "city"); DataSet dscity = GetDataSet (sqlcity); ddlcity.DataSource = dscity; ddlcity.DataTextField = "city"; ddlcity.DataValueField = "cityID"; ddlcity.DataBind (); DDLCITY.ITEMS.FindByValue (DataBinder.eval ("CityId")))))))))))))))))). SELECTED = true; // Level IF (E.Item.itemType == ListItemType.editItem ) { if (ddlcity.SelectedIndex = - 1!) {string sqlarea = "select * from area where father = '" ddlcity.SelectedValue "'"; DropDownList ddlarea = (DropDownList) e.Item.FindControl ( "area"); DataSet Dsarea = getDataSet (Sqlarea); ddlarea.datasource = dsarea; ddlarea.DataTextField = "Area"; DDLAREA.DATAVALUEFIELD = "Areaid"; ddlarea.database (); ddlarea.items.FindByValue (DataBinder.eval ("AreaId")))). SELECTED = true;}}}}}}}}}}}}}}}}}}}}}}}} break; default: break;}} #endregion DataGrid1_SortCommand # region DataGrid1_SortCommand private void DataGrid1_SortCommand (object source, DataGridSortCommandEventArgs e) {if (SortExpression == e.SortExpression.ToString ()) {if (sort == "asc") {sort = "DESC";} else {sort = "ASC";}} else {if (sort == "ASC") {Sort = "desc";} else {sort = "ASC";}} sortexpression = e.Sortexpression; dt.defaultview " sort; datagrid1.datasource = dt; dataGrid1.databind (); } #endregion DataGrid1_ItemCreated # region DataGrid1_ItemCreated private void DataGrid1_ItemCreated (object sender, DataGridItemEventArgs e) {for (int i = 3; i ) // - 2 means that two Button columns are not sorted {if (i == 0) Continue; if (E.Item.ItemIndex == - 1) {ix (datagrid1.columns [i] .sortexpression == Sortexpression) {Try {Tablecell TableCell = E.Item.cells [i]; label label = new label (); label.font.name = "WebDings"; if (sort == "ASC") {label.text = " 6 ";} else {label.text =" 5 ";} label.width = 20; tablecell.controls.add (label);} catch {}}}} foreach (DataGridItem di in this.data Grid1.items) {if (Di.ItemType == ListItemType.Item || di.itemType == ListItemType.alternatingItem) {(button) Di.cells [10] .controls [0]). Attributes.add ("onclick "," RETURN CONFIRM ('confirmation delete this item?'); ");}}} #ENDREGION PROPERTY # region profions property DataTable DT {Get {if (ViewState [" DT "]! = null) {return (DataTable ViewState ["DT"]; Else {Return Null }}} Set {viewState ["DT"] = value;}} private string sortexpression {get {ix (viewState ["sortexpression"]! = Null && viewstate ["sortexpression"]. Tostring ()! = String.empty) { Return ViewState ["sortexpression"]. TOSTRING ();} else {return ";}} set {viewState [" sortexpression "] = value;}} private string sort {get {ix (viewstate [" sort "]! = Null && viewState ["sort"]. TOSTRING ()! = String.empty) {Return ViewState ["sort"]. Tostring ();} else {return "";}}}}} "}}}] = value ; }} #Endregion Edit # region Edit protected void Edit (object sender, DataGridCommandEventArgs e) {this.DataGrid1.EditItemIndex = e.Item.ItemIndex; DataBind ();} #endregion Cancel # region Cancel protected void Cancel (object sender, DataGridCommandEventArgs e) {this.DataGrid1.edititeMindex = -1; databind ();} #endregion update # region update protected void update (Object sender, DataGridCommandEventArgs E) {if (E.Item.ItemType == ListItemType.EditItem) {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]); SqlCommand comm = new SqlCommand ( "update testgrid set UserName = @ UserName, provinceID = @ province, cityID = @ city, areaID = @ area, Enabled = @ Enabled where UserID = @ UserID ", conn); SqlParameter parm1 = new SqlParameter (" @ UserName ", SqlDbType.NVarChar, 50); parm1.Value = ((TextBox) e.Item.FindControl ( "UserName")) Text;. SqlParameter parm2 = new SqlParameter ( "@ province", SqlDbType.NVarChar, 50); parm2.Value = ((DropDownList) e.Item.FindControl ( "province")) SelectedValue;. SqlParameter PARM3 = New SqlParameter ("@ City", Sqldbtype.nvarchar, 50); PARM3.Value = ((DropDownList) E.Item.FindControl ("city")). SELECTEDVALUE; SQLParameter PARM4 = New Sqlparameter ("@ area", Sqldbtype.nvarchar, 50); PARM4.VALUE = (DropDownList) E.Item.fi ndControl ( "area")) SelectedValue;. SqlParameter parm5 = new SqlParameter ( "@ Enabled", SqlDbType.Bit); parm5.Value = ((CheckBox) e.Item.FindControl ( "chkenabled")) Checked;. SqlParameter parm6 = New SqlParameter ("@ userid", sqldbtype.int); PARM6.Value = this.dataGrid1.datakeys [e.Item.itemindex]; comm.Parameters.Add (PARM1); Comm.Parameters.Add (PARM2); Comm. .Parameters.Add (PARM3); Comm.Parameters.Add (PARM4); Comm.Parameters.Add (PARM5); Comm.Parameters.Add (PARM6); conn.open (); comm.executenonquery (); conn.close (); this.DataGrid1.edititeMindex = -1; databind (); } #endregion Delete event # region Delete event protected void Delete (object sender, DataGridCommandEventArgs e) {delete (this.DataGrid1.DataKeys [e.Item.ItemIndex] .ToString ());} #endregion ddlprovince_SelectedIndexChanged # region ddlprovince_SelectedIndexChanged public void ddlprovince_SelectedIndexChanged (object sender, EventArgs e) {DropDownList ddlprovince = sender as DropDownList; if (! ddlprovince.SelectedIndex = - 1) {DropDownList ddlcity = (. (Control) (((Control) sender)) Parent) .FindControl ( "city" ) As dropdownload; string sqlcity = "select * from city where father = '" DDLPROVINCE.SELECTEDVALUE "'" DataSet dscity = GetDataSet (sqlcity); ddlcity.DataSource = dscity; ddlcity.DataTextField = "city"; ddlcity.DataValueField = "cityID"; ddlcity.DataBind ();}} #endregion ddlcity_SelectedIndexChanged # region ddlcity_SelectedIndexChanged public void ddlcity_SelectedIndexChanged (object sender Eventargs e) {DropDownloadList Ddlcity = sender as dropdownload; if (ddlcity.selectedIndex! = - 1) {DropdownList DDLAREA = ((Control) ((Control)) .findcontrol (" city ") as DropDownList; string sqlarea =" select * from area where father = ' " ddlcity.SelectedValue "' "; DataSet dsarea = GetDataSet (sqlarea); ddlarea.DataSource = dsarea; ddlarea.DataTextField =" area "; ddlarea .DataValueField = "areaID"; ddlarea.DataBind ();}} #endregion btndelete_Click # region btndelete_Click private void btndelete_Click (object sender, System.EventArgs e) {StringBuilder s = new StringBuilder (); foreach (DataGridItem di in DataGrid1.Items ) {IF ("ChKEXPORT"))). Checked == true) {s.append (","). Append (dataGrid1.datakeys [di.itemindex] .tostring ()) }}}. Startswith (",") == true) {delete () {delete (). Substring (1, s.toString (). Length-1));}} # endregion delete # region delete private void delete (string id) {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]); string sql = @ "declare @sql nvarchar (400) set @sql = ' Delete from testgrid where userid in (' @ userid ') 'exec (@SQL) "; SQLCommand Comm = New SQLCommand (SQL, Conn); SQLParameter Parm1 = New SQLParameter (" @ userid ", sqldbtype.varchar, 200); PARM1.VALUE = ID; comm.Parameters.Add (PARM1); conn.open (); comm.executenonquery (); conn.close (); databind ();} #ENDREGION ListProVince # Region ListProVince Private Void ListProVince () { string sql = "select * from province"; DataSet ds = GetDataSet (sql); DropDownList1.DataSource = ds; DropDownList1.DataTextField = "province"; DropDownList1.DataValueField = "provinceID"; DropDownList1.DataBind ();} #endregion ListCity #region ListCity private void ListCity () {if (DropDownList1.SelectedIndex = - 1!) {string sql = "select * from city where father = '" DropDownList1.SelectedValue "'"; DataSet ds = GetDataSet (sql); DropDownList2 .Datasource = DS; dropdownload = "city"; DropDownList2.Datavaluefield = "cityID"; DropDownList2.DataBind ();}} #endregion ListArea # region ListArea private void ListArea () {if (! DropDownList2.SelectedIndex = - 1) {string sql = "select * from area where father = '" DropDownList2.SelectedValue "' "; DataSet DS = getDataSet (SQL); DROPDOWNLIST3.DATASOURCE = DS; DropDownList3.DataTextField =" Area "; DropDownList3.DataValuefield =" Areaid "; DropDownList3.DataBind ();}} #endregion save # region save private void Button1_Click (object sender, System.EventArgs e) {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]); SqlCommand comm = new SqlCommand ( "insert into testgrid (UserName, provinceID, cityID, areaID, Enabled) values (@ UserName, @ provinceID, @ cityID, @ areaID, @ Enabled)", conn); SqlParameter parm1 = new SqlParameter ( "@ UserName" , SqlDbType.NVarChar, 50); parm1.Value = this.TextBox1.Text; SqlParameter parm2 = new SqlParameter ( "@ provinceID", SqlDbType.NVarChar, 50); parm2.Value = this.DropDownList1.SelectedValue; SqlParameter parm3 = new SqlParameter ( "@ cityID", SqlDbType.NVarChar, 50); parm3.Value = this.DropDownList2.SelectedValue; SqlParameter parm4 = new SqlParameter ( "@ areaID", SqlDbType.NVarChar, 50); parm4.Value = this.DropD ownList3.SelectedValue; SqlParameter parm5 = new SqlParameter ( "@ Enabled", SqlDbType.Bit); parm5.Value = this.CheckBox1.Checked; comm.Parameters.Add (parm1); comm.Parameters.Add (parm2); comm. Parameters.Add (PARM3); Comm.Parameters.Add (PARM4); Comm.Parameters.Add (PARM5); conn.Open (); comm.executenonquery (); conn.close (); databind (); } #Endregion private void DropDownList1_SelectedIndexChanged (object sender, System.EventArgs e) {ListCity ();} private void DropDownList2_SelectedIndexChanged (object sender, System.EventArgs e) {ListArea ();} private void Button2_Click (object sender, System.EventArgs e ) {TOEXCEL (THISDATAGRID1, "Meng");} TOEXCEL # Region TOEXCEL PUBLIC STATIC VOID TOEXCEL (System.Web.ui.Control CTL, String FileName) {httpContext.current.response.charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application / ms-excel"; HttpContext.Current.Response.AppendHeader ( "Content-Disposition", "attachment; filename = " " FileName ". Xls"); ctl.page.enableviewState = false; system.io.stringWriter TW = new system.io.stringWriter (); system .Web.ui.htmltextwriter hw = new system.web.ui.htmlTextWriter (TW); CTL.RenderControl (hw); httpContext.current.response.write (20); httpContext.current.Response.end ( );} #endregion private void DataGrid1_PageIndexChanged (object source, DataGridPageChangedEventArgs e) {this.DataGrid1.CurrentPageIndex = e.NewPageIndex; DataBind ();.}} 3 textbook database (or download real data /Files/singlepine/area.rar) IF exists ( SELECT * From DBO.SYSOBJECTS WHERE id = Object_id (N ' [dbo]. [TestGrid] ' ) and ObjectProperty (ID, N ' Isusertable ' ) = 1 ) Drop TABLE [ DBO ] . [ TestGrid ] Go IF exists ( SELECT * From DBO.SYSOBJECTS WHERE id = Object_id (N ' [dbo]. [province] ' ) and ObjectProperty (ID, N ' Isusertable ' ) = 1 ) Drop TABLE [ DBO ] . [ province ] Go IF exists ( SELECT * From DBO.SYSOBJECTS WHERE id = Object_id (N ' [dbo]. [City] ' ) and ObjectProperty (ID, N ' Isusertable ' ) = 1 ) Drop TABLE [ DBO ] . [ CITY ] Go IF exists ( SELECT * From DBO.SYSOBJECTS WHERE id = Object_id (N ' [dbo]. [area] ' ) and ObjectProperty (ID, N ' Isusertable ' ) = 1 ) Drop TABLE [ DBO ] . [ Area ] Go Create TABLE [ DBO ] . [ TestGrid ] ( [ UserID ] [ int ] Identity ( 1 , 1 ) NOT NULL , [ Username ] [ nvarchar ] ( 50 COLLATE_PRC_CI_AS NULL , [ provinceID ] [ nvarchar ] ( 50 COLLATE_PRC_CI_AS NULL , [ CityID ] [ nvarchar ] ( 50 COLLATE_PRC_CI_AS NULL , [ AreaID ] [ nvarchar ] ( 50 COLLATE_PRC_CI_AS NULL , [ Enabled ] [ Bit ] NULL ) On [ Primary ] Go Create TABLE [ DBO ] . [ province ] ( [ id ] [ int ] NOT NULL , [ provinceID ] [ nvarchar ] ( 6 COLLATE_PRC_CI_AS NULL , [ province ] [ nvarchar ] ( 40 COLLATE_PRC_CI_AS NULL ) On [ Primary ] Go Create TABLE [ DBO ] . [ CITY ] ( [ id ] [ int ] NOT NULL , [ CityID ] [ nvarchar ] ( 6 COLLATE_PRC_CI_AS NULL , [ CITY ] [ nvarchar ] ( 50 COLLATE_PRC_CI_AS NULL , [ Father] [ nvarchar ] ( 6 COLLATE_PRC_CI_AS NULL ) On [ Primary ] Go Create TABLE [ DBO ] . [ Area ] ( [ id ] [ int ] NOT NULL , [ AreaID ] [ nvarchar ] ( 50 COLLATE_PRC_CI_AS NULL , [ Area ] [ nvarchar ] ( 60 COLLATE_PRC_CI_AS NULL , [ Father ] [ nvarchar ] ( 6 COLLATE_PRC_CI_AS NULL ) On [ Primary ] Go