Sometimes we need (1) use the drop-down box when edited, and the default is the content of the database (2) Use the drop-down box filtering data (3) Use the CSS unified DataGrid to give the code: Data structure: Table DEP: Depid (Identify Primary Key), DepName (College Name) Table Stu: Stuid (Identifier Primary Key), Stuname (Student Name), StudiePid (College ID = Table Dep.DEPID) Front Desk: <% @ Page Language = "CODEBEHIND =" Webform28.aspx.cs "autoeventwireup =" false "inherits =" 9cbs.webform28 "%>
"Stuid">
Namespace 9cbs {///
SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from stu, dep where stu.studepid = dep.depid", conn); DataSet ds = new DataSet (); da.fill (DS, "Table1"); this.dataGrid1.datasource = ds.tables ["Table1"]; this.dataGrid1.databind ();}
protected void setbind2 () {
SqlConnection conn2 = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da2 = new SqlDataAdapter ( "select * from dep", conn2); DataSet ds2 = new DataSet (); da2.Fill (ds2, "table1"); this.DropDownList1.DataSource = ds2.Tables [ "table1"]; this.DropDownList1.DataTextField = "depname"; this.DropDownList1.DataValueField = "depid"; this.DropDownList1.DataBind (); this. DropdownList1.Items.Insert (0, New ListItem ";
protected void SetBind3 () {string s = this.DropDownList1.SelectedValue; SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlCommand comm = new SqlCommand (); comm.Connection = conn; if (S! = "") {Comm.commandtext = "SELECT * from STU, Dep where stu.studepid = Dep.Depid and depid = @ depid"; sqlparameter parm1 = new sqlparameter ("@ depid", sqldbtype.It); parm1.Value = s; comm.Parameters.Add (parm1);} else comm.CommandText = "select * from stu, dep where stu.studepid = dep.depid"; SqlDataAdapter da = new SqlDataAdapter (); da.SelectCommand = Comm; DataSet DS = New Dataset (); DA.FILL (DS, "Table1"); this.dataGrid1.datasource = DS.TABLES ["Table1"]; this.dataGrid1.database ();} #Region Web Form The code generated by the designer Override protected void Oninit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///
private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from dep ", conn); DataSet DS = New Dataset (); DA.FILL (DS," Table1 "); if (E.Item.itemType == ListItemType.editItem) {DropDownList DDL = (DropDownList) E.Item.FindControl "dep"); ddl.datasource = ds.tables ["Table1"]; ddl.datatextfield = "depname"; DDL.DataValuefield = "depid"; ddl.database (); ddl.items.FindByValue (Convert.Tostring DataBinder.eval ("DEPID"))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))). Item.itemindex; if (this.dropdownList1.selectedValue == "") setbind (); else setbind3 ();
Protected Void Cancel (Object Sender, DataGridCommandeventArgs E) {this.dataGrid1.editItemIndex = -1; if (this.dropdownList1.SelectedValue == ") setbind (); else setbind3 ();}
protected void update (object sender, DataGridCommandEventArgs e) {if (e.Item.ItemType == ListItemType.EditItem) // pressed to edit only after submission {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ " conn "]); SqlCommand comm = new SqlCommand (" update stu set stuname = @ name, studepid = @ depid where stuid = @ id ", conn); SqlParameter parm1 = new SqlParameter (" @ name ", SqlDbType.NVarChar, 50 ); Parm1.value = ((TextBox) E.Item.FindControl ("name"))). Text; sqlparameter parm2 = new sqlparameter ("@ depid", sqldbtype.int); parm2.value = ((DropDownList) E. Item.FindControl ("dep")). SelectedValue; SQLParameter Parm3 = New SQLParameter ("@ id", sqldbtype.INT); PARM3.Value = this.dataGrid1.DataKeys [E.Item.itemindex]; Comm.Parameters.Add (PARM1); Comm.Parameters.Add (PARM2); Comm.Parameters.Add (PARM3); conn.open (); Comm.ExecutenonQuery (); conn.close (); this.dataGrid1.edititeMindex = -1; if; if (this.dropdownList1.selectedValue == "") setbind (); else setbind3 (); // Selected filtered using SetBind3 ()}} private void DropDownList1_SelectedIndexChanged (object sender, System.EventArgs e) {SetBind3 ();}}} css: .border {background-color: # 00496C;} header {font-family:. " Song Song ", SANS-Serif; Font-Size: 10pt; Font-Weight: bold; color: #fffff; Background-Color: # 0080c0; Text-Align: center;}. Item {font-family:" Song ", SANS -serif; font-size: 9pt; font-weight: Normal; Color: # 0080c0; Background-Color: #fffff; Text-Align: center;
The code is relatively simple, and the following is briefly explained: (1) setbind () is the basic binding; setbind2 () is the DROPDOWNLIST that is bound to be outside; setbind3 () is the binding of DataGrid after filtering after the drop-down box ( 2) Use the CSS to implement the table border to utilize CellSpacing, so this value is the width of the border, using Background-Color in the CSS of the Table Border to describe the color of the border. Below is some index of DataGrid this site: DataGrid implementation to delete (with prompt) to change the paging binding Dynamically changed DataGrid DataGrid in DataGrid Setting multiple parameters DataGrid Implement Self-amplification, radio, multi-selection DataGrid data export Several methods of downloading the Excel files DataGrid and DropDownList's cooperation and using CSS custom DataGrid