如何从gridview删除行. [英] how to delete the row from gridview.

查看:84
本文介绍了如何从gridview删除行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从asp.net c#中的gridview中删除行. ======================================

亲爱的朋友,

我正在使用sql数据源在gridview中显示数据.也是从gridview的左侧,我具有从网格属性中手动选择的编辑和删除链接.

记录正在完美删除....但是我需要使用Javascript的确认消息...已成功删除记录


请帮我解决这个问题,

谢谢.

how to delete the row from gridview in asp.net c#.
========================================

Dear Frnds,

Am displaying data in gridview by using sql datasource. and also from left side of gridview i have edit and delete links manually selected from grid properties.

Records are Deleting Perfectly.......But i need Confirmation Message using Javascript...that RECORDS Deleted successfully


Please help me this problem,

Thanks.

推荐答案

请参考:
如何在带有确认的GridView中实现行删除.
带有确认的GridView删除 [使用GridView控件更新,删除记录 [
Please refer:
How to implement row deletion in GridView, with confirmation.
GridView Delete, with Confirmation[^]

Updating, Deleting records using GridView control[^]


Delete.aspx

Delete.aspx

<div>
        <asp:gridview id="GridView1" runat="server" onrowdeleting="GridView1_RowDeleting" xmlns:asp="#unknown">
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            Style="margin-bottom: 0px" AutoGenerateColumns="False" ForeColor="#CC0099" GridLines="Vertical">
            <columns>
                <asp:templatefield headertext="Username">
                    <itemtemplate>
                        <asp:label id="lblName" runat="server" text="<%#Bind("username")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtusername" runat="server" text="<%#Bind("username")%>"></asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Type">
                    <itemtemplate>
                        <asp:label id="lblfiletype" runat="server" text="<%#Bind("filetype")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtfiletype" runat="server" text="<%#Bind("filetype")%>"></asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Path">
                    <itemtemplate>
                        <asp:label id="lblfilepath" runat="server" text="<%#Bind("filepath")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtfilepath" runat="server" text="<%#Bind("filepath")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Name">
                    <itemtemplate>
                        <asp:label id="lblfilename_upload" runat="server" text="<%#Bind("filename_upload")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtfilename_upload" runat="server" text="<%#Bind("filename_upload")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="Creation Date">
                    <itemtemplate>
                        <asp:label id="lblcreatedon" runat="server" text="<%#Bind("createdon")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtcreatedon" runat="server" text="<%#Bind("createdon")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="File Size">
                    <itemtemplate>
                        <asp:label id="lblsize" runat="server" text="<%#Bind("size")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:textbox id="txtsize" runat="server" text="<%#Bind("size")%>">
                        </asp:textbox>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="ID">
                    <itemtemplate>
                        <asp:label id="lblProductid" runat="server" visible="true" text="<%#Bind("ID")%>"></asp:label>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:label id="lblProductid" runat="server" visible="false" text="<%#Bind("ID")%>">
                        </asp:label>
                    </edititemtemplate>
                </asp:templatefield>
                <asp:templatefield itemstyle-width="50px" headertext="Edit">
                    <itemtemplate>
                        <asp:linkbutton id="lbtnedit" runat="server" forecolor="#0C4375" text="Edit" commandname="Edit"></asp:linkbutton>
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:linkbutton id="lbtnupdate" runat="server" forecolor="#0C4375" text="Update">
                            CommandName="Update"></asp:linkbutton>
                        <asp:linkbutton id="lbtncancel" runat="server" forecolor="#0C4375" text="Cancel">
                            CommandName="Cancel"></asp:linkbutton>
                    </edititemtemplate>
                    <itemstyle width="50px"></itemstyle>
                </asp:templatefield>
                <asp:templatefield itemstyle-width="50px" headertext="Delete">
                    <itemtemplate>
                        <asp:linkbutton id="lbtnDelete" runat="server" forecolor="#0C4375" text="Delete">
                            CommandName="Delete"></asp:linkbutton>
                    </itemtemplate>
                    <itemstyle horizontalalign="Left" wrap="True" />
                    <controlstyle backcolor="#EAF6FE" />
                </asp:templatefield>
            </columns>
            <footerstyle forecolor="#CC66FF" />
            <HeaderStyle Font-Italic="True" ForeColor="#FF3300" />
        </asp:gridview>
        <table>
        </table>
    </div>



delete.cs:




delete.cs:


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       Label lblProductid = GridView1.Rows[e.RowIndex].FindControl("lblProductid") as Label;
       int prodcutid = Convert.ToInt32(lblProductid.Text);
       query = "delete from attachment where ID= " + prodcutid + " ";
       setConnectionString();
       Message = EmpBal.binddeleteuserBAL(query, conString);
       if (Message == "success")
       {
           Bind();
       }

   }


}


}


将按钮设为模板字段
Make a button as template field
<asp:templatefield xmlns:asp="#unknown">
                 <itemtemplate>
                 <asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return <b>confirm('Are You sure to delete this entry')</b>" text="Delete">CommandArgument='<%#Eval("id")%>' ></asp:linkbutton>
                 </itemtemplate>
                 
                 </asp:templatefield>



并且gridview的行命令事件用作



and on row command event of gridview use as

  protected void rowcmd(object sender, GridViewCommandEventArgs e)
    {
    
        try
        {
           
            Int32 id = Convert.ToInt32(e.CommandArgument);
            string comm = "Delete from tblComments where id=@id";
            SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
            cmd.Parameters.AddWithValue("id", id);
            cmd.ExecuteNonQuery();
             GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

    }
}


这篇关于如何从gridview删除行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