清清月儿之GridView 72般绝技.docx
《清清月儿之GridView 72般绝技.docx》由会员分享,可在线阅读,更多相关《清清月儿之GridView 72般绝技.docx(75页珍藏版)》请在冰豆网上搜索。
![清清月儿之GridView 72般绝技.docx](https://file1.bdocx.com/fileroot1/2023-8/4/6b4c8fb7-9c60-4e57-960f-43cdddb83540/6b4c8fb7-9c60-4e57-960f-43cdddb835401.gif)
清清月儿之GridView72般绝技
第四讲GridView72般绝技
作者:
清清月儿
主页:
时间:
2007.3.24-26
为了写本文,又搭工又搭料,累的2天2宿没睡觉,这篇文章实在是耗费我太多的心血,版权永远属于CSDN社区的清清月儿,请不要试图修改本文,破坏全文的完整性,希望您尊重我的劳动成果!
目录
1.GridView无代码分页排序:
2
2.GridView选中,编辑,取消,删除:
2
3.GridView正反双向排序:
6
4.GridView和下拉菜单DropDownList结合:
8
5.GridView和CheckBox结合:
10
6.鼠标移到GridView某一行时改变该行的背景色方法一:
14
7.鼠标移到GridView某一行时改变该行的背景色方法二:
16
8.GridView实现删除时弹出确认对话框:
17
9.GridView实现自动编号:
17
10.GridView实现自定义时间货币等字符串格式:
19
11.GridView实现用“...”代替超长字符串:
22
12.GridView一般换行与强制换行:
26
13.GridView显示隐藏某一列:
27
14.GridView弹出新页面/弹出制定大小位置新窗口:
32
15.GridView固定表头(不用javascript只用CSS!
,很好用):
34
16.GridView合并表头多重表头无错完美版(以合并3列3行举例)35
17.GridView突出显示某一单元格(例如金额低于多少,分数不及格等)40
18.GridView加入自动求和求平均值小计44
19.GridView数据导入Excel/Excel数据读入GridView47
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();
}
}
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();
}
}
前台主要代码:
......
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="身份证号码"HeaderText="用户ID"ReadOnly="True"/>
BoundFieldDataField="姓名"HeaderText="用户姓名"/>
BoundFieldDataField="员工性别"HeaderText="性别"/>
BoundFieldDataField="家庭住址"HeaderText="家庭住址"/>
CommandFieldHeaderText="选择"ShowSelectButton="True"/>
CommandFieldHeaderText="编辑"ShowEditButton="True"/>
CommandFieldHeaderText="删除"ShowDeleteButton="True"/>
GridView>
3.GridView正反双向排序:
后台代码:
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();
}
}
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飞狐工作室";
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();
}
}
前台主要代码:
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="身份证号码"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();
}
前台主要代码:
GridViewID="GridView1"runat="server"AllowSorting="True"AutoGenerateColumns="False"
CellPadding="3"Font-Size="9pt"BackColor="White"BorderColor="#CCCCCC"BorderStyle="None"BorderWidth="1px">
BoundFieldDataField="身份证号码"HeaderText="用户ID"SortExpression="身份证号码"/>
BoundFieldDataField="姓名"HeaderText="用户姓名"SortExpression="姓名"/>
TemplateFieldHeaderText="员工性别">
DropDownListID="DropDownList1"runat="server"DataSource='<%#ddlbind()%>'DataValueField="员工性别"DataTextField="员工性别">
DropDownList>
TemplateField>
BoundFieldDataField="家庭住址"HeaderText="家庭住址"SortExpression="家庭住址"/>
GridView>
5.GridView和CheckBox结合:
效果图:
后台代码:
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;
publicpartialclassDefault5:
System.Web.UI.Page
{
//清清月儿
SqlConnectionsqlcon;
stringstrCon="DataSource=(local);Dat