Datagrid分页、排序、删除代码

原创 2004年08月30日 16:11:00

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="datagrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <script>
       window.resizeTo(screen.width,screen.height-30);
   </script>
   <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server"
    HorizontalAlign="Center" DataKeyField="au_id" PageSize="3" AllowPaging="True" Width="408px"
    AutoGenerateColumns="False" AllowSorting="True">
    <Columns>
     <asp:BoundColumn DataField="au_id" SortExpression="au_id" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
     <asp:BoundColumn DataField="city" SortExpression="city" ReadOnly="True" HeaderText="城市"></asp:BoundColumn>
     <asp:TemplateColumn SortExpression="au_id" HeaderText="操作">
      <ItemTemplate>
       <asp:LinkButton runat="server" ID="delbutton" Text="删除" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
      </ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
    <PagerStyle Visible="False" PageButtonCount="6"></PagerStyle>
   </asp:datagrid><asp:label id="Label1" style="Z-INDEX: 106; LEFT: 312px; POSITION: absolute; TOP: 24px" runat="server"></asp:label><asp:linkbutton id="LBtfir" style="Z-INDEX: 105; LEFT: 72px; POSITION: absolute; TOP: 24px" runat="server">首页</asp:linkbutton><asp:linkbutton id="LBtlast" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 24px" runat="server">尾页</asp:linkbutton><asp:linkbutton id="LBtpre" style="Z-INDEX: 103; LEFT: 192px; POSITION: absolute; TOP: 24px" runat="server">上一页</asp:linkbutton><asp:linkbutton id="LBtnex" style="Z-INDEX: 102; LEFT: 120px; POSITION: absolute; TOP: 24px" runat="server">下一页</asp:linkbutton></form>
 </body>
</HTML>
代码

default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace datagrid
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.LinkButton LBtnex;
  protected System.Web.UI.WebControls.LinkButton LBtpre;
  protected System.Web.UI.WebControls.LinkButton LBtlast;
  protected System.Web.UI.WebControls.LinkButton LBtfir;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  public string SortField;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面     
   if(!Page.IsPostBack)
   {
   databind();
   }  
  }
  public void databind()
  {
  SqlConnection con = new SqlConnection("server=it003;database=pubs;uid=sa;pwd=");
  con.Open();  
  string sql="select * from authors";
  SqlDataAdapter ada = new SqlDataAdapter(sql,con);
  DataSet ds = new DataSet();
  ada.Fill(ds,"temp");
  ds.Tables["temp"].DefaultView.Sort=SortField;
  DataGrid1.DataSource=ds.Tables["temp"].DefaultView;
  DataGrid1.DataBind();
  ada.Dispose();
  con.Close();
  Label1.Text="共"+ds.Tables["temp"].DefaultView.Count.ToString()+"条记录,"+DataGrid1.PageCount.ToString()+"页,第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页,本页"+DataGrid1.Items.Count.ToString()+"条记录";
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand_1);
   this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound_1);
   this.LBtfir.Click += new System.EventHandler(this.LBtfir_Click);
   this.LBtlast.Click += new System.EventHandler(this.LBtlast_Click);
   this.LBtpre.Click += new System.EventHandler(this.LBtpre_Click);
   this.LBtnex.Click += new System.EventHandler(this.LBtnex_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   DataGrid1.CurrentPageIndex=e.NewPageIndex;
   databind();
  }

  private void LBtnex_Click(object sender, System.EventArgs e)
  {
   if(DataGrid1.CurrentPageIndex<DataGrid1.PageCount-1)
   {
    DataGrid1.CurrentPageIndex++;
    databind();
   }
  }

  private void LBtpre_Click(object sender, System.EventArgs e)
  {
   if(DataGrid1.CurrentPageIndex>0)
   {
    DataGrid1.CurrentPageIndex--;
    databind();
   }
  }

  private void LBtfir_Click(object sender, System.EventArgs e)
  {
   DataGrid1.CurrentPageIndex=0;
   databind();
  }

  private void LBtlast_Click(object sender, System.EventArgs e)
  {
   DataGrid1.CurrentPageIndex=DataGrid1.PageCount-1;
   databind();
  }

  private void DataGrid1_DeleteCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   try{
   
    string ID = (string)DataGrid1.DataKeys[(int) e.Item.ItemIndex];  
    string sql="delete from authors where au_id='"+Convert.ToString(ID)+"'";
    SqlConnection con = new SqlConnection("server=it003;database=pubs;uid=sa;pwd=");
    con.Open();
    SqlCommand com=new SqlCommand(sql,con);
    com.ExecuteNonQuery();
    com.Dispose();
    con.Close();
    if (DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0)
    {
     DataGrid1.CurrentPageIndex--;
    }
    databind();
   }
   catch(Exception ex)
   {
    Response.Write("<script>alert('"+ex.Message+"')</script>");
   //Page.RegisterStartupScript("","<script>alert('"+ex.Message+"')</script>");
   
   }
   
  }

  private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if(e.Item.ItemIndex<0) return;   
   e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='cccccc'");
   e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
      LinkButton lbtnDel = (LinkButton)e.Item.FindControl("delbutton");
   string ID = (string)DataGrid1.DataKeys[(int) e.Item.ItemIndex];
   lbtnDel.Attributes.Add("onclick", "return confirm('您真的要删除"+ID+ " 行吗?');");
   
  }
  private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
  {
  SortField=(string)e.SortExpression;
  databind();
  }  
 }
}

