gridview使用大全.docx

上传人:b****6 文档编号:13654396 上传时间:2023-06-16 格式:DOCX 页数:68 大小:34.47KB
下载 相关 举报
gridview使用大全.docx_第1页
第1页 / 共68页
gridview使用大全.docx_第2页
第2页 / 共68页
gridview使用大全.docx_第3页
第3页 / 共68页
gridview使用大全.docx_第4页
第4页 / 共68页
gridview使用大全.docx_第5页
第5页 / 共68页
gridview使用大全.docx_第6页
第6页 / 共68页
gridview使用大全.docx_第7页
第7页 / 共68页
gridview使用大全.docx_第8页
第8页 / 共68页
gridview使用大全.docx_第9页
第9页 / 共68页
gridview使用大全.docx_第10页
第10页 / 共68页
gridview使用大全.docx_第11页
第11页 / 共68页
gridview使用大全.docx_第12页
第12页 / 共68页
gridview使用大全.docx_第13页
第13页 / 共68页
gridview使用大全.docx_第14页
第14页 / 共68页
gridview使用大全.docx_第15页
第15页 / 共68页
gridview使用大全.docx_第16页
第16页 / 共68页
gridview使用大全.docx_第17页
第17页 / 共68页
gridview使用大全.docx_第18页
第18页 / 共68页
gridview使用大全.docx_第19页
第19页 / 共68页
gridview使用大全.docx_第20页
第20页 / 共68页
亲,该文档总共68页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

gridview使用大全.docx

《gridview使用大全.docx》由会员分享,可在线阅读,更多相关《gridview使用大全.docx(68页珍藏版)》请在冰点文库上搜索。

gridview使用大全.docx

gridview使用大全

gridview使用大全

GridView代码分页保存分页CheckBox的状态

GridView选中,编辑,取消,删除

GridView正反双向排序

GridView和下拉菜单DropDownList结合

GridView和CheckBox结合

鼠标移到GridView某一行时改变该行的背景色方法一

鼠标移到GridView某一行时改变该行的背景色方法二

GridView实现删除时弹出确认对话框

GridView实现自动编号

GridView实现自定义时间货币等字符串格式

GridView实现用“...”代替超长字符串

GridView一般换行与强制换行

GridView显示隐藏某一列

GridView弹出新页面/弹出新窗口

GridView固定表头(不用javascript只用CSS,2行代码,很好用)

GridView合并表头多重表头无错完美版(以合并3列3行举例)

GridView突出显示某一单元格(例如金额低于多少,分数不及格等)

GridView加入自动求和求平均值小计

GridView数据导入Excel/Excel数据读入GridView

1.GridView代码分页排序:

效果图:

1.AllowSorting设为True,aspx代码中是AllowSorting="True";

2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize="12"。

3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。

1:

分页保存选中状态

保存CheckBox的值

GridView在分页过程中并不维护CheckBox的选择状态,幸运的是,我们可以使用Session来维护CheckBox的状态,这个功能使用RememberOldValues完成

privatevoidRememberOldValues()

{

ArrayListcatego

ryIDList=newArrayList();

       intindex=-1;

       foreach(GridViewRowrowinokZMGV.Rows)

       {

           index=Convert.ToInt32(okZMGV.DataKeys[row.RowIndex].Value);

           boolresult=((CheckBox)row.FindControl("IsCheckBox")).Checked;

 

           //CheckintheSession

           if(Session["state"]!

=null)

               categoryIDList=(ArrayList)Session["state"];

           if(result)

           {

               if(!

categoryIDList.Contains(index))

                   categoryIDList.Add(index);

           }

           else

               categoryIDList.Remove(index);

       }

       if(categoryIDList!

=null&&categoryIDList.Count>0)

           Session["state"]=categoryIDList;

}

还原CheckBox的状态

  下一步,需要定义一个方法来还原Checkbox的状态值

 

   privatevoidRePopulateValues()

   {

       ArrayListcategoryIDList=(ArrayList)Session["state"];

       if(categoryIDList!

=null&&categoryIDList.Count>0)

       {

           foreach(GridViewRowrowinokZMGV.Rows)

{

               intindex=(int)okZMGV.DataKeys[row.RowIndex].Value;

               if(categoryIDList.Contains(index))

               {

                   CheckBoxmyCheckBox=(CheckBox)row.FindControl("IsCheckBox");

                   myCheckBox.Checked=true;

               }

           }

       }

   }

 

 

 

