GridView实现编辑删除
前台界面:
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="" ForeColor="#333333"
AutoGenerateColumns="False" AllowPaging="True" PageSize="" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging"
GridLines="None" BorderStyle="Groove">
<Columns>
<asp:BoundField HeaderText="编号" DataField="id" Visible="false" />
<asp:BoundField HeaderText="序号" DataField="num" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="隧道名称" ReadOnly="True" />
<asp:BoundField DataField="tunnelClassName" HeaderText="隧道类别" ReadOnly="True" />
<asp:TemplateField HeaderText="起始点">
<ItemTemplate>
<%# Eval("PointStart")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbStart" Text='<%# Eval("PointStart") %>' runat="server" Width="90px" />
</EditItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="长度">
<ItemTemplate>
<%# Eval("Length")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbLength" Text='<%# Eval("Length") %>' runat="server" Width="90px" />
</EditItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
</Columns>
<PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
<RowStyle Height="20px" BackColor="#FFF3EE" ForeColor="#333333" HorizontalAlign="Center"/>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#FE6D38" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#FE6D38" Font-Bold="True" ForeColor="Black"/>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
后台代码:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
} protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -;
GridViewBind();
} protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[].ToString();
string connStr = ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ConnectionString;
string SqlStr = "delete from TN_TunnelBlankPoints where id=" + id; try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close(); GridView1.EditIndex = -;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
} protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[].ToString();
string strStart = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbStart")).Text;
string strLength = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbLength")).Text; string connStr = ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ConnectionString;
string SqlStr = " update TN_TunnelBlankPoints set PointStart=" + strStart + ",Length=" + strLength + " where id=" + id; try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close(); GridView1.EditIndex = -;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
GridView实现编辑删除的更多相关文章
- GridView总结二:GridView自带编辑删除更新
GridView自带编辑删除更新逻辑很简单:操作完,重新绑定.总结总结,防止忘记... 效果图: 前台代码: <%@ Page Language="C#" AutoEvent ...
- GridView编辑删除操作
第一种:使用DataSource数据源中自带的编辑删除方法,这样的不经常使用,在这里就不加说明了. 另外一种:使用GridView的三种事件:GridView1_RowEditing(编辑).Grid ...
- 利用yii2 gridview实现批量删除案例[转]
今天仍然继续探讨GridView的问题,昨天有个小伙伴留言说你用gridview给我去掉表头的链接?我想啊想,这用gridview确实不容易实现,至少我没想出来,会的下方可留言.但是呢,这根gridv ...
- 利用yii2 gridview实现批量删除案例
作者:白狼 出处:http://www.manks.top/article/yii2_gridview_deleteall本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...
- editplus批量删除重复行(编辑-删除-删除重复行)
editplus快速删除重复数据 多行文本,有些行的文字或数据是重复的,该怎么删除重复部分,只留下不重复的部分?很多人对这个问题感到无比头疼,Editplus同样能快速帮你删除数据. 那么,editp ...
- UITableView 自带编辑删除 自己定义button
一:UITableView 自带编辑删除 1:实现两个方法就可以 #pragma mark tableView自带的编辑功能 -(void)tableView:(UITableView *)tab ...
- GridView行编辑、更新、取消、删除事件使用方法
注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框.为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码.这样就能使你想编辑的列转换成文 ...
- 【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码
代码都是基础操作,后续功能还会更新,如有问题欢迎提出和提问....... 前台代码: <asp:GridView ID=" OnRowDataBound="GridView1 ...
- GridView编辑删除
A前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...
随机推荐
- centos 6.5 下使用nginx 反向代理 多个tornado
centos 6.5 nginx 1.4.4 /etc/nginx/conf.d 下创建tornado.conf upstream tornado { server 127.0.0.1:8887; s ...
- B4:策略模式 Strategy
它定义了算法家族,分别封装起来,让他们之间可互相替换,此模式让算法的变化,不会影响到使用算法的客户. UML 示例代码: abstract class Strategy { protected $mo ...
- struts2 接口如何接收客户端提交的json数据
struts2 接口如何接收客户端提交的json数据 CreationTime--2018年6月20日15点54分 Author:Marydon 1.情景还原 使用struts2写的接口(服务端) ...
- Alternating Least Squares(ASL) for Implicit Feedback Datasets的数学推导以及用Python实现
近期在看CF的相关论文,<Collaborative Filtering for Implicit Feedback Datasets>思想非常好,非常easy理解.可是从目标函数 是怎样 ...
- loadrunner使用sitescope监测监控mysql数据库
分类: LoadRunner 性能测试 2012-11-22 00:14 2644人阅读 评论(0) 收藏 举报 loadrunnerLoadRunnermysqlMySQLMysqlMYSQLMyS ...
- tomcat 输入localhost:8080显示404 (找不到tomcat主页)
最近使用tomcat时常出现一个问题,tomcat开启后浏览器输入localhost:8080时显示404,但是输入项目的路径是可以看到效果的,因为没啥大碍,所以没有在意 [ 在这里顺便介绍几种访问 ...
- JSON——Java中的使用
1. 构建JSON方法(数据——>JSON) 这里使用Maven构建项目 在pom.xml中添加如下依赖 <dependency> <groupId>org.json&l ...
- 版本控制器-VSS和SVN区别
SVN 默认的工作方式和VSS不同, VSS是[锁定-修改-解锁],VSS是一个人在改的时候必须以独占的方式签出文件,导致其他人不能够修改.用VSS经常要问同事:"改完没,签入一下" ...
- [c#.Net]DevExpress使用记录
Devexpress知识点 Grid controlDataSource使用BindingList<>,动态绑定数据源 Grid viewOptionBehaviour.Editable可 ...
- 微信小程序下拉按钮动画
有些时候要求下拉按钮需要动画效果,但又不需要引入插件. 这时需要手动写一个动画. 主要思路: 动态切换class 默认与动画转向的样式编写 上图是默认给出的按钮向下的样式, 上图是动画转向后的样式 上 ...