Gridview中的选择、删除、编辑、更新、取消留着备用。
后台程序:
public partial class tw2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.createdb();
}
}
private void createdb()
{
SqlConnection cn = new SqlConnection("server=.;database=zhang;uid=sa;pwd=410"); //(Data Source =服务器;Initial Catalog=数据库;Integrated Security=True")本地用户登录
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = new SqlCommand("select * from chang", cn);
DataSet ds = new DataSet();
adp.Fill(ds, "chang");
this.GridView1.DataKeyNames = new string[] { "changid" }; //定义主键
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
cn.Close(); }
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) //取消按钮
{
GridView1.EditIndex = -1;
this.createdb();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) //编辑按钮
{
GridView1.EditIndex = e.NewEditIndex;
this.createdb();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) //选择按钮
{
int id = Convert.ToInt32(e.CommandArgument); //取行索引号
string txt = GridView1.DataKeys[id].Value.ToString(); //取主键的值
Response.Write(txt);
// this.createdb(); }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) //删除按钮
{
//在删除事件中获取绑定列的值 GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
SqlConnection cn = new SqlConnection("server=.;database=zhang;uid=sa;pwd=410");
cn.Open();
SqlCommand cm=new SqlCommand("delete from chang where changid=" +GridView1.DataKeys[e.RowIndex].Value,cn); //e.RowIndex取本行索引号,
cm.ExecuteNonQuery();
cn.Close();
GridView1.EditIndex = -1;
this.createdb(); }
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) //更新按钮
{
GridViewRow row = GridView1.Rows[e.RowIndex];
TextBox box = (TextBox)row.Cells[2].Controls[0];
Response.Write(box.Text);
try
{
SqlConnection cn = new SqlConnection("server=.;database=zhang;uid=sa;pwd=410");
cn.Open();
SqlCommand cm = new SqlCommand("update chang set chang1='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',chang2='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where changid=" + GridView1.DataKeys[e.RowIndex].Value, cn); //列的编号是从1,这里更新的是第二列和第三列所以cell[2],cell[3],而行的索引是从0开始的
cm.ExecuteNonQuery();
GridView1.EditIndex = -1;
this.createdb();
cn.Close();
//在更新事件中获取其他绑定列的值
// GridView1.Rows[e.RowIndex].Cells[9].Text.ToString();
//由于隐藏列不能获取其值,可以用以下方法:
//1.先定义个隐藏样式
<style type="text/css">
.hidden { display:none;}
</style>
2.在隐藏列的HTML标记里引用这个样式,代码如下:
<asp:BoundField DataField="num" ReadOnly="True">
<HeaderStyle CssClass="hidden" />
<ItemStyle CssClass="hidden" />
</asp:BoundField>
//这样就可以获取隐藏列的值了,比如修改出库数量的时候要先判断库存等。
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
}
Gridview中的选择、删除、编辑、更新、取消留着备用。的更多相关文章
- gridview的编辑,更新,取消,自动分页等
gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...
- Gridview中运用CommandField 删除控件时注意点
我在gridview1 <编辑列>里面添加了一个<CommandField 删除>的控件,之后在gridview1的事件<RowDeleting>事件下 写了一段删 ...
- GridView中CheckBox的用法
以下是我在GridView中控制CheckBox的全选/取消全选,根据子CheckBox处理全选CheckBox状态的操作并获取所选GridView中所选行的列的值. 脚本代码: <scr ...
- GridView中的编辑和删除按钮,执行更新和删除代码之前的更新提示或删除提示
在GridView中,可以通过设计界面GridViewr任务->编辑列->CommandField,很简单的添加的编辑和删除按钮 在前台源码中,可以看到GridView自动生成了两个列. ...
- ASP.net中GridView中增加一行记录并默认显示为编辑状态
//添加 protected void Button1_Click(object sender, EventArgs e) { DataSet ds = (DataSet)pa.GetDataSet( ...
- AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除
AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...
- [原创]MYSQL中利用外键实现级联删除和更新
MySQL中利用外键实现级联删除.更新 MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定 ...
- MySQL中利用外键实现级联删除、更新
MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定在删除.更新父表时,对子表进行的相应操作 ...
- openerp模块收藏 移除下拉选择列表中的“创建并编辑”链接(转载)
移除下拉选择列表中的“创建并编辑”链接 原文:http://shine-it.net/index.php/topic,5990.0.html 有时希望下拉列表中列出的项是与主表某个字段关联的,用户只能 ...
随机推荐
- IIS7 MVC 403 禁止访问:访问被拒绝
- django源码解析之 BooleanField (二)
class BooleanField(Field): empty_strings_allowed = False default_error_messages = { 'invalid': _(u&q ...
- .net将枚举转成List
using System; using System.Collections.Generic; using System.ComponentModel; namespace zifar.SgmhWeb ...
- One-hot 编码/TF-IDF 值来提取特征,LAD/梯度下降法(Gradient Descent),Sigmoid
1. 多值无序类数据的特征提取: 多值无序类问题(One-hot 编码)把“耐克”编码为[0,1,0],其中“1”代表了“耐克”的中 间位置,而且是唯一标识.同理我们可以把“中国”标识为[1,0],把 ...
- php使用正则函数使用详解
1. int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, in ...
- 【微信小程序】tabBar的显示问题
tabBar不显示 在app.json中配置了4个页面,在tabBar的list中随意写了两个页面,编译后发现不能显示tabBar. { "pages": [ "page ...
- [git]使用vimdiff做git代码比较
#git 如何实现vimdiffgit config --global diff.tool vimdiff git config --global difftool.prompt false git ...
- log_bin_trust_function_creators
log_bin_trust_function_creators错误解决 当有mysql本地或远程建立function或procedure时报上面的错误 或者如果开启了二进制日志,但是用户没有suppe ...
- jdbc读取百万条数据出现内存溢出的解决办法
本人在做项目实施时,我们使用的是mysql数据库,在不到一个月的时间已经有了2千万条数据,查询的时候非常慢,就写了一个数据迁移的小项目,将这两千万条数据存放到MongoDB中看效率怎么样,再读取数据时 ...
- MPAndroidChart的具体属性方法
android中常用的第三方图表MPAndroidChart的一些具体属性及方法说明 注意:在将折线图转为曲线图时,lineDataSet.setMode(LineDataSet.Mode.CUBIC ...