最后,在分页事件里调用上面两个方法

protectedvoidpage_Click(objectsender,ImageClickEventArgse)

   {

       RememberOldValues();

       stringcount=((ImageButton)sender).CommandArgument.ToString().ToLower();

       switch(count)

       {

           case"prev":

               if(okZMGV.PageIndex>0)

               {

                   okZMGV.PageIndex-=1;

               }

               break;

           case"next":

               if(okZMGV.PageIndex

             &nb

sp; {

                   okZMGV.PageIndex+=1;

               }

               break;

           case"0":

                  okZMGV.PageIndex=0;

break;

case"last":

okZMGV.PageIndex=okZMGV.pagecount-1;

break;

 

       }

       BrndOKBind();

       RePopulateValues();

   }

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

{

//清清月儿 

   SqlConnectionsqlcon;

   SqlCommandsqlcom;

   stringstrCon="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,Grid

ViewUpdateEventArgse)

{

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();

}

}

前台主要代码:

......

GridViewID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="4"

ForeColor="#333333"GridLines="None"OnRowDeleting="GridView1_RowDeleting"OnRowEditing="GridView1_RowEditing"

OnRowUpdating="GridView1_RowUpdating"OnRowCancelingEdit="GridView1_RowCancelingEdit">

BoundFieldDataField="SFZ号码"HeaderText="用户ID"ReadOnly="True"/>

BoundFieldDataField="姓名"HeaderText="用户姓名"/>

BoundFieldDataField="员工性别"HeaderText="性别"/>

BoundFieldDataField="家庭住址"HeaderText="家庭住址"/>

CommandF

本文来源【学网】网站链接是

ieldHeaderText="选择"ShowSelectButton="True"/>

                           

CommandFieldHeaderText="编辑"ShowEditButton="True"/>

                           

CommandFieldHeaderText="删除"ShowDeleteButton="True"/>

                       

                       

                       

                       

                       

                   

GridView>

3.GridView正反双向排序:

效果图:

点姓名各2次的排序,点其他也一样可以。

后台代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.SqlClient;

publicpartialclassDefault3:

System.Web.UI.Page

{

//清清月儿的博客 

   SqlConnectionsqlcon;

   stringstrCon="DataSource=(local);Database=北风贸易;Uid=sa;Pwd=";

   protectedvoidPage_Load(objectsender,EventArgse)

   {

       if(!

IsPostBack)

       {

           ViewState["SortOrder"]="SFZ号码";

           ViewState["OrderDire"]="ASC";

           bind();

       }

   }

   protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)

   {

       stringsPage=e.SortExpression;

       if(ViewState["SortOrder"].ToString()==sPage)

       {

           if(ViewState["OrderDire"].ToString()=="Desc")

               ViewState["OrderDire"]="ASC";

           else

               ViewState["OrderDire"]="Desc";

       }

       else

       {

           ViewState["SortOrder"]=e.SortExpression;

       }

       bind();

   }

   publicvoidbind()

   {

       

       stringsqlstr="selecttop5*from飞狐工作室";

       sqlco

n=newSqlConnection(strCon);

       SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon);

       DataSetmyds=newDataSet();

       sqlcon.Open();

       myda.Fill(myds,"飞狐工作室");

       DataViewview=myds.Tables["飞狐工作室"].DefaultView;

       stringsort=(string)ViewState["SortOrder"]+""+(string)ViewState["OrderDire"];

       view.Sort=sort;

       GridView1.DataSource=view;

       GridView1.DataBind();

       sqlcon.Close();

   }

}

前台主要代码:

GridViewID="GridView1"runat="server"AllowSorting="True"AutoGenerateColumns="False"

                       CellPadding="3"Font-Size="9pt"OnSorting="GridView1_Sorting"BackColor="White"BorderColor="#CCCCCC"BorderStyle="None"BorderWidth="1px">

                       

                       

                            

BoundFieldDataField="SFZ号码"HeaderText="用户ID"SortExpression="SFZ号码"/>

                           

BoundFieldDataField="姓名"HeaderText="用户姓名"SortExpression="姓名"/>

                           

BoundFieldDataField="员工性别"HeaderText="性别"SortExpression="员工性别"/>

                           

BoundFieldDataField="家庭住址"HeaderText="家庭住址"SortExpression="家庭住址"/>

                               

                       

                       

                       

                       

                       

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2