下面的是WebForm1.aspx文件 源文件下载 数据库文件下载 | |
<%
...
@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DatagridTest1.WebForm1"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > < HTML > < HEAD > < title > WebForm1 </ 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" > < LINK href ="http://www.jxecstac.com/web/160/image/style.css" type ="text/css" rel ="stylesheet" > < style type ="text/css" > ... .a1 {...}{ BORDER-RIGHT: #ff0000 1px solid; BORDER-TOP: #ff0000 1px solid; BORDER-LEFT: #ff0000 1px solid; BORDER-BOTTOM: #ff0000 1px solid } </ style > </ HEAD > < body MS_POSITIONING ="GridLayout" > < form id ="Form1" method ="post" runat ="server" > < FONT id ="FONT1" > < TABLE class ="a1" id ="Table1" style ="Z-INDEX: 101; LEFT: 8px; WIDTH: 832px; POSITION: absolute; TOP: 8px; HEIGHT: 307px" cellSpacing ="0" cellPadding ="0" width ="832" border ="1" > < TR > < TD class ="a1" style ="HEIGHT: 52px" >< 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 ="可见否" ></ asp:checkbox >< asp:button id ="Button1" runat ="server" Text ="保存" ></ asp:button >< asp:button id ="Button2" runat ="server" Text ="导出Excel" ></ asp:button ></ TD > </ TR > < TR > < TD class ="a1" style ="HEIGHT: 6px" >< asp:checkbox id ="CheckBox2" runat ="server" AutoPostBack ="True" Text ="全部选择" ></ asp:checkbox >< asp:button id ="btndelete" runat ="server" Text ="删除" ></ asp:button ></ TD > </ TR > < TR > < TD >< asp:datagrid id ="DataGrid1" runat ="server" DataKeyField ="UserID" AllowPaging ="True" PageSize ="5" AllowSorting ="True" CellPadding ="3" BackColor ="White" BorderWidth ="1px" BorderColor ="#CCCCCC" AutoGenerateColumns ="False" BorderStyle ="None" Height ="168px" > < SelectedItemStyle Font-Bold ="True" ForeColor ="White" BackColor ="#669999" ></ SelectedItemStyle > < ItemStyle ForeColor ="#000066" ></ ItemStyle > < HeaderStyle Font-Bold ="True" ForeColor ="White" BackColor ="#006699" ></ HeaderStyle > < FooterStyle ForeColor ="#000066" BackColor ="White" ></ FooterStyle > < Columns > < asp:TemplateColumn HeaderText ="序号" > < HeaderStyle Width ="35px" ></ HeaderStyle > < ItemTemplate > < INPUT id ="radio" type ="radio" name ="radio" > </ ItemTemplate > </ asp:TemplateColumn > < asp:TemplateColumn HeaderText ="序号" > < HeaderStyle Width ="35px" ></ HeaderStyle > </ asp:TemplateColumn > < asp:TemplateColumn HeaderText ="模板" > < HeaderStyle Width ="35px" ></ HeaderStyle > < ItemTemplate > < asp:CheckBox id ="chkExport" Width ="100%" 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 ="姓名" > < HeaderStyle Width ="70px" ></ HeaderStyle > < ItemTemplate > < asp:Label id =Label1 runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.UserName") % > ' Width="100%"> </ asp:Label > </ ItemTemplate > < EditItemTemplate > < asp:TextBox id =UserName runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.UserName") % > ' Width="100%"> </ asp:TextBox > </ EditItemTemplate > </ asp:TemplateColumn > < asp:TemplateColumn SortExpression ="province" HeaderText ="省市" > < HeaderStyle Width ="70px" ></ HeaderStyle > < ItemTemplate > < asp:Label id =Label2 runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.province") % > ' Width="100%"> </ asp:Label > </ ItemTemplate > < EditItemTemplate > < asp:DropDownList id ="province" runat ="server" AutoPostBack ="True" Width ="100%" OnSelectedIndexChanged ="ddlprovince_SelectedIndexChanged" ></ asp:DropDownList > </ EditItemTemplate > </ asp:TemplateColumn > < asp:TemplateColumn SortExpression ="city" HeaderText ="县市" > < HeaderStyle Width ="70px" ></ HeaderStyle > < ItemTemplate > < asp:Label id =Label3 runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.city") % > ' Width="100%"> </ asp:Label > </ ItemTemplate > < EditItemTemplate > < asp:DropDownList id ="city" runat ="server" AutoPostBack ="True" Width ="100%" OnSelectedIndexChanged ="ddlcity_SelectedIndexChanged" ></ asp:DropDownList > </ EditItemTemplate > </ asp:TemplateColumn > < asp:TemplateColumn SortExpression ="area" HeaderText ="乡镇" > < HeaderStyle Width ="70px" ></ HeaderStyle > < ItemTemplate > < asp:Label id =Label4 runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.area") % > ' Width="100%"> </ asp:Label > </ ItemTemplate > < EditItemTemplate > < asp:DropDownList id ="area" runat ="server" Width ="100%" ></ asp:DropDownList > </ EditItemTemplate > </ asp:TemplateColumn > < asp:TemplateColumn SortExpression ="Enabled" HeaderText ="可见否" > < HeaderStyle Width ="70px" ></ HeaderStyle > < ItemTemplate > < asp:Label id =Label5 runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.Enabled") % > ' Width="100%"> </ asp:Label > </ ItemTemplate > < EditItemTemplate > < asp:CheckBox id =chkenabled runat ="server" Width ="100%" Checked ='<%# DataBinder.Eval(Container.DataItem,"Enabled") % > '> </ asp:CheckBox > </ EditItemTemplate > </ asp:TemplateColumn > < asp:EditCommandColumn ButtonType ="LinkButton" UpdateText ="更新" HeaderText ="编辑" CancelText ="取消" EditText ="编辑" > < HeaderStyle Width ="70px" ></ HeaderStyle > </ asp:EditCommandColumn > < asp:TemplateColumn HeaderText ="删除" > < HeaderStyle Width ="35px" ></ HeaderStyle > < ItemTemplate > < asp:Button id ="btnDelete1" runat ="server" Text ="删除" Width ="60px" CommandName ="UserDelete" ></ asp:Button > </ ItemTemplate > </ asp:TemplateColumn > < asp:TemplateColumn HeaderText ="详细信息" > < HeaderStyle Width ="90px" ></ HeaderStyle > < ItemTemplate > < asp:HyperLink id ="HyperLink1" runat ="server" Text ="" Width ="100%" Target ="_blank" NavigateUrl ="http://www.guwenlvshi.com/" > 详细信息 </ asp:HyperLink > </ ItemTemplate > </ asp:TemplateColumn > </ Columns > < PagerStyle NextPageText ="下一页" PrevPageText ="上一页" HorizontalAlign ="Left" ForeColor ="#000066" BackColor ="White" ></ PagerStyle > </ asp:datagrid ></ TD > </ TR > </ TABLE > </ FONT > </ form > </ body > </ HTML > | |
下面是WebForm1.aspx.cs文件 | |
using
System;
using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Text; namespace DatagridTest1 ... { /**//// <summary> /// /// </summary> public class WebForm1 : System.Web.UI.Page ...{ protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.CheckBox CheckBox1; protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.DropDownList DropDownList3; protected System.Web.UI.WebControls.DropDownList DropDownList2; protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.Button btndelete; protected System.Web.UI.WebControls.CheckBox CheckBox2; private string constring=""; private void Page_Load(object sender, System.EventArgs e) ...{ if(!Page.IsPostBack) ...{ if(DataGrid1.Attributes["SortExpression"] == null) ...{ DataGrid1.Attributes["SortExpression"] = "UserID"; DataGrid1.Attributes["SortDirection"] = "ASC"; } ListProvince(); ListCity(); ListArea(); BindData(); } } private void BindData() ...{ 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.provinceID"; 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]; string SortExpression = DataGrid1.Attributes["SortExpression"]; string SortDirection = DataGrid1.Attributes["SortDirection"]; dt.DefaultView.Sort = SortExpression + " " + SortDirection; // dt.DefaultView.Sort="UserID asc"; dt.AcceptChanges(); this.DataGrid1.DataSource=dt; this.DataGrid1.DataBind(); } 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; } private DataTable dt ...{ get ...{ if(ViewState["dt"]!=null) ...{ return (DataTable)ViewState["dt"]; } else ...{ return null; } } set ...{ ViewState["dt"]=value; } } private void ListProvince() ...{ string sql="select * from province"; DataSet ds=GetDataSet(sql); DropDownList1.DataSource=ds; DropDownList1.DataTextField="province"; DropDownList1.DataValueField="provinceID"; DropDownList1.DataBind(); } private void ListCity() ...{ if(DropDownList1.SelectedIndex!=-1) ...{ string sql="select * from city where father='"+DropDownList1.SelectedValue+"'"; DataSet ds=GetDataSet(sql); DropDownList2.DataSource=ds; DropDownList2.DataTextField="city"; DropDownList2.DataValueField="cityID"; DropDownList2.DataBind(); } } 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(); } } Web Form#region Web Form override protected void OnInit(EventArgs e) ...{ // // // InitializeComponent(); base.OnInit(e); } /**//// <summary> /// </summary> private void InitializeComponent() ...{ this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated); this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand); this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged); this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand); this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand); this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand); this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand); this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand); this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); 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.CheckBox2.CheckedChanged += new System.EventHandler(this.CheckBox2_CheckedChanged); this.btndelete.Click += new System.EventHandler(this.btndelete_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) ...{ this.ListCity(); this.ListArea(); } private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e) ...{ this.ListArea(); } 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.DropDownList3.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(); this.BindData(); } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ...{ // this.DataGrid1.Columns[2].Visible=false; // e.Item.Cells[2].Visible=false; if(e.Item.ItemIndex!=-1) ...{ e.Item.Cells[1].Text=Convert.ToString(e.Item.ItemIndex+1); } if(e.Item.ItemType!=ListItemType.Header) ...{ if(e.Item.ItemIndex%2==0) ...{ e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor='#bbaa99'"); } else ...{ e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor='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; e.Item.Cells[1].Visible=false; break; case ListItemType.EditItem: // for (int i=4; i < DataGrid1.Columns.Count-2; i++) // { // e.Item.Cells[i].Attributes.Add("Width", "70px"); // } string sqlprovince="select * from province"; DataSet dsprovince=GetDataSet(sqlprovince); DropDownList ddlprovince=(DropDownList)e.Item.FindControl("province"); ddlprovince.DataSource=dsprovince; ddlprovince.DataTextField="province"; ddlprovince.DataValueField="provinceID"; ddlprovince.DataBind(); ddlprovince.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"provinceID"))).Selected=true; if(ddlprovince.SelectedIndex!=-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(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"cityID"))).Selected=true; 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.DataBind(); ddlarea.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"areaID"))).Selected=true; } } break; } } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) ...{ this.DataGrid1.EditItemIndex=e.Item.ItemIndex; this.BindData(); } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) ...{ this.DataGrid1.EditItemIndex=-1; this.BindData(); } 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 DropDownList; string sqlcity="select * from city where father='"+ddlprovince.SelectedValue+"'"; DataSet dscity=GetDataSet(sqlcity); ddlcity.DataSource=dscity; ddlcity.DataTextField="city"; ddlcity.DataValueField="cityID"; ddlcity.DataBind(); if(ddlcity.SelectedIndex!=-1) ...{ DropDownList ddlarea = ((Control)(((Control)sender)).Parent).FindControl("area") 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(); } } } public void ddlcity_SelectedIndexChanged(object sender, EventArgs e) ...{ DropDownList ddlcity = sender as DropDownList; if(ddlcity.SelectedIndex!=-1) ...{ DropDownList ddlarea = ((Control)(((Control)sender)).Parent).FindControl("area") 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(); } } private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.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.FindControl("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; this.BindData(); } } private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ...{ switch(e.Item.ItemType) ...{ case ListItemType.Item: case ListItemType.EditItem: case ListItemType.AlternatingItem: Button myDeleteButton = (Button)e.Item.FindControl("btnDelete1"); myDeleteButton.Text = "删除"; myDeleteButton.Attributes.Add("onclick", "return confirm('你确认删除此项吗?');"); break; } } private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) ...{ if(DataGrid1.Items.Count==1) ...{ if(DataGrid1.CurrentPageIndex!=0) DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1; } string strSql = "delete from testgrid where UserID='"+e.Item.Cells[3].Text+"'"; try ...{ string strconn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];// SqlConnection conn =new SqlConnection(strconn); SqlCommand com = new SqlCommand(strSql,conn); conn.Open(); com.ExecuteNonQuery(); conn.Close(); } catch(Exception e1) ...{ Response.Write("<script language = 'javascript'> alert('"+e1.Message+"');</script>") ; } BindData(); } private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) ...{ if(e.CommandName=="UserDelete") DataGrid1_DeleteCommand(source,e); } private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) ...{ string SortExpression = e.SortExpression.ToString(); string SortDirection = "ASC"; if(SortExpression == DataGrid1.Attributes["SortExpression"]) ...{ SortDirection = (DataGrid1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC"); } DataGrid1.Attributes["SortExpression"] = SortExpression; DataGrid1.Attributes["SortDirection"] = SortDirection; BindData(); } private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) ...{ DataGrid1.CurrentPageIndex = e.NewPageIndex; this.BindData(); } private void Button4_Click(object sender, System.EventArgs e) ...{ } private void btndelete_Click(object sender, System.EventArgs e) ...{ StringBuilder s=new StringBuilder(); foreach(DataGridItem di in DataGrid1.Items) ...{ if(((CheckBox)(di.FindControl("chkExport"))).Checked==true) ...{ s.Append(",").Append(DataGrid1.DataKeys[di.ItemIndex].ToString()); } } if(s.ToString().StartsWith(",")==true) ...{ delete(s.ToString().Substring(1,s.ToString().Length-1)); } } 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(); this.BindData(); } private void CheckBox2_CheckedChanged(object sender, System.EventArgs e) ...{ for (int i = 0; i <= this.DataGrid1.Items.Count-1;i++) ...{ CheckBox cbox = (CheckBox)this.DataGrid1.Items[i].FindControl("chkExport"); if (CheckBox2.Checked == true) ...{ cbox.Checked = true; this.CheckBox2.Text = "取消全选"; } else ...{ cbox.Checked = false; this.CheckBox2.Text = "全部选择"; } } } private void Button2_Click(object sender, System.EventArgs e) ...{ // HttpContext.Current.Response.Charset ="UTF-8";GB18030 // HttpContext.Current.Response.Charset ="GB18030"; Response.ContentEncoding=System.Text.Encoding.Default; Response.ContentType = "application/vnd.ms-excel"; Response.AppendHeader("Content-Disposition","attachment;filename=1.xls"); this.EnableViewState = false; System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); int nCur = DataGrid1.CurrentPageIndex; int nSize = DataGrid1.PageSize; DataGrid1.AllowPaging = false; DataGrid1.AllowSorting=false; BindData(); DataGrid1.Columns[0].Visible =false; DataGrid1.Columns[1].Visible =false; DataGrid1.Columns[2].Visible =false; DataGrid1.Columns[9].Visible =false; DataGrid1.Columns[10].Visible =false; DataGrid1.RenderControl(hw); DataGrid1.Columns[0].Visible =true; DataGrid1.Columns[1].Visible =true; DataGrid1.Columns[2].Visible =true; DataGrid1.Columns[9].Visible =true; DataGrid1.Columns[10].Visible =true; DataGrid1.AllowSorting=true; DataGrid1.AllowPaging = true; DataGrid1.CurrentPageIndex = nCur; DataGrid1.PageSize = nSize; this.EnableViewState = true; BindData(); Response.Write(sw.ToString()); Response.End(); } } } |
功能齐全的DataGrid (根据小山老师的例子修改的)
最新推荐文章于 2018-06-14 11:43:28 发布