asp.net gridview delete row of data in a way

2010-03-14  来源:本站原创  分类:DotNet  人气:307 

This is the day I spent time before they get to know, and write down the mark about!

Front of the code: Note that not using sqldatasource

<asp:GridView runat="server" AllowPaging="True" PageSize="6"  OnRowDeleting="GridView1_deleting"
    AutoGenerateColumns="False" DataKeyNames=" User name  "  Width="819px">
    <Columns>
        <asp:BoundField DataField=" The name "HeaderText =" user name  " ReadOnly="True"
            SortExpression=" User name  " />
        <asp:BoundField DataField=" Password "HeaderText =" password  " SortExpression=" Password  " />
        <asp:BoundField DataField=" Gender "HeaderText =" gender  " SortExpression=" Gender  " />
        <asp:BoundField DataField=" Date of birth "HeaderText =" date of birth  " SortExpression=" Date of birth  " />
        <asp:BoundField DataField=" Contact "HeaderText =" contact  " SortExpression=" Contact us  " />
        <asp:BoundField DataField=" Mailbox "HeaderText =" mailbox  " SortExpression=" Mailbox  " />
        <asp:BoundField DataField=" Password protection "HeaderText =" password protection issues  "
            SortExpression=" Password protection issues  " />
        <asp:BoundField DataField=" Password protection problems answered "HeaderText =" password protection questions answered  "
            SortExpression=" Password protection questions answered  " />
        <asp:BoundField DataField=" Since "HeaderText =" since  " SortExpression=" Since  " />
        <asp:TemplateField ShowHeader="true" HeaderText=" Delete user  ">
            <ItemTemplate>
                <asp:LinkButton runat="server" CausesValidation="False" CommandName="Delete"
                    Text=" Delete  " OnClientClick='<%#  "if (!confirm(\" Are you sure you want to delete  " + Eval(" User name  ").ToString() + " ?  ?\")) return false;"%>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Background code:

protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        SqlConnection conn = new SqlConnection(CommonMethods.connstring);
        conn.Open();
        string cmd = "select * from userlogin";
        SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
    }

    protected void GridView1_deleting(object sender, GridViewDeleteEventArgs e)// Note the parameter type of the various events
    {
        SqlConnection conn = new SqlConnection(CommonMethods.connstring);
        conn.Open();
        string deleteuser = GridView1.DataKeys[e.RowIndex].Value.ToString();// Gets the primary key
        string cmd = "delete from userlogin where  User name  ='" + deleteuser+"'" ;//
        SqlCommand cmdt = new SqlCommand(cmd, conn);
        try
        {
            cmdt.ExecuteNonQuery();
            Response.Write("<script>alert(' Removed successfully  !')</script>");
        }
        catch
        {
            return;
        }
        bind();
        conn.Close();
    }

When I write I find that the compiler prompts row and rowindex no such instruction set, and finally I realized that I was wrong parameter types of events, I discovered that the type of event parameters is very important. Here mind you, I hope we do not think I am guilty of the same low-level error!

