aspnet gridview.docx

上传人:b****2 文档编号:23128786 上传时间:2023-05-08 格式:DOCX 页数:11 大小:56.93KB
下载 相关 举报
aspnet gridview.docx_第1页
第1页 / 共11页
aspnet gridview.docx_第2页
第2页 / 共11页
aspnet gridview.docx_第3页
第3页 / 共11页
aspnet gridview.docx_第4页
第4页 / 共11页
aspnet gridview.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

aspnet gridview.docx

《aspnet gridview.docx》由会员分享,可在线阅读,更多相关《aspnet gridview.docx(11页珍藏版)》请在冰豆网上搜索。

aspnet gridview.docx

aspnetgridview

目录

[ASP.NET]GridView点击行触发SelectedIndexChanged事件1

 gridview 72般绝技3

1.GridView无代码分页排序:

3

2.GridView选中,编辑,取消,删除:

4

3.GridView正反双向排序:

10

4.GridView和下拉菜单DropDownList结合:

15

datagridview.Rows[i].DefaultCellStyle.BackColor背景色

datagridview.Rows[i].DefaultCellStyle.ForeColor前景色

[ASP.NET]GridView点击行触发SelectedIndexChanged事件

1、在<%@Page ...... %>指令中添加EnableEventValidation="false"

 2、在RowDataBound事件中添加

protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)

{      

        switch(e.Row.RowType)

       {

           caseDataControlRowType.DataRow:

               e.Row.Attributes["style"]="cursor:

hand";

               #region  //点击行触发SelectedIndexChanged事件

               PostBackOptionsmyPostBackOptions=newPostBackOptions(this);

               myPostBackOptions.AutoPostBack=false;

               myPostBackOptions.PerformValidation=false;

               myPostBackOptions.RequiresJavaScriptProtocol=true;//加入javascript:

               Stringevt=Page.ClientScript.GetPostBackClientHyperlink(senderasGridView,"Select$"+e.Row.RowIndex.ToString());

               e.Row.Attributes.Add("onclick",evt);

               #endregion

               break;

       }

   }

  3、在第一二步之后就可以在SelectedIndexChanging和SelectedIndexChanged事件中添加处理逻辑了。

 gridview 72般绝技

1.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 

{

//清清月儿 

SqlConnectionsqlcon;

SqlCommandsqlcom;

stringstrCon="DataSource=(local);Database=数据库名;Uid=帐号;Pwd=密码";

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

bind();

}

}

protectedvoid GridView1_RowEditing(objectsender,GridViewEditEventArgse)

{

GridView1.EditIndex=e.NewEditIndex;

bind();

}

//删除

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

}

//更新

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

}

//取消

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

}

}

前台主要代码:

......

GridView ID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="4"

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

OnRowUpdating="GridView1_RowUpdating"OnRowCancelingEdit="GridView1_RowCancelingEdit">

BoundFieldDataField="身份证号码"HeaderText="用户ID"ReadOnly="True"/>

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

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

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

CommandFieldHeaderText="选择"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"]="身份证号码";

ViewState["OrderDire"]="ASC";

bind();

}

}

protectedvoid GridView1_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飞狐工作室";

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

}

}

前台主要代码:

GridView ID="GridView1"runat="server"AllowSorting="True"AutoGenerateColumns="False"

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

BoundFieldDataField="身份证号码"HeaderText="用户ID"SortExpression="身份证号码"/>

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

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

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

GridView>

4.GridView和下拉菜单DropDownList结合:

效果图:

后台代码:

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;

publicpartialclassDefault4:

System.Web.UI.Page

{

SqlConnectionsqlcon;

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

protectedvoidPage_Load(objectsender,EventArgse)

{

DropDownListddl;

if(!

IsPostBack)

{

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

sqlcon=newSqlConnection(strCon);

SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon);

DataSetmyds=newDataSet();

sqlcon.Open();

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

GridView1.DataSource=myds;

GridView1.DataBind();

for(inti=0;i<= GridView1.Rows.Count-1;i++)

{

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

if(Convert.ToString(mydrv["员工性别"]).Trim()=="True")

{

ddl=(DropDownList)GridView1.Rows[i].FindControl("DropDownList1");

ddl.SelectedIndex=0;

}

if(Convert.ToString(mydrv["员工性别"]).Trim()=="False")

{

ddl=(DropDownList)GridView1.Rows[i].FindControl("DropDownList1");

ddl.SelectedIndex=1;

}

}

sqlcon.Close();

}

}

publicSqlDataReaderddlbind()

{

stringsqlstr="selectdistinct员工性别from飞狐工作室";

sqlcon=newSqlConnection(strCon);

SqlCommandsqlcom=newSqlCommand(sqlstr,sqlcon);

sqlcon.Open();

returnsqlcom.ExecuteReader();

}

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

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1