GridView 72般绝技

gattohy 2010-01-28 02:35:00
GridView选中,编辑,取消,删除:
后台代码:
你可以使用sqlhelper,本文没用。代码如下:
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
//清清月儿http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//绑定
public void bind()
{
string sqlstr = "select * from 表";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "表");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close();
}
}

前台主要代码:
... ...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="姓名" HeaderText="用户姓名" />
<asp:BoundField DataField="员工性别" HeaderText="性别" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
...全文
127 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
dangrl 2010-01-28
  • 打赏
  • 举报
回复
不知道干嘛呢?
jilezhenren 2010-01-28
  • 打赏
  • 举报
回复
太简单了
hhww520 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 pengzhihua 的回复:]
楼主太无聊,鉴定完毕
从网上找了这么个东西,只有菜鸟才有兴趣看
[/Quote]hj
星星点灯6 2010-01-28
  • 打赏
  • 举报
回复
楼主太无聊,鉴定完毕
从网上找了这么个东西,只有菜鸟才有兴趣看
assiwe 2010-01-28
  • 打赏
  • 举报
回复
接分~~
takako_mu 2010-01-28
  • 打赏
  • 举报
回复
莫非所有人都在用ListView,可ListView是VS2008才有的東西。。。
sssddfff 2010-01-28
  • 打赏
  • 举报
回复
up
小鹏 2010-01-28
  • 打赏
  • 举报
回复
up
feelnowt 2010-01-28
  • 打赏
  • 举报
回复
我接我接
huming_h 2010-01-28
  • 打赏
  • 举报
回复
楼主在散分。。
xingshungames 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 vrhero 的回复:]
GridView已经过时了,淘汰产品...
[/Quote]

Repeater用的比较多。GridView我还真不太会用。。。。
vrhero 2010-01-28
  • 打赏
  • 举报
回复
GridView已经过时了,淘汰产品...
zhulong1111 2010-01-28
  • 打赏
  • 举报
回复
me too 在 too
xingshungames 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 michael_76549786 的回复:]
引用 3 楼 jenny0810 的回复:
引用 2 楼 zhangkewen1234 的回复:
路过,搞不清楼主想干嘛


我也是
[/Quote]
me too。清清月儿的GridView 72般绝技,好久的了
Darren 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhangkewen1234 的回复:]
路过,搞不清楼主想干嘛
[/Quote]

路过
Michael_76549786 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jenny0810 的回复:]
引用 2 楼 zhangkewen1234 的回复:
路过,搞不清楼主想干嘛

[/Quote]
我也是
jenny0810 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhangkewen1234 的回复:]
路过,搞不清楼主想干嘛
[/Quote]恩
zhangkewen1234 2010-01-28
  • 打赏
  • 举报
回复
路过,搞不清楼主想干嘛
cfvgodot 2010-01-28
  • 打赏
  • 举报
回复
职业帮顶

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