- 浏览: 1042013 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (639)
- 服务器配置篇 (58)
- hibernate篇 (14)
- spring篇 (33)
- struts篇 (28)
- JS篇 (46)
- 其他技术篇 (46)
- 数据库集群配置 (6)
- JAVA基础相关 (48)
- 分布式框架HadHoop的应用 (2)
- FLEX篇 (8)
- SQLSERVER技术 (32)
- Android学习 (13)
- amchart学习笔记 (1)
- openfire+smark搭建即时通讯 (9)
- Linux学习 (18)
- Oracle数据库 (15)
- 网站优化技术 (12)
- mysql数据库 (2)
- 项目学习总结 (18)
- 工具类(JAVA) (12)
- 工具类(JS) (2)
- 设计模式 (10)
- Lucene学习 (24)
- EJB3学习 (6)
- Sphinx搜索引擎 (3)
- 工作中用到的软件小工具 (5)
- .NET (49)
- JAVA 连接SQLSERVER2008步骤 (1)
- MongoDB (19)
- Android手机开发 (3)
- Maven (6)
- vue (9)
- Shiro (4)
- mybatis (3)
- netty框架 (1)
- SpringCloud (3)
- spring-cloud (7)
- Git (1)
- dubbo (2)
- springboot (13)
- rocketmq (1)
- git学习 (2)
- kafka服务器 (2)
- linux (10)
- WEB系统辅助项目 (1)
- jenkins (2)
- docker (4)
- influxdb (3)
- python (2)
- nginx (1)
最新评论
-
jiangfuofu555:
这样数据量大,效率怎么样?
sqlserver 实现分页的前台代码 以及后台的sqlserver语句 -
w156445045:
博主请问下,如何做到实时的刷新呢,
另外我后台是Java 谢谢 ...
web 版本的汽车仪表盘,非常好看。还有各种图形 -
jackyin5918:
<transportConnector name=&qu ...
ActiveMQ的activemq.xml详细配置讲解 -
握着橄榄枝的人:
你这个不是spring1.x的吧
spring1.x使用AOP实例 -
xiaophai:
全乱套了!
openfire+spark搭建完美的及时通讯
基本步骤:
1.在页面选择GridView空间
2.通过ADO.NET方法链接数据库。
3.在代码中,根据删除,编辑,取消,更新等按钮,执行相应的指定方法。
代码:
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 MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
namespace AspDB
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)//该段代码表示,当页面第一次加载的时候会绑定一次,当页面点更新的时候,一定要写该行代码,要不然就更新不了。因为点更新按钮后
//没有该行,页面就重新获取原来的值了。
dbBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
MySqlCommand commend = new MySqlCommand("select * from user", con);
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
MySqlDataAdapter msda = new MySqlDataAdapter("select * from user limit 50",con);
DataSet ds = new DataSet();
msda.Fill(ds);
con.Close();
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "ID" };
this.GridView1.DataBind();
}
else
{
Response.Write("<script>alert('数据库连接没打开。');</script>");
}
}
//该控件如果点击分页按钮,默认会触发PageIndexChanging方法
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;//通过点击页面的分页数字。传到后台的固定取值方法。
dbBind();
}
private void dbBind()
{
MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
MySqlCommand commend = new MySqlCommand("select * from user", con);
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
MySqlDataAdapter msda = new MySqlDataAdapter("select * from user limit 50", con);
DataSet ds = new DataSet();
msda.Fill(ds);
con.Close();
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "ID"};//该段代码用来绑定你在删除或更新操作的时候,页面所传来的ID表示的是哪个字段。
this.GridView1.DataBind();
}
else
{
Response.Write("<script>alert('数据库连接没打开。');</script>");
}
}
//我们在GridView的列表中,选择旁边的按钮,然后选择"编辑列",然后选择CommandField选择“删除”,就会出现删除的列,当点击删除的时候,会指定执行RowDeleting
//方法,你双击事件中的该方法,然后开始写代码。
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();//指定写法,来得到页面传来的删除的ID值
//要选择GridView1.DataKeys[e.RowIndex].Value的时候,在绑定数据源的时候,一定要申明你绑定的值:this.GridView1.DataKeyNames = new string[] { "ID"};
MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
string sql = "delete from user where id="+Convert.ToInt32(id);
con.Open();
MySqlCommand commend = new MySqlCommand(sql, con);
commend.ExecuteNonQuery();
con.Close();
this.dbBind();
}
//当点击编辑按钮,默认执行事件中的RowEditing方法
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//当选择编辑按钮,页面传入的ID值的取值方式固定写法。
this.GridView1.EditIndex = e.NewEditIndex;
this.dbBind();
}
//当点击取消按钮,默认执行事件中的RowCancelingEdit方法
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;//当点击取消按钮,只要将EditIndex设置为-1;
this.dbBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
//取值你要更新列的值。我的列的顺序为:删除,选择,ID,USERNAME,FLAG.
//我要更新的是username,flag,列是从0开始,所以选择的cell为第3列,和第4列。
//要取得你要更新列的指定写法。
string newName = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
string newFlag = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim();
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = "update user set name='"+newName+"',flag='"+newFlag+"' where id="+Convert.ToInt32(id);
MySqlCommand command = new MySqlCommand(sql,con);
con.Open();
command.ExecuteNonQuery();
con.Close();
this.GridView1.EditIndex = -1;
this.dbBind();
}
}
}
发表评论
-
C# WinForm程序如何与js交互
2012-07-15 22:28 2253一、建立网页 <html ... -
SQLITE FOR .NET4.0使用说明以及DLL
2012-06-28 22:07 1160SQLITE FOR .NET4.0使用说明以及DLL -
WinForm中TabControl的一些事件写法(C#)
2012-06-27 20:41 9828在TabControl控件中并没提供单个选项卡的Click事件 ... -
C# 通过字符串动态创建一个窗体
2012-06-27 20:27 1682private void button1_Click(obje ... -
vs2010 打包winform成EXE文件
2012-04-20 14:03 1567见附件 -
C#中DataGridView控件60招
2012-01-13 09:36 64791. DataGridView当前的单元格属性取得、变更 2 ... -
单击dataGridView某一行时将dataGridView当前选择行的某列值赋值给某个文本框
2012-01-13 09:19 2409SelectedRows和CurrentRow之间的区别 ... -
c# winform开发-datagridview开发
2012-01-13 09:18 1688datagridview 操作详解 目录: 1、 取得或 ... -
C# 中奇妙的函数. String Split 和 Join
2011-10-25 10:51 1056很多时候处理字符串数据,比如从文件中读取或者存入 - 我们可能 ... -
asp.net中web.config配置节点大全详解
2011-10-25 10:16 1333asp.net中web.config配置节点大全详解 2 ... -
Entity Framework in ASP.NET MVC Application (三)
2011-05-10 20:31 2429In the previous tutorial you im ... -
Entity Framework in ASP.NET MVC Application (二)
2011-05-10 20:29 1654In the previous tutorial you cr ... -
Entity Frame Work 4.1调用存储过程
2011-05-10 20:24 2070在这个问题上,琢磨了很久了。今天终于找到了调用的方法。 存储 ... -
entity-framework (code-first)实例开发(一)
2011-05-09 20:40 2708The Contoso University Web Appl ... -
使用ef4.1 的dbcontext进行数据库循环操作
2011-04-24 23:06 2206如果你想要调用一个类的方法进行循环操作:官方的例子: Dis ... -
使用EF 4.1的DbContext
2011-04-24 22:36 3600简述:EF4.1包括Code First和DbContext ... -
ADO.NET 访问存储过程
2011-04-24 22:15 1544ADO.NET 访问存储过程其实也比较简单,但是有些小细节部分 ... -
JAVA与.NET的相互调用——通过Web服务实现相互调用(附原代码)
2011-04-24 20:58 1257JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有 ... -
视图模式/视图->视图包
2011-04-24 20:16 1431ASP.NET MVC(自V1起)就支持一个带控制器和视图的V ... -
SkipRequestValidation] –> [AllowHtml]
2011-04-24 20:15 1385ASP.NET MVC内置了防止HTML和跨站脚本注入攻击的支 ...
相关推荐
asp.net 导出GridView里的数据到Excel中
ASP.NET固定GridView表头_SuperTables
本文关键词: TonyPatton TonyPatton GridView ASP.NET ASP.NET ASP.Net Asp.net ASP.NET 2.0
asp.net中GridView控件的各种操作方法,使用大全,附加数据库。包含:GridView导入导出,多层嵌套,表头表尾,分页排序,结合控件,设置属性,增除删改,主键索引...物超所值,亲情放送!~
Formatting ASP.NET GridView using jQuery Highlight row on mouseover in GridView using jQuery Set Alternate color for GridView columns using jQuery Change cursor to hand on mouseover in GridView ...
C# 写的一个简单的 ASP.NET 2.0 GridView 的示例,在示例1的基础上又添加了隐藏列和给ButtonField添加提示窗体的示范。
asp.net ajax GridView 无刷新
asp.net 获取gridview绑定列索引,以便后续根据绑定字段名对各列操作。
asp.net的GridView控件的技术使用 asp.net的GridView控件的技术使用 asp.net的GridView控件的技术使用 asp.net的GridView控件的技术使用 (本人保证绝对实用经典整合!!!)
ASP.net GridView双击事件,弹出一个窗口显示详细信息,模式窗口显示详细信息。
附详细使用说明,非常简单,解决问题直接了当。
ASP.NET的GridView控件自定义模板设计实例
一个漂亮的GridView CSS 模板样式。 测试过,可用。
Asp.net Ajax组件应用,数据显示控件Ajax GridView应用
ASP.NET中的数据绑定-gridview,对此不了解的朋友们可以看下。
Asp.net中GridView的使用详解
ASp.NET GridView 编辑模板ASp.NET GridView 编辑模板
在项目中主要用了gridview来显示数据,其中的方法都是常用的。
asp.net TreeView 与 GridView 相结合管理无限极分类,本资源用javascript脚步实现选中离开后依然保持变色,直到选中另一个节点
ASP.NET实现Gridview隐藏/显示列源码 介绍: 这篇文章演示如果让用户有显示/隐藏他们需要的GridView的列的功能,这是非常有用的,因为在GridView的所有列并不是每个的用户都需要的.用户想根据自己的需求看到想要的...