easyui datagrid本地分页排序

html代码 加载数据 js代码 var listData = []; $(function(){ $('#dg').datagr
  • lnkToKing
  • lnkToKing
  • 2016-11-18 10:45:22
  • 2011

easyui datagrid 客户端搜索、分页、排序

easyui datagrid的排序默认是服务器端排序,可以用sorter实现客户端排序[2];客户端分页可用filter实现[3];客户端搜索同样可以用filter实现。...
  • junglezax
  • junglezax
  • 2014-06-26 14:22:56
  • 2343

让Asp.NET的DataGrid可排序、可选择、可分页

   让Asp.NET的DataGrid可排序、可选择、可分页       DataGrid是Asp.NET中的一个重要的控件,经常我们都将DataGrid做成可分页的和可排序的,有时还需要加上选择功...
  • lihonggen0
  • lihonggen0
  • 2004-05-17 17:02:00
  • 4164

DataGrid常用三种方法:分页,排序,后绑定 (转)

当DataGrid数据绑定之后将数据存入Cache;dgList.DataSource = ds;                this.Cache["DataSet"] = ds;        ...
  • luckisok
  • luckisok
  • 2007-04-25 00:10:00
  • 884

EasyUI Datagrid 分页和排序

datagrid分页     只需后台获取分页控件自动提交的两个参数每页显示的记录数rows和当前第几页page,然后读取相应页数的记录,和总记录数total一块返回即可,注意返回值要为Json。例如...
  • u012841509
  • u012841509
  • 2013-12-30 13:01:35
  • 1387

通用的DataGrid操作

  • 2008年01月03日 16:38
  • 8KB
  • 下载

jquery easyui datagrid使用,分页、排序、查询

$('#dg').datagrid({                 url: "xxx.ashx",                 pagination: true,              ...
  • cafuc229
  • cafuc229
  • 2013-11-14 20:27:00
  • 17727

DataGrid在分页状态下删除纪录的问题

在使用DataGrid分页的时候,正常情况下,绑定数据库列表纪录时会自动产生分页的效果,然而我发觉在删除纪录的时候总会发生"无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 P...
  • 21aspnet
  • 21aspnet
  • 2004-10-24 23:54:00
  • 1411

ASP.NET中使用DataGrid同时实现分页与排序

问题:在DataGrid中实现分页、排序是十分容易的相关核心代码:分页代码:(必需在DataGrid中设置允许分页)如果仅实现分页下面代码仅需要:this.DataGrid1.CurrentPageI...
  • liucfy
  • liucfy
  • 2008-03-25 11:10:00
  • 369

easuyi datagrid列排序出现排序错乱怎么办

自带的排序功能属于自负排序,有时候会出现9排序在25前面的情况 解决办法 自定义一个排序方法即可 function sort_int(a,b){ if(a.length > b....
  • u014572906
  • u014572906
  • 2016-08-05 11:09:27
  • 221
收藏助手
不良信息举报
您举报文章:Datagrid分页、排序、删除代码
举报原因:
原因补充:

(最多只允许输入30个字)