GridView 72般绝技Word下载.docx
《GridView 72般绝技Word下载.docx》由会员分享,可在线阅读,更多相关《GridView 72般绝技Word下载.docx(53页珍藏版)》请在冰豆网上搜索。
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即可。
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;
//删除
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();
//更新
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse)
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='
sqlcom=newSqlCommand(sqlstr,sqlcon);
GridView1.EditIndex=-1;
//取消
protectedvoidGridView1_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse)
//绑定
publicvoidbind()
select*from表"
SqlDataAdaptermyda=newSqlDataAdapter(sqlstr,sqlcon);
DataSetmyds=newDataSet();
myda.Fill(myds,"
表"
);
GridView1.DataSource=myds;
GridView1.DataKeyNames=newstring[]{"
id"
};
//主键
GridView1.DataBind();
}
前台主要代码:
......
<
asp:
GridViewID="
GridView1"
runat="
server"
AutoGenerateColumns="
False"
CellPadding="
4"
ForeColor="
#333333"
GridLines="
None"
OnRowDeleting="
GridView1_RowDeleting"
OnRowEditing="
GridView1_RowEditing"
OnRowUpdating="
GridView1_RowUpdating"
OnRowCancelingEdit="
GridView1_RowCancelingEdit"
>
<
FooterStyleBackColor="
#990000"
Font-Bold="
White"
/>
Columns>
BoundFieldDataField="
身份证号码"
HeaderText="
用户ID"
ReadOnly="
姓名"
用户姓名"
员工性别"
性别"
家庭住址"
CommandFieldHeaderText="
选择"
ShowSelectButton="
编辑"
ShowEditButton="
删除"
ShowDeleteButton="
/Columns>
RowStyleForeColor="
#000066"
SelectedRowStyleBackColor="
#669999"
PagerStyleBackColor="
HorizontalAlign="
Left"
HeaderStyleBackColor="
#006699"
/asp:
GridView>
3.GridView正反双向排序:
点姓名各2次的排序,点其他也一样可以。
usingSystem.Collections;
publicpartialclassDefault3:
System.Web.UI.Page
//清清月儿的博客
Database=北风贸易;
Uid=sa;
Pwd="
ViewState["
SortOrder"
]="
OrderDire"
ASC"
protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)
stringsPage=e.SortExpression;
if(ViewState["
].ToString()==sPage)
].ToString()=="
Desc"
)
else
]=e.SortExpression;
selecttop5*from飞狐工作室"
飞狐工作室"
DataViewview=myds.Tables["
].DefaultView;
stringsort=(string)ViewState["
]+"
"
+(string)ViewState["
];
view.Sort=sort;
GridView1.DataSource=view;
AllowSorting="
3"
Font-Size="
9pt"
OnSorting="
GridView1_Sorting"
BackColor="
BorderColor="
#CCCCCC"
BorderStyle="
BorderWidth="
1px"
SortExpression="
/>
4.GridView和下拉菜单DropDownList结合:
publicpartialclassDefault4:
Pwd=sa"
DropDownListddl;
for(inti=0;
i<
=GridView1.Rows.Count-1;
i++)
DataRowViewmydrv=myds.Tables["
].DefaultView[i];
if(Convert.ToString(mydrv["
]).Trim()=="
ddl=(DropDownList)GridView1.Rows[i].FindControl("
DropDownList1"
ddl.SelectedIndex=0;
ddl.SelectedIndex=1;
publicSqlDataReaderddlbind()
selectdistinct员工性别from飞狐工作室"
SqlCommandsqlcom=newSqlCommand(sqlstr,sqlcon);
returnsqlcom.ExecuteReader();
TemplateFieldHeaderText="
ItemTemplate>
DropDownListID="
DataSource='
%#ddlbind()%>
DataValueField="
DataTextField="
DropDownList>
/ItemTemplate>
TemplateField>
5.GridView和CheckBox结合:
publicpartialclassDefault5:
IsPostBa