1、aspnet gridview目录ASP.NETGridView点击行触发SelectedIndexChanged事件 1gridview72般绝技 31.GridView无代码分页排序: 32.GridView选中,编辑,取消,删除: 43.GridView正反双向排序: 104.GridView和下拉菜单DropDownList结合: 15datagridview.Rowsi.DefaultCellStyle.BackColor背景色datagridview.Rowsi.DefaultCellStyle.ForeColor前景色 ASP.NETGridView点击行触发SelectedIn
2、dexChanged事件1、在指令中添加 EnableEventValidation=false2、在RowDataBound事件中添加protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)switch (e.Row.RowType) case DataControlRowType.DataRow: e.Row.Attributesstyle = cursor:hand; #region /点击行触发SelectedIndexChanged事件 PostBackOptions myPostBack
3、Options = new PostBackOptions(this); myPostBackOptions.AutoPostBack = false; myPostBackOptions.PerformValidation = false; myPostBackOptions.RequiresJavaScriptProtocol = true; /加入javascript:头 String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, Select$ + e.Row.RowIndex.ToStri
4、ng(); e.Row.Attributes.Add(onclick, evt); #endregion break; 3、在第一二步之后就可以在SelectedIndexChanging和SelectedIndexChanged事件中添加处理逻辑了。gridview72般绝技1.GridView无代码分页排序:效果图:1.AllowSorting设为True,aspx代码中是AllowSorting=True;2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize=12。3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSo
5、rting为True即可。2.GridView选中,编辑,取消,删除:效果图:后台代码:你可以使用sqlhelper,本文没用。代码如下: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;usin
6、g System.Data.SqlClient;public partial class _Default : System.Web.UI.Page/清清月儿SqlConnection sqlcon;SqlCommand sqlcom;string strCon = Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码;protected void Page_Load(object sender, EventArgs e)if (!IsPostBack)bind();protected voidGridView1_RowEditing(object s
7、ender, GridViewEditEventArgs e)GridView1.EditIndex = e.NewEditIndex;bind();/删除protected voidGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)string sqlstr = delete from 表 where id= +GridView1.DataKeyse.RowIndex.Value.ToString() + ;sqlcon = new SqlConnection(strCon);sqlcom = new SqlComm
8、and(sqlstr,sqlcon);sqlcon.Open();sqlcom.ExecuteNonQuery();sqlcon.Close();bind();/更新protected voidGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)sqlcon = new SqlConnection(strCon);string sqlstr = update 表 set 字段1=+ (TextBox)(GridView1.Rowse.RowIndex.Cells1.Controls0).Text.ToString().T
9、rim() + ,字段2=+ (TextBox)(GridView1.Rowse.RowIndex.Cells2.Controls0).Text.ToString().Trim() + ,字段3=+ (TextBox)(GridView1.Rowse.RowIndex.Cells3.Controls0).Text.ToString().Trim() + where id=+GridView1.DataKeyse.RowIndex.Value.ToString() + ;sqlcom=new SqlCommand(sqlstr,sqlcon);sqlcon.Open();sqlcom.Execu
10、teNonQuery();sqlcon.Close();GridView1.EditIndex = -1;bind();/取消protected voidGridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)GridView1.EditIndex = -1;bind();/绑定public void bind()string sqlstr = select * from 表;sqlcon = new SqlConnection(strCon);SqlDataAdapter myda = new SqlDa
11、taAdapter(sqlstr, sqlcon);DataSet myds = new DataSet();sqlcon.Open();myda.Fill(myds, 表);GridView1.DataSource = myds;GridView1.DataKeyNames = new string id ;/主键GridView1.DataBind();sqlcon.Close();前台主要代码:. .3.GridView正反双向排序:效果图:点姓名各2次的排序,点其他也一样可以。后台代码:using System;using System.Data;using System.Config
12、uration;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 Default3 : System.Web.UI.Page/清清月儿的博客SqlConnectio
13、n sqlcon;string strCon = Data Source=(local);Database=北风贸易;Uid=sa;Pwd=;protected void Page_Load(object sender, EventArgs e)if (!IsPostBack)ViewStateSortOrder = 身份证号码;ViewStateOrderDire = ASC;bind();protected voidGridView1_Sorting(object sender, GridViewSortEventArgs e)string sPage = e.SortExpression
14、;if (ViewStateSortOrder.ToString() = sPage)if (ViewStateOrderDire.ToString() = Desc)ViewStateOrderDire = ASC;elseViewStateOrderDire = Desc;elseViewStateSortOrder = e.SortExpression;bind();public void bind()string sqlstr = select top 5 * from 飞狐工作室;sqlcon = new SqlConnection(strCon);SqlDataAdapter my
15、da = new SqlDataAdapter(sqlstr, sqlcon);DataSet myds = new DataSet();sqlcon.Open();myda.Fill(myds, 飞狐工作室);DataView view = myds.Tables飞狐工作室.DefaultView;string sort = (string)ViewStateSortOrder + + (string)ViewStateOrderDire;view.Sort = sort;GridView1.DataSource = view;GridView1.DataBind();sqlcon.Clos
16、e();前台主要代码:4.GridView和下拉菜单DropDownList结合:效果图:后台代码: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.HtmlControl
17、s;using System.Data.SqlClient;public partial class Default4 : System.Web.UI.PageSqlConnection sqlcon;string strCon = Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa;protected void Page_Load(object sender, EventArgs e)DropDownList ddl;if (!IsPostBack)string sqlstr = select top 5 * from 飞狐工作室;sqlcon =
18、 new SqlConnection(strCon);SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);DataSet myds = new DataSet();sqlcon.Open();myda.Fill(myds, 飞狐工作室);GridView1.DataSource = myds;GridView1.DataBind();for (int i = 0; i =GridView1.Rows.Count - 1; i+)DataRowView mydrv = myds.Tables飞狐工作室.DefaultViewi;if
19、(Convert.ToString(mydrv员工性别).Trim() = True)ddl = (DropDownList)GridView1.Rowsi.FindControl(DropDownList1);ddl.SelectedIndex = 0;if (Convert.ToString(mydrv员工性别).Trim() = False)ddl = (DropDownList)GridView1.Rowsi.FindControl(DropDownList1);ddl.SelectedIndex = 1;sqlcon.Close();public SqlDataReader ddlbind()string sqlstr = select distinct 员工性别 from 飞狐工作室;sqlcon = new SqlConnection(strCon);SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);sqlcon.Open();return sqlcom.ExecuteReader();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1