您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > Gridview控件的使用详解
第四讲GridView72般绝技作者:清清月儿主页:时间:2007.3.24-26为了写本文,又搭工又搭料,累的2天2宿没睡觉,这篇文章实在是耗费我太多的心血,版权永远属于CSDN社区的清清月儿,请不要试图修改本文,破坏全文的完整性,希望您尊重我的劳动成果!目录1.GridView无代码分页排序:.......................................................................................12.GridView选中,编辑,取消,删除:.......................................................................23.GridView正反双向排序:...........................................................................................54.GridView和下拉菜单DropDownList结合:.............................................................85.GridView和CheckBox结合:..................................................................................106.鼠标移到GridView某一行时改变该行的背景色方法一:....................................137.鼠标移到GridView某一行时改变该行的背景色方法二:....................................158.GridView实现删除时弹出确认对话框:.................................................................179.GridView实现自动编号:.........................................................................................1710.GridView实现自定义时间货币等字符串格式:...................................................1911.GridView实现用“...”代替超长字符串:.................................................................2212.GridView一般换行与强制换行:...........................................................................2613.GridView显示隐藏某一列:...................................................................................2714.GridView弹出新页面/弹出制定大小位置新窗口:..............................................3215.GridView固定表头(不用javascript只用CSS!,很好用):..................................3416.GridView合并表头多重表头无错完美版(以合并3列3行举例)...................3517.GridView突出显示某一单元格(例如金额低于多少,分数不及格等)...........4018.GridView加入自动求和求平均值小计...................................................................4419.GridView数据导入Excel/Excel数据读入GridView.............................................471.GridView无代码分页排序:效果图:1.AllowSorting设为True,aspx代码中是AllowSorting=True;2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize=12。3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。2.GridView选中,编辑,取消,删除:效果图:后台代码:你可以使用sqlhelper,本文没用。代码如下:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;publicpartialclass_Default:System.Web.UI.Page{//清清月儿=DataSource=(local);Database=数据库名;Uid=帐号;Pwd=密码;protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){bind();}}protectedvoidGridView1_RowEditing(objectsender,GridViewEditEventArgse){GridView1.EditIndex=e.NewEditIndex;bind();}//删除protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse){stringsqlstr=deletefrom表whereid='+GridView1.DataKeys[e.RowIndex].Value.ToString()+';sqlcon=newSqlConnection(strCon);sqlcom=newSqlCommand(sqlstr,sqlcon);sqlcon.Open();sqlcom.ExecuteNonQuery();sqlcon.Close();bind();}//更新protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){sqlcon=newSqlConnection(strCon);stringsqlstr=update表set字段1='+((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()+',字段2='+((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()+',字段3='+((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()+'whereid='+GridView1.DataKeys[e.RowIndex].Value.ToString()+';sqlcom=newSqlCommand(sqlstr,sqlcon);sqlcon.Open();sqlcom.ExecuteNonQuery();sqlcon.Close();GridView1.EditIndex=-1;bind();}//取消protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse){GridView1.EditIndex=-1;bind();}//绑定publicvoidbind(){stringsqlstr=select*from表;sqlcon=newSqlConnection(strCon);SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon);DataSetmyds=newDataSet();sqlcon.Open();myda.Fill(myds,表);GridView1.DataSource=myds;GridView1.DataKeyNames=newstring[]{id};//主键GridView1.DataBind();sqlcon.Close();}}前台主要代码:......asp:GridViewID=GridView1runat=serverAutoGenerateColumns=FalseCellPadding=4ForeColor=#333333GridLines=NoneOnRowDeleting=GridView1_RowDeletingOnRowEditing=GridView1_RowEditingOnRowUpdating=GridView1_RowUpdatingOnRowCancelingEdit=GridView1_RowCancelingEditFooterStyleBackColor=#990000Font-Bold=TrueForeColor=White/Columnsasp:BoundFieldDataField=身份证号码HeaderText=用户IDReadOnly=True/asp:BoundFieldDataField=姓名HeaderText=用户姓名/asp:BoundFieldDataField=员工性别HeaderText=性别/asp:BoundFieldDataField=家庭住址HeaderText=家庭住址/asp:CommandFieldHeaderText=选择ShowSelectButton=True/asp:CommandFieldHeaderText=编辑ShowEditButton=True/asp:CommandFieldHeaderText=删除ShowDeleteButton=True//ColumnsRowStyleForeColor=#000066/SelectedRowStyleBackColor=#669999Font-Bold=TrueForeColor=White/PagerStyleBackColor=WhiteForeColor=#000066HorizontalAlign=Left/HeaderStyleBackColor=#006699Font-Bold=TrueForeColor=White//asp:GridView3.GridView正反双向排序:后台代码:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usin
本文标题:Gridview控件的使用详解
链接地址:https://www.777doc.com/doc-4897929 .html