ASP.net GridView基本用法
2017-05-19 17:18
661 查看
转载自:http://www.cnblogs.com/ianunspace/p/3438233.html
包含有
数据的编辑,删除,
标题的添加,自定义分页,高亮显示鼠标所在,以及数据不足时添加空行
aspx页面代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
GridLines="None" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" Width="945px" AllowPaging="True" OnPageIndexChanged="GridView1_PageIndexChanged" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCreated="GridView1_RowCreated"
OnRowDeleting="GridView1_RowDeleting" OnDataBound="GridView1_DataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="stuid" HeaderText="学号" />
<asp:BoundField DataField="stuname" HeaderText="姓名" />
<asp:BoundField DataField="majorid" HeaderText="专业编号" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="birthdate" HeaderText="出生日期" />
<asp:BoundField DataField="credit" HeaderText="总学分" />
<asp:BoundField DataField="remark" HeaderText="备注" />
<asp:CommandField HeaderText="操作" ShowEditButton="True" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
<PagerTemplate>
<table>
<tr><td>
第<asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex+1 %>"></asp:Label>页
共<asp:Label ID="lblPageCount" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="false" CommandArgument="First" CommandName="Page">首页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="false" CommandArgument="Prev" CommandName="Page">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="false" CommandArgument="Next" CommandName="Page">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="false" CommandArgument="Last" CommandName="Page">尾页</asp:LinkButton>
到<asp:DropDownList ID="listPageCount" runat="server" AutoPostBack="true" Width="50"></asp:DropDownList>
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="false" CommandName="Go">Go</asp:LinkButton></td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
以下是后台代码:---------------------------------------------------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.PageSize = 6;
GridView1.Columns[0].Visible = false;
GridView1.PagerSettings.Mode = PagerButtons.NumericFirstLast;
GridView1.PagerSettings.PageButtonCount = 4;
// 页数居中显示
GridView1.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
DataGridBind();
}
}
string connStr = ConfigurationManager.ConnectionStrings["Key"].ToString();
// 数据绑定
private void DataGridBind()
{
string sql = "select * from student";
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
conn.Close();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] { "stuid" };
GridView1.Columns[1].ItemStyle.Width=80;
GridView1.Columns[2].ItemStyle.Width = 80;
GridView1.Columns[3].ItemStyle.Width = 40;
GridView1.Columns[4].ItemStyle.Width = 200;
GridView1.Columns[5].ItemStyle.Width = 60;
GridView1.Columns[6].ItemStyle.Width = 250;
GridView1.Columns[7].ItemStyle.Width = 100;
GridView1.DataBind();
}
}
// 编辑事件
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataGridBind();
}
// 取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataGridBind();
}
// 更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string stuid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string stuname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
int majorid = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString());
string sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
DateTime Birthdate = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString());
float credit = Convert.ToSingle(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString());
string remark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlParameter[] paras = {new SqlParameter("@stuid",stuid),
new SqlParameter("@stuname",stuname),
new SqlParameter("@majorid",majorid),
new SqlParameter("@sex",sex),
new SqlParameter("@birthdate",Birthdate),
new SqlParameter("@credit",credit),
new SqlParameter("@remark",remark)};
string sql = @"update student set stuname=@stuname,majorid=@majorid,sex=@sex,
birthdate=@birthdate,credit=@credit,remark=@remark where stuid=@stuid";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
foreach (SqlParameter para in paras)
{
cmd.Parameters.Add(para);
}
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
GridView1.EditIndex = -1;
DataGridBind();
}
// 高亮显示鼠标所在的行
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
LinkButton linBtn = (LinkButton)(e.Row.Cells[7].Controls[2]);
if (linBtn.Text == "删除")
{
linBtn.Attributes.Add("onclick", "return confirm('你确定要删除吗?')");
}
}
}
// 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string stuid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = "delete from student where stuid=" + stuid;
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
DataGridBind();
}
// 分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex < 0)
{
GridView1.PageIndex = 0;
}
else
{
GridView1.PageIndex = e.NewPageIndex;
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DataGridBind();
}
// 行创建时
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// 添加标题
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow gvr = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
Literal lit = new Literal();
lit.Text = @"<td colspan='6' align='center'><h2>学生信息</h2></td>";
TableHeaderCell thc = new TableHeaderCell();
thc.Controls.Add(lit);
gvr.Cells.Add(thc);
GridView1.Controls[0].Controls.AddAt(0, gvr);
}
// 本页行数不足添加空行
int count = 0;
count = GridView1.Rows.Count;
if (e.Row.RowType == DataControlRowType.Footer)
{
int rowCount = GridView1.PageSize - count;
int colCount = GridView1.Rows[0].Cells.Count;
for (int i = 0; i < rowCount; i++)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
for (int j = 0; j < colCount - 1; j++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt(count +2, row);
}
}
}
// 控件被数据绑定后
protected void GridView1_DataBound(object sender, EventArgs e)
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("listPageCount");
for (int i = 1; i <= GridView1.PageCount; i++)
{
ListItem item = new ListItem(i.ToString());
if (i==GridView1.PageIndex+1)
{
item.Selected = true;
}
list.Items.Add(item);
}
}
// 响应跳转事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="Go")
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("listPageCount");
GridViewPageEventArgs arg = new GridViewPageEventArgs(list.SelectedIndex);
GridView1_PageIndexChanging(null, arg);
GridView1_PageIndexChanged(null, null);
}
}
//本人验证代码
//aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="Wvti.Test.login" %>
<!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>
<%--直接将对应的css文件拖到此处即可,就可调用对应css文件里的样式 --%>
<link href="css/page.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%-- 其效果是将table中的内容紧贴浏览器的边框--%>
<form id="form1" runat="server">
<div>
<%-- 设置表格的宽和高,背景颜色,表格网线的宽度,单元格内的内容与单元格边框的距离,单元格与边框之间的距离--%>
<table height="100%" class="table1">
<tbody>
<tr>
<td width="100%" height="40" bgcolor="#02f78e">
</td>
</tr>
<tr>
<td>
<%-- 如果不再写一个表格,则若第一行中的列数和第二行的列数不同,将发生错位,
即第一行有两列,第二行有三列,则第一行的前两列和第二行的前两列将对其,第三行的
第三列空间无法控制
--%>
<table height="100%" class="table1">
<tbody>
<tr>
<td width="20%" height="20">
</td>
<td align="center" width="60%" height="20">
<h1>
欢迎登陆本系统</h1>
</td>
<td width="20%" height="20">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table height="300" class="table1">
<tbody>
<tr>
<td width="20%" height="300">
</td>
<td width="60%" height="300">
<table height="300" class="table1">
<tbody>
<tr>
<td align="left">
<asp:GridView Width="100%" ID="GridView1" runat="server" AutoGenerateColumns="false"
AllowPaging="true" PageSize="8" BackColor="Black" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanged="GridView1_PageIndexChanged"
OnRowCommand="GridView1_RowCommand" OnDataBound="GridView1_DataBound">
<Columns>
<asp:BoundField DataField="outid" HeaderText="学号" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="name" HeaderText="姓名" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="sex" HeaderText="性别" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="sumfare" HeaderText="卡总额" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="cardno" HeaderText="卡序列号" ItemStyle-Width="20%" ItemStyle-HorizontalAlign="Center" />
<asp:CommandField HeaderText="操作" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="25%"
ItemStyle-HorizontalAlign="Center" />
</Columns>
<RowStyle BackColor="white" />
<HeaderStyle BackColor="DarkOrange" />
<PagerStyle BackColor="LightGreen" />
<PagerTemplate>
<table width="100%">
<tr>
<td align="right">
第<asp:Label ID="lblpage" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex+1 %>"></asp:Label>页
共<asp:Label ID="lblpageCount" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
<asp:LinkButton ID="btnFirst" runat="server" Text="首页" CommandArgument="First" CommandName="page"></asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" Text="上一页" CommandArgument="Prev" CommandName="page"></asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" Text="下一页" CommandArgument="Next" CommandName="page"></asp:LinkButton>
到<asp:DropDownList ID="dDL" runat="server" AutoPostBack="true" Width="50">
</asp:DropDownList>
<asp:LinkButton ID="btnSkip" runat="server" Text="GO" CommandName="Go"></asp:LinkButton>
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
</td>
</tr>
</tbody>
</table>
</td>
<td width="20%" height="300">
<table height="300" class="table1">
<tbody>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td align="center" width="100%">
版权所有
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
//aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
namespace Wvti.Test
{
public partial class login : System.Web.UI.Page
{
private static string conn;
private OracleConnection oConn;
string strSql;
private OracleCommand oComm;
private OracleDataAdapter oData;
private DataTable dt;
public login()
{
string conn = System.Web.Configuration.WebConfigurationManager.AppSettings["conn"].ToString();
oConn = new OracleConnection(conn);
oConn.Open();
strSql= string.Format(@"select *from base_customers");
oComm = new OracleCommand(strSql, oConn);
oData = new OracleDataAdapter(oComm);
dt = new DataTable();
oData.Fill(dt);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGrid();
}
}
//private string conn = System.Web.Configuration.WebConfigurationManager.AppSettings["conn"].ToString();
// private OracleConnection oConn = new OracleConnection(conn);
protected void DataGrid()
{
//string conn=System.Web.Configuration.WebConfigurationManager.AppSettings["conn"].ToString();
//连接数据库
//OracleConnection oConn = new OracleConnection(conn);
// string strSql = string.Format(@"select *from base_customers");
//OracleCommand oComm = new OracleCommand(strSql, oConn);
//OracleDataAdapter oData = new OracleDataAdapter(oComm);
//DataTable dt = new DataTable();
//oData.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataGrid();
}
protected void GridView1_RowDeleting(object sender, GridViewEditEventArgs e)
{
//删除数据库中的数据再重新绑定数据源
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex < 0)
{
GridView1.PageIndex = 0;
}
else
{
GridView1.PageIndex = e.NewPageIndex;
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DataGrid();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("dDL");//找到ID为dDL的控件
for (int i = 1; i < GridView1.PageCount; i++)
{
ListItem item = new ListItem(i.ToString());
if (i == GridView1.PageIndex + 1) //当前页
{
item.Selected = true; //此元素作为当前被选中的元素
}
list.Items.Add(item);
}
}
//当点击GridView中某个控件时触发的事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Go")//判断传过来的命令是不是GO
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("dDL");
GridViewPageEventArgs arg = new GridViewPageEventArgs(list.SelectedIndex);
GridView1_PageIndexChanging(null, arg);
GridView1_PageIndexChanged(null, null);
}
}
// 高亮显示鼠标所在的行
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
LinkButton linBtn = (LinkButton)(e.Row.Cells[5].Controls[2]); //定位到当前行第6列中第二个控件
if (linBtn.Text == "删除")
{
//当点击删除按钮是蹦出提示框
linBtn.Attributes.Add("onclick", "return confirm('你确定要删除吗?')");
}
}
}
}
}
包含有
数据的编辑,删除,
标题的添加,自定义分页,高亮显示鼠标所在,以及数据不足时添加空行
aspx页面代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
GridLines="None" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" Width="945px" AllowPaging="True" OnPageIndexChanged="GridView1_PageIndexChanged" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCreated="GridView1_RowCreated"
OnRowDeleting="GridView1_RowDeleting" OnDataBound="GridView1_DataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="stuid" HeaderText="学号" />
<asp:BoundField DataField="stuname" HeaderText="姓名" />
<asp:BoundField DataField="majorid" HeaderText="专业编号" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="birthdate" HeaderText="出生日期" />
<asp:BoundField DataField="credit" HeaderText="总学分" />
<asp:BoundField DataField="remark" HeaderText="备注" />
<asp:CommandField HeaderText="操作" ShowEditButton="True" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
<PagerTemplate>
<table>
<tr><td>
第<asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex+1 %>"></asp:Label>页
共<asp:Label ID="lblPageCount" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="false" CommandArgument="First" CommandName="Page">首页</asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="false" CommandArgument="Prev" CommandName="Page">上一页</asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="false" CommandArgument="Next" CommandName="Page">下一页</asp:LinkButton>
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="false" CommandArgument="Last" CommandName="Page">尾页</asp:LinkButton>
到<asp:DropDownList ID="listPageCount" runat="server" AutoPostBack="true" Width="50"></asp:DropDownList>
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="false" CommandName="Go">Go</asp:LinkButton></td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
以下是后台代码:---------------------------------------------------------------------------------------------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.PageSize = 6;
GridView1.Columns[0].Visible = false;
GridView1.PagerSettings.Mode = PagerButtons.NumericFirstLast;
GridView1.PagerSettings.PageButtonCount = 4;
// 页数居中显示
GridView1.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
DataGridBind();
}
}
string connStr = ConfigurationManager.ConnectionStrings["Key"].ToString();
// 数据绑定
private void DataGridBind()
{
string sql = "select * from student";
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
conn.Close();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] { "stuid" };
GridView1.Columns[1].ItemStyle.Width=80;
GridView1.Columns[2].ItemStyle.Width = 80;
GridView1.Columns[3].ItemStyle.Width = 40;
GridView1.Columns[4].ItemStyle.Width = 200;
GridView1.Columns[5].ItemStyle.Width = 60;
GridView1.Columns[6].ItemStyle.Width = 250;
GridView1.Columns[7].ItemStyle.Width = 100;
GridView1.DataBind();
}
}
// 编辑事件
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataGridBind();
}
// 取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataGridBind();
}
// 更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string stuid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string stuname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
int majorid = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString());
string sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
DateTime Birthdate = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString());
float credit = Convert.ToSingle(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString());
string remark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlParameter[] paras = {new SqlParameter("@stuid",stuid),
new SqlParameter("@stuname",stuname),
new SqlParameter("@majorid",majorid),
new SqlParameter("@sex",sex),
new SqlParameter("@birthdate",Birthdate),
new SqlParameter("@credit",credit),
new SqlParameter("@remark",remark)};
string sql = @"update student set stuname=@stuname,majorid=@majorid,sex=@sex,
birthdate=@birthdate,credit=@credit,remark=@remark where stuid=@stuid";
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
foreach (SqlParameter para in paras)
{
cmd.Parameters.Add(para);
}
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
GridView1.EditIndex = -1;
DataGridBind();
}
// 高亮显示鼠标所在的行
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
LinkButton linBtn = (LinkButton)(e.Row.Cells[7].Controls[2]);
if (linBtn.Text == "删除")
{
linBtn.Attributes.Add("onclick", "return confirm('你确定要删除吗?')");
}
}
}
// 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string stuid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = "delete from student where stuid=" + stuid;
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
DataGridBind();
}
// 分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex < 0)
{
GridView1.PageIndex = 0;
}
else
{
GridView1.PageIndex = e.NewPageIndex;
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DataGridBind();
}
// 行创建时
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// 添加标题
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow gvr = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
Literal lit = new Literal();
lit.Text = @"<td colspan='6' align='center'><h2>学生信息</h2></td>";
TableHeaderCell thc = new TableHeaderCell();
thc.Controls.Add(lit);
gvr.Cells.Add(thc);
GridView1.Controls[0].Controls.AddAt(0, gvr);
}
// 本页行数不足添加空行
int count = 0;
count = GridView1.Rows.Count;
if (e.Row.RowType == DataControlRowType.Footer)
{
int rowCount = GridView1.PageSize - count;
int colCount = GridView1.Rows[0].Cells.Count;
for (int i = 0; i < rowCount; i++)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
for (int j = 0; j < colCount - 1; j++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt(count +2, row);
}
}
}
// 控件被数据绑定后
protected void GridView1_DataBound(object sender, EventArgs e)
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("listPageCount");
for (int i = 1; i <= GridView1.PageCount; i++)
{
ListItem item = new ListItem(i.ToString());
if (i==GridView1.PageIndex+1)
{
item.Selected = true;
}
list.Items.Add(item);
}
}
// 响应跳转事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="Go")
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("listPageCount");
GridViewPageEventArgs arg = new GridViewPageEventArgs(list.SelectedIndex);
GridView1_PageIndexChanging(null, arg);
GridView1_PageIndexChanged(null, null);
}
}
//本人验证代码
//aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="Wvti.Test.login" %>
<!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>
<%--直接将对应的css文件拖到此处即可,就可调用对应css文件里的样式 --%>
<link href="css/page.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%-- 其效果是将table中的内容紧贴浏览器的边框--%>
<form id="form1" runat="server">
<div>
<%-- 设置表格的宽和高,背景颜色,表格网线的宽度,单元格内的内容与单元格边框的距离,单元格与边框之间的距离--%>
<table height="100%" class="table1">
<tbody>
<tr>
<td width="100%" height="40" bgcolor="#02f78e">
</td>
</tr>
<tr>
<td>
<%-- 如果不再写一个表格,则若第一行中的列数和第二行的列数不同,将发生错位,
即第一行有两列,第二行有三列,则第一行的前两列和第二行的前两列将对其,第三行的
第三列空间无法控制
--%>
<table height="100%" class="table1">
<tbody>
<tr>
<td width="20%" height="20">
</td>
<td align="center" width="60%" height="20">
<h1>
欢迎登陆本系统</h1>
</td>
<td width="20%" height="20">
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table height="300" class="table1">
<tbody>
<tr>
<td width="20%" height="300">
</td>
<td width="60%" height="300">
<table height="300" class="table1">
<tbody>
<tr>
<td align="left">
<asp:GridView Width="100%" ID="GridView1" runat="server" AutoGenerateColumns="false"
AllowPaging="true" PageSize="8" BackColor="Black" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanged="GridView1_PageIndexChanged"
OnRowCommand="GridView1_RowCommand" OnDataBound="GridView1_DataBound">
<Columns>
<asp:BoundField DataField="outid" HeaderText="学号" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="name" HeaderText="姓名" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="sex" HeaderText="性别" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="sumfare" HeaderText="卡总额" ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="cardno" HeaderText="卡序列号" ItemStyle-Width="20%" ItemStyle-HorizontalAlign="Center" />
<asp:CommandField HeaderText="操作" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="25%"
ItemStyle-HorizontalAlign="Center" />
</Columns>
<RowStyle BackColor="white" />
<HeaderStyle BackColor="DarkOrange" />
<PagerStyle BackColor="LightGreen" />
<PagerTemplate>
<table width="100%">
<tr>
<td align="right">
第<asp:Label ID="lblpage" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex+1 %>"></asp:Label>页
共<asp:Label ID="lblpageCount" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页
<asp:LinkButton ID="btnFirst" runat="server" Text="首页" CommandArgument="First" CommandName="page"></asp:LinkButton>
<asp:LinkButton ID="btnPrev" runat="server" Text="上一页" CommandArgument="Prev" CommandName="page"></asp:LinkButton>
<asp:LinkButton ID="btnNext" runat="server" Text="下一页" CommandArgument="Next" CommandName="page"></asp:LinkButton>
到<asp:DropDownList ID="dDL" runat="server" AutoPostBack="true" Width="50">
</asp:DropDownList>
<asp:LinkButton ID="btnSkip" runat="server" Text="GO" CommandName="Go"></asp:LinkButton>
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
</td>
</tr>
</tbody>
</table>
</td>
<td width="20%" height="300">
<table height="300" class="table1">
<tbody>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td align="center" width="100%">
版权所有
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
//aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
namespace Wvti.Test
{
public partial class login : System.Web.UI.Page
{
private static string conn;
private OracleConnection oConn;
string strSql;
private OracleCommand oComm;
private OracleDataAdapter oData;
private DataTable dt;
public login()
{
string conn = System.Web.Configuration.WebConfigurationManager.AppSettings["conn"].ToString();
oConn = new OracleConnection(conn);
oConn.Open();
strSql= string.Format(@"select *from base_customers");
oComm = new OracleCommand(strSql, oConn);
oData = new OracleDataAdapter(oComm);
dt = new DataTable();
oData.Fill(dt);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGrid();
}
}
//private string conn = System.Web.Configuration.WebConfigurationManager.AppSettings["conn"].ToString();
// private OracleConnection oConn = new OracleConnection(conn);
protected void DataGrid()
{
//string conn=System.Web.Configuration.WebConfigurationManager.AppSettings["conn"].ToString();
//连接数据库
//OracleConnection oConn = new OracleConnection(conn);
// string strSql = string.Format(@"select *from base_customers");
//OracleCommand oComm = new OracleCommand(strSql, oConn);
//OracleDataAdapter oData = new OracleDataAdapter(oComm);
//DataTable dt = new DataTable();
//oData.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataGrid();
}
protected void GridView1_RowDeleting(object sender, GridViewEditEventArgs e)
{
//删除数据库中的数据再重新绑定数据源
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex < 0)
{
GridView1.PageIndex = 0;
}
else
{
GridView1.PageIndex = e.NewPageIndex;
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DataGrid();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("dDL");//找到ID为dDL的控件
for (int i = 1; i < GridView1.PageCount; i++)
{
ListItem item = new ListItem(i.ToString());
if (i == GridView1.PageIndex + 1) //当前页
{
item.Selected = true; //此元素作为当前被选中的元素
}
list.Items.Add(item);
}
}
//当点击GridView中某个控件时触发的事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Go")//判断传过来的命令是不是GO
{
DropDownList list = (DropDownList)GridView1.BottomPagerRow.FindControl("dDL");
GridViewPageEventArgs arg = new GridViewPageEventArgs(list.SelectedIndex);
GridView1_PageIndexChanging(null, arg);
GridView1_PageIndexChanged(null, null);
}
}
// 高亮显示鼠标所在的行
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
LinkButton linBtn = (LinkButton)(e.Row.Cells[5].Controls[2]); //定位到当前行第6列中第二个控件
if (linBtn.Text == "删除")
{
//当点击删除按钮是蹦出提示框
linBtn.Attributes.Add("onclick", "return confirm('你确定要删除吗?')");
}
}
}
}
}
相关文章推荐
- ASP.NET整理:GridView,基本用法,高级用法,分页
- ASP.NET整理:GridView,基本用法…
- ASP.NET中的三个富数据控件:GridView,Repeater,DataList的基本用法
- ASP.NET验证控件使用详解,介绍了基本属性和用法
- asp.net的Gridview和detailsview的用法
- CKEditor 3.6 + CKFinder 2.0在Asp.Net中的配置与基本用法
- ASP.NET中的验证相关知识 及 Login 控件的基本用法
- asp.net 2.0的gridview基本知识
- ASP.NET gridview 的基本操作
- Asp.Net MVC 2.0 Filter基本用法
- CKEditor 3.6 + CKFinder 2.0在Asp.Net中的配置与基本用法
- Asp.net 中GridView各种用法
- CKEditor 3.6 + CKFinder 2.0在Asp.Net中的配置与基本用法
- asp.net 2.0的gridview基本知识
- Asp.net教程:Gridview用法总结
- asp.net 2.0的GridView的动态绑定数据的用法!
- ASP.NET项目申报系统-5.3~5.4(FileUpload和GridView用法)
- ASP.NET AjaxControlToolkit 的基本用法
- ASP.NET Repeater_第一篇_基本用法
- AspNet pager 分页的基本用法