gridview的简单示例 (个人收藏)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.OracleClient;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetDataBak();
}
}
更新#region 更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//主键 .net2003 string strKey=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string strKey = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
//更新的行 .net2003 string str=((TextBox)e.Item.Cells[1].Controls[0]).Text;
string str = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string strUpdate = string.Format("update xz_shoulidengji set baogaorenxm='{0}' where iidd='{1}'", str,strKey);
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
OracleConnection conn = new OracleConnection(connectionstring);
OracleCommand com = new OracleCommand(strUpdate, conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
this.GridView1.EditIndex = -1;
GetDataBak();
}
#endregion
绑定数据#region 绑定数据
private void GetDataBak()
{
this.GridView1.DataSource = SqlDataSource1;
this.GridView1.DataBind();
}
#endregion
编辑#region 编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
GetDataBak();
}
#endregion
取消编辑#region 取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
GetDataBak();
}
#endregion
翻页#region 翻页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GetDataBak();
}
#endregion
删除#region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strKey = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string strUpdate = string.Format("delete xz_shoulidengji where iidd='{0}'", strKey);
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
OracleConnection conn = new OracleConnection(connectionstring);
OracleCommand com = new OracleCommand(strUpdate, conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
GetDataBak();
}
#endregion
排序#region 排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//GridView1.SortDirection == SortDirection.Ascending;
//GridView1.SortExpression=SortDirection = SortDirection.Ascending;
}
#endregion
判断checkbox被选中#region 判断checkbox被选中
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder SB = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
// .net2003 if(((CheckBox)DataGrid1.Items[i].FindControl("CheckBox1")).Checked==true) string str=DataGrid1.Items[i].Cells[1].Text;
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked)
{
// 得到超连接的文本
string hl = ((HyperLink)GridView1.Rows[i].Cells[1].Controls[0]).Text;
SB.Append(hl);
// Response.Write(某某被选中);此处要用Text
string strKey = GridView1.Rows[i].Cells[2].Text;
SB.Append(strKey);
}
}
Response.Write("<script>alert('"+SB.ToString()+"')</script>");
}
#endregion
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='yellow',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
//单击行改变行背景颜色
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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.OracleClient;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetDataBak();
}
}
更新#region 更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//主键 .net2003 string strKey=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string strKey = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
//更新的行 .net2003 string str=((TextBox)e.Item.Cells[1].Controls[0]).Text;
string str = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string strUpdate = string.Format("update xz_shoulidengji set baogaorenxm='{0}' where iidd='{1}'", str,strKey);
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
OracleConnection conn = new OracleConnection(connectionstring);
OracleCommand com = new OracleCommand(strUpdate, conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
this.GridView1.EditIndex = -1;
GetDataBak();
}
#endregion
绑定数据#region 绑定数据
private void GetDataBak()
{
this.GridView1.DataSource = SqlDataSource1;
this.GridView1.DataBind();
}
#endregion
编辑#region 编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
GetDataBak();
}
#endregion
取消编辑#region 取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
GetDataBak();
}
#endregion
翻页#region 翻页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GetDataBak();
}
#endregion
删除#region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strKey = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string strUpdate = string.Format("delete xz_shoulidengji where iidd='{0}'", strKey);
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
OracleConnection conn = new OracleConnection(connectionstring);
OracleCommand com = new OracleCommand(strUpdate, conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
GetDataBak();
}
#endregion
排序#region 排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//GridView1.SortDirection == SortDirection.Ascending;
//GridView1.SortExpression=SortDirection = SortDirection.Ascending;
}
#endregion
判断checkbox被选中#region 判断checkbox被选中
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder SB = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
// .net2003 if(((CheckBox)DataGrid1.Items[i].FindControl("CheckBox1")).Checked==true) string str=DataGrid1.Items[i].Cells[1].Text;
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked)
{
// 得到超连接的文本
string hl = ((HyperLink)GridView1.Rows[i].Cells[1].Controls[0]).Text;
SB.Append(hl);
// Response.Write(某某被选中);此处要用Text
string strKey = GridView1.Rows[i].Cells[2].Text;
SB.Append(strKey);
}
}
Response.Write("<script>alert('"+SB.ToString()+"')</script>");
}
#endregion
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='yellow',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
}
//单击行改变行背景颜色
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="iidd" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="anjianid" DataNavigateUrlFormatString="Default3.aspx?anjianid={0}"
DataTextField="anjianid" HeaderText="ANJIANID" />
<asp:BoundField DataField="BAOGAORENXM" HeaderText="BAOGAORENXM" SortExpression="BAOGAORENXM" />
<asp:BoundField DataField="IIDD" HeaderText="IIDD" SortExpression="IIDD" />
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick="return confirm('确定要删除此记录吗?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "ANJIANID", "BAOGAORENXM", "IIDD" FROM "XZ_SHOULIDENGJI"'>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
<!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>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="iidd" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="anjianid" DataNavigateUrlFormatString="Default3.aspx?anjianid={0}"
DataTextField="anjianid" HeaderText="ANJIANID" />
<asp:BoundField DataField="BAOGAORENXM" HeaderText="BAOGAORENXM" SortExpression="BAOGAORENXM" />
<asp:BoundField DataField="IIDD" HeaderText="IIDD" SortExpression="IIDD" />
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" OnClientClick="return confirm('确定要删除此记录吗?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "ANJIANID", "BAOGAORENXM", "IIDD" FROM "XZ_SHOULIDENGJI"'>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>