相关文章
  • asp.net gridview delete row of data in a way 2010-03-14

    This is the day I spent time before they get to know, and write down the mark about! Front of the code: Note that not using sqldatasource <asp:GridView runat="server" AllowPaging="True" PageSize="6" OnRowDeleting="Gri

  • Using javascript / css implementation GridView alternate row background color. Click the line color 2009-06-20

    Using javascript / css implementation GridView alternate row background color, click the line color Ask Bo a few days ago was found in someone clicks GridView question about the issue of line color, suddenly think of a long, long time ago, wrote an a

  • Using javascript / css to achieve GridView alternate row background color. Click the line color 2009-06-22

    Using javascript / css to achieve GridView alternate row background color, click the line color Asked a few days ago was found in Bo was asked click on the GridView row color problem, suddenly reminded of a long, long time ago, wrote an article in a

  • ASP.NET 2.0 to implement data binding template 2010-07-07

    Templated data-bound controls to display data on the page we provide a fundamental flexibility. You may recall ASP.NET v1.x Several template-based controls (such as DataList, and Repeater controls). ASP.NET 2.0 continues to support these controls, bu

  • asp.net GridView控件鼠标移动某行改变背景颜色(方法一) 2014-01-13

    asp.net 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.W

  • asp.net gridview实现全选,反选与删除记录的操作代码 2014-05-11

    asp.net gridview实现全选,反选与删除记录的操作代码,需要的朋友可以参考下. gridview全选操作 'columns'=>array( array( 'class'=>'CCheckBoxColumn', //'header'=>'全选', //'value'=>'$data->id', //'checked'=>'true', 'htmlOptions'=>array( 'width'=>'30', 'style'=>'text-a

  • ASP.NET Gridview 中使用checkbox删除的2种方法实例分享 2014-05-29

    ASP.NET Gridview 中使用checkbox删除的2种方法实例分享,需要的朋友可以参考一下 方法一: 后台代码: protected void btn_delete_Click(object sender, EventArgs e) { for (int i = 0; i <this.GridView1.Rows.Count; i++) { int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value); if ((this.Gr

  • asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页 2014-08-08

    这篇文章主要介绍了asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页,可使用上下键选中行,选中后点击修改,textbox获得gridview中的代码的数据,需要的朋友可以参考下 效果图: 功能简介:可使用上下键选中行,选中后点击修改,textbox获得gridview中的代码的数据.对你有帮助的话,请记得要点击"好文要顶"哦!!!不懂的,请留言.废话不多说了,贴码如下: <head runat="server"> <

  • asp.net gridview列宽固定的几种方法介绍 2014-01-31

    以下是对asp.net gridview列宽固定的几种方法进行了介绍,需要的朋友可以过来参考下 1.在编辑列里面,直接就有设置列宽度的!如果你不想单元格的内容把单元格撑大,那么就加一个样式 <style>table{table-layout:fixed}</style>,意思就是每列都固定为最初设置的样式,不再变化,超出的部分隐藏 2.可以设置gridview中编辑列下面的某一列的属性<HeaderStyle Width="8%"></Head

  • asp.net gridview的Rowcommand命令中获取行索引的方法总结 2014-04-04

    asp.net gridview的Rowcommand命令中获取行索引的方法总结,需要的朋友可以参考下. 一.通过命令源获取当前行索引. 方法比较多, GridView 的 Command 事件中无法象 DataGrid 那样直接获取行, 法1, GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//CommandSource 引起事件的命令源,(疑问,根据MSDN说的是GridView,如果

  • asp.net GridView导出到Excel代码 2014-07-07

    asp.net GridView导出到Excel代码,需要的朋友可以参考下. StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page(); HtmlForm form = new HtmlForm(); GVLinkman.EnableViewState = fa

  • asp.net Gridview分页保存选项 2014-12-17

    这篇文章介绍了asp.net Gridview分页保存选项,有需要的朋友可以参考一下 #region //'Revision: 1.00 Created Date: 2013/08/02 Created ID: Una [#1300071]增加多?框 /// <summary> /// Session?取多?框值 /// </summary> private void RememberOldValues() { ArrayList categoryIDList = new Arra

  • Once to delete all tables data in a database 2010-06-06

    Once to delete the database to delete all tables data in a table of data in the database approach is not complicated, why should this really necessary? First, the database presented here is to delete all the data, because the data may be formed betwe

  • Tr and td Gridview control to display data consolidation solution 2010-07-19

    Tr and td Gridview control to display data consolidation solution Through gridview control and code control solution, the code below / / Display data combined tr private static void GroupRows (GridView GridView1, int cellNum, int begin, int end) ( in

  • asp.net gridview自定义value值的代码 2014-01-23

    asp.net gridview自定义value值的代码,需要的朋友可以参考下. model里: public function item($items,$name){ $returnValue=''; foreach($items as $n=>$item){ if(isset($this->$name)){ if($n==$this->$name){ $returnValue=$item; } } } return $returnValue; } view里: 复制代码 代码如下:

  • ASP.NET Gridview与checkbox全选.全不选实现代码 2015-03-29

    ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传. 1. 页面 在onclick事件中 "传自己" <asp:TemplateField HeaderText="全选"> <HeaderTemplate> <input type="checkbox" id="CheckBox1" name="CheckBox1&

  • 绕过外建约束drop table or delete row 2014-11-06

    外键关联表在开发中很常见,经常遇到要删除一个表或者一行数据时因为有外键关联,删除的时候是不能通过的,但我就是想删除,怎么办呢,5.x之后,MySQL提供了一个办法,将外键约束给关闭了: mysql> SET foreign_key_checks = 0; mysql> drop table or delete row... mysql> SET foreign_key_checks = 1; foreign_key_checks设置为0时,外键约束就关闭啦,设置为1外键约束又回来啦!这个

  • asp.net GridView 删除时弹出确认对话框(包括内容提示) 2015-03-29

    GridView 删除时弹出确认对话框(包括内容提示) 效果图: html代码 <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%"> <tr> <th colspan="2"> GridView演

  • Use of asp.net GridView 2010-07-16

    Effect of light sticks In the GridView's RowCreated the incident, which created the event of data lines if (e.Row.RowType == DataControlRowType.DataRow) ( e.Row.Attributes.Add ("onmouseover", "currertcolor = this.style.backgroundColor; this

  • Detailed ASP.NET4 GridView Add 4 large sort style 2010-10-13

    Original published in: http://tech.it168.com/a2010/1012/1112/000001112829.shtml The other with the ASP.NET Web controls one can, Gridview control has many different CSS style property settings, including as CssClass, Font font, ForeColor, BackColor,