gridview运用

存储过程:

ALTER PROCEDURE [dbo].[procarticle]
-- Add the parameters for the stored procedure here
@pageindex int,
@pagesize int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
select * from(select ROW_NUMBER() over(order by ARTICLEID)as rownumb,* from ArticleInfo)t
left outer join ArticleClass c on t.CLASSID=c.CLASSID
where t.rownumb>(@pageindex-1)*@pagesize and
t.rownumb<=@pageindex*@pagesize
END

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace 上传
{
public partial class WebForm4 : System.Web.UI.Page
{
int pagesize = 5;
int totalsize = 1;

protected void Page_Load(object sender, EventArgs e)
{
ViewState["pagindex"] = "1";
if (!IsPostBack)
{
GetData(Convert.ToInt32(ViewState["pagindex"]));
}

}
//显示字数
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//数据行
{
string Str = e.Row.Cells[3].Text; //第二列内容
if (Str.Length > 20) //第二列内容大于20个
{
e.Row.Cells[3].Text = Str.Substring(0, 20) + "..."; //截取20个显示,其他用“...”号代替
e.Row.Cells[3].ToolTip = Str;//鼠标放上去显示全部信息
}
}

}

//数据库
public void GetData(int pageindex)
{
string strcon = @"data source=LENOVO-PC\SQLEXPRESS;database=YYSYD.MDF;Integrated Security=true";
//data source=LENOVO-PC\SQLEXPRESS;database=YYSYD.MDF;Integrated Security=true

SqlConnection conn = new SqlConnection(strcon);
SqlCommand comm = conn.CreateCommand();
comm.Connection = conn;
conn.Open();
comm.CommandText = "prArticleInfo";
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("@pageindex", pageindex);
comm.Parameters.AddWithValue("@pagesize", pagesize);
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(comm);
adapter.Fill(dt);
comm.ExecuteNonQuery();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
comm.Dispose();
conn.Dispose();

}
//全选
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox ckb = sender as CheckBox;
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
(row.Cells[1].FindControl("CheckBox1") as CheckBox).Checked = ckb.Checked;
}
}
}
//删除
protected void btndelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{

if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckb = row.Cells[1].FindControl("CheckBox1") as CheckBox;
if (ckb.Checked)
{
int a = Convert.ToInt32(row.Cells[0].Text);
string strcon = @"data source=LENOVO-PC\SQLEXPRESS;database=YYSYD.MDF;Integrated Security=true";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
conn.Open();
comm.CommandText = "delete from ArticleInfo where ARTICLEID=@id";
comm.Parameters.AddWithValue("@id",a);
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
}
}
}
}
//获得数量
private int GetCount()
{
SqlConnection conn = new SqlConnection(@"data source=LENOVO-PC\SQLEXPRESS;database=YYSYD.MDF;Integrated Security=true");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "select count(*) from ArticleInfo";
int count = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
conn.Close();
conn.Dispose();
if (count % pagesize != 0)
{
return totalsize = count / pagesize + 1;
}
else
{
return totalsize = count / pagesize;
}
}
//首页
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = "1";
GetData(Convert.ToInt32(ViewState["pageindex"]));
}
//上一页
protected void Button2_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32( ViewState["pageindex"]);
if (pageindex > 1)
{
pageindex--;
}
ViewState["pageindex"] = pageindex.ToString();
GetData(pageindex);
}
//下一页
protected void Button3_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
int count=GetCount();
if (pageindex <count )
{
pageindex++;
}
ViewState["pageindex"] = count.ToString();
GetData(pageindex);
}
//末页
protected void Button4_Click(object sender, EventArgs e)
{
int count = GetCount();
ViewState["pageindex"] = count.ToString();
GetData(count);
}
}
}

前台:

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="上传.WebForm4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style=" width:500px; width:532px;">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Xonrowdatabound="GridView1_RowDataBound" style="text-align: center">

<Columns>
<asp:BoundField DataField="ARTICLEID" HeaderText="文章编号" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server"
Xoncheckedchanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TITLE" HeaderText="标题" />
<asp:BoundField DataField="CONTENT" HeaderText="内容" />
<asp:BoundField DataField="CLICKCOUNT" HeaderText="点击量" />
<asp:BoundField DataField="PUBLISHER" HeaderText="作者" />
</Columns>


</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="首页" Xοnclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="上一页" Xοnclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="下一页" Xοnclick="Button3_Click"
style="height: 21px" />
<asp:Button ID="Button4" runat="server" Text="末页" Xοnclick="Button4_Click" />
<asp:Button ID="btndelete" runat="server" Xοnclick="btndelete_Click" Text="删除"
style="height: 21px" />

</div>
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值