史上最全GridView使用方法Word文档格式.docx
《史上最全GridView使用方法Word文档格式.docx》由会员分享,可在线阅读,更多相关《史上最全GridView使用方法Word文档格式.docx(64页珍藏版)》请在冰豆网上搜索。
给Gridview加入Tooltip的功能
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正反双向排序:
(用到数据库视图DATAVIEW及GRIDVIEW的VIEWSTAT类)
点姓名各2次的排序,点其他也一样可以。
usingSystem.Collections;
publicpartialclassDefault3:
System.Web.UI.Page
//清清月儿的博客
Database=北风贸易;
Uid=sa;
Pwd="
//viewstate:
服务器控件的视图状态为其所有属性值的累计。
为了在HTTP请求间保留这些值,ASP.NET服//务器控件使用该属性(它是StateBag类的实例)来存储属性值。
在处理后续请求时,该值随//即作为变量传递给HTML隐藏输入元素。
ViewState["
SortOrder"
]="
OrderDire"
ASC"
protectedvoidGridView1_Sorting(objectsender,GridViewSortEventArgse)
stringsPage=e.SortExpression;
if(ViewState["
].ToString()==sPage)
].ToString()=="
Desc"
)
else
]=e.SortExpression;
publicvoidbind()
selecttop5*from飞狐工作室"
飞狐工作室"
//DATAVIEW类实现了视图功能,可以在视图中对得出的表进行编辑
//DATATABLE类实现了表功能,可以在表里构造我们想要的数据
DataViewview=myds.Tables["
].DefaultView;
stringsort=(string)ViewState["
]+"
"
+(string)ViewState["
];
view.Sort=sort;
//数据库中的数据可以经过视图组织之后再承现给GV
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;
DropDown