1.GridView代码分页排序: 效果图:
1.AllowSorting设为True,aspx代码中是AllowSorting=\;
2.默认1页10条,如果要修改每页条数,修改PageSize即可,在aspx代码中是PageSize=\。
3.默认的是单向排序的,右击GridView弹出“属性”,选择AllowSorting为True即可。
1:分页保存选中状态 保存CheckBox的值
GridView在分页过程中并不维护CheckBox的选择状态,幸运的是,我们可以使用Session来维护CheckBox的状态,这个功能使用RememberOldValues完成 private void RememberOldValues() {
ArrayList categoryIDList = new ArrayList(); int index = -1;
foreach (GridViewRow row in okZMGV.Rows) {
index =Convert .ToInt32( okZMGV.DataKeys[row.RowIndex].Value); bool result = ((CheckBox)row.FindControl(\)).Checked;
// Check in the Session if (Session[\] != null)
categoryIDList = (ArrayList)Session[\]; if (result) {
if (!categoryIDList.Contains(index)) categoryIDList.Add(index); } else
categoryIDList.Remove(index); }
if (categoryIDList != null && categoryIDList.Count > 0) Session[\] = categoryIDList;
}
还原CheckBox的状态
下一步,需要定义一个方法来还原Checkbox的状态值
private void RePopulateValues() {
ArrayList categoryIDList = (ArrayList)Session[\]; if (categoryIDList != null && categoryIDList.Count > 0) {
foreach (GridViewRow row in okZMGV .Rows) {
int index = (int)okZMGV.DataKeys[row.RowIndex].Value; if (categoryIDList.Contains(index)) {
CheckBox myCheckBox = (CheckBox)row.FindControl(\); myCheckBox.Checked = true; } } } }
最后,在分页事件里调用上面两个方法
protected void page_Click(object sender, ImageClickEventArgs e) {
RememberOldValues();
string count = ((ImageButton)sender).CommandArgument.ToString().ToLower (); switch(count) {
case \:
if (okZMGV.PageIndex > 0) {
okZMGV.PageIndex -= 1; } break; case \:
if (okZMGV.PageIndex < okZMGV.PageCount - 1) {
okZMGV.PageIndex += 1; } break; case \:
okZMGV.PageIndex=0; break; case \:
okZMGV.PageIndex= okZMGV.pagecount-1; break; }
BrndOKBind(); RePopulateValues(); }
2.GridView选中,编辑,取消,删除: 效果图:
后台代码:
你可以使用sqlhelper,本文没用。代码如下: using System; using System.Data;
using System.Configuration; using System.Web;
using System.Web.Security; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
//清清月儿http://blog.csdn.net/21aspnet SqlConnection sqlcon; SqlCommand sqlcom;
string strCon = \数据库名;Uid=帐号;Pwd=密码\ protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
bind(); }
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
GridView1.EditIndex = e.NewEditIndex; bind(); } //删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
string sqlstr = \表 where id=''\GridView1.DataKeys[e.RowIndex].Value.ToString() + \ sqlcon = new SqlConnection(strCon); sqlcom = new SqlCommand(sqlstr,sqlcon); sqlcon.Open();
sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); } //更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
sqlcon = new SqlConnection(strCon); string sqlstr = \表 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() + \
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + \ sqlcom=new SqlCommand(sqlstr,sqlcon); sqlcon.Open();
sqlcom.ExecuteNonQuery(); sqlcon.Close();
GridView1.EditIndex = -1; bind(); } //取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
GridView1.EditIndex = -1; bind(); } //绑定
public void bind() {
string sqlstr = \表\ sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, \表\
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { \主键 GridView1.DataBind(); sqlcon.Close(); } }
前台主要代码:
... ...
ForeColor=\OnRowDeleting=\OnRowEditing=\
OnRowUpdating=\OnRowCancelingEdit=\