GridView分页-编辑-删除
引入DB数据库:
public class DB
{
public DB()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection CreateConnection()
{
SqlConnection con = new SqlConnection("server=.;database=vote;uid=sa;pwd=1100");
return con;
}
public static SqlConnection NorthwindConnection()
{
SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=1100");
return con;
}
public static PagedDataSource pagedata ()
{
SqlConnection con = DB.NorthwindConnection();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from Customers ", con);
DataSet ds = new DataSet();
sda.Fill(ds, "Customers");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["Customers"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
return pds;
}
}
引入ASP.NET的.aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GridView_Default" %>
<!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>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
DataKeyNames="CustomerID" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" style="margin-right: 0px" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="编号" NullDisplayText="编号" >
<FooterStyle BackColor="#333399" Wrap="False" />
<HeaderStyle BackColor="#6600FF" />
<ItemStyle ForeColor="#6600FF" />
</asp:BoundField>
<asp:TemplateField HeaderText="姓名">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="城市">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="电话">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Phone") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:Panel ID="Panel1" runat="server">
当前页:
<asp:Label ID="num" runat="server" Text=""></asp:Label>
<br>
<asp:Button ID="btnup" runat="server" Text="上一页"
onclick="btnup_Click" />
<asp:Button ID="btndown" runat="server" Text="下一页"
onclick="btndown_Click" />
</asp:Panel>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="隐藏第二列"
Width="159px" />
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="隐藏第二行"
Width="159px" />
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class GridView_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.num.Text = "1";
PageCount();
}
}
private void PageCount()
{
PagedDataSource pds = DB.pagedata();
int curpage = Convert.ToInt32(num.Text);
this.btndown.Enabled = true;
this.btnup.Enabled = true;
pds.CurrentPageIndex = curpage - 1;
if (curpage == 1)
{
this.btnup.Enabled = false;
}
if (curpage == pds.PageCount)
{
this.btndown.Enabled = false;
}
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
}
protected void btnup_Click(object sender, EventArgs e)
{
this.num.Text = Convert.ToString(Convert.ToInt32(num.Text) - 1);
PageCount();
}
protected void btndown_Click(object sender, EventArgs e)
{
this.num.Text = Convert.ToString(Convert.ToInt32(num.Text) + 1);
PageCount();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = Convert.ToInt32(e.NewEditIndex.ToString());
PageCount();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
PageCount();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys [e.RowIndex]["CustomerID"].ToString ();
Response.Write(id.ToString());
string sqlcmd = "delete from Customers where CustomerID='" + id + "'";
SqlConnection conn = DB.NorthwindConnection();
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
conn.Open();
cmd.ExecuteNonQuery();
PageCount();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.Columns[1].Visible = false;
PageCount();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex]["CustomerID"].ToString();
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1")).Text;
string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox2")).Text;
string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text;
string sqlcmd = "update Customers set CompanyName='" + name + "',City='"+city+"',Phone='"+phone+"'where CustomerID='"+id+"' ";
SqlConnection conn = DB.NorthwindConnection();
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
conn.Open();
cmd.ExecuteNonQuery();
this.GridView1.EditIndex = -1;
PageCount();
}
protected void Button2_Click(object sender, EventArgs e)
{
this.GridView1.Rows[1].Visible = false;
}
}
浙公网安备 33010602011771号