如何使用c#asp.net代码删除gridview和数据库中的行? [英] How to delete row in a gridview and database also using c# asp.net code?
问题描述
您好,
如何使用c#asp.net代码删除gridview和数据库中的行?
我不使用向导或SqlDataSource,
我想删除行,我的StoredProcedure。
图像按钮没有使用OnDeleting命令触发gridview。
我们如何删除数据库和Gridview中的行,请帮助我。
我的以下代码是: -
< asp:GridView ID = GvMyCart runat = server CssClass < span class =code-keyword> = dataTable
< span class =code-attribute> AutoGenerateColumns = False RowStyle-CssClass = odd_row
AlternatingRowStyle-CssClass = even_row
onrowdatabound = GvMyCart_RowDataBound ShowFooter = True
OnRowDeleting = GvMyCart_RowDeleting onrowcommand = GvMyCart_RowCommand >
< AlternatingRowStyle CssClass = even_row / >
< 列 >
< < span class =code-leadattribute> asp:TemplateField HeaderStyle-CssClass = dataTableHeader >
< span class =code-keyword>< ItemTemplate >
< asp:ImageButton runat = server ID = IgmBtnDeleteCard
ImageUrl = 〜/ images / remove-icon.png CommandName = 删除 CommandArgument =' <% #Eval( CardID) %> ' >
< / asp:ImageButton >
< / ItemTemplate >
< Heade rStyle CssClass = dataTableHeader / >
< / asp:TemplateField < span class =code-keyword>>
< asp:TemplateField HeaderStyle-CssClass = < span class =code-keyword> dataTableHeader HeaderText = S.No。 >
< span class =code-keyword>< ItemTemplate >
<%#((GridViewRow)容器).RowIndex + 1 %>
< / ItemTemplate >
< HeaderStyle CssClass = dataTableHeader < span class =code-keyword> / >
< / asp:TemplateField >
亲tected void GvMyCart_RowCommand( object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == 删除)
{
var id = Int32 .Parse(e。 CommandArgument.ToString());
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd1 = new SqlCommand( procdeleteCard ,con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add( @ CardID,SqlDbType.Int).Value = Convert.ToInt32(ID);
cmd1.ExecuteNonQuery();
con.Close();
BindGrid();
// 使用id删除代码
// 重新绑定GridView
}
}
请帮帮我。
提前致谢。
Ankit Agarwal
Web开发人员
void GvMyCart_RowCommand( Object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == 删除)
{
var id = Int32 .Parse(e.CommandArgument);
GVDetail.DeleteRow(id);
// 绑定你的网格,它必须
}
}
我可以看到一个问题,RowCommand
将不会触发。活动名称区分大小写。
您有...
<前lang =HTML> onrowcommand =GvMyCart_RowCommand
但它应该是..
OnRowCommand =GvMyCart_RowCommand<源块视图中的/ blockquote>
< asp:gridview id = GvMyCart runat = server cssclass = dataTable xmlns:asp = #unknown >
AutoGenerateColumns = 错误 ---------------
--------- -----------------
OnRowDeleting = GvMyCart_RowDeleting 跨度> >
< asp:imagebutton runat = server id = IgmBtnDeleteCard >
ImageUrl = 〜/ images / remove-icon.png CommandName = 删除 >
< / asp:imagebutton > ;
< / asp:gridview >
并在GvMyCart_RowDeleting事件中写下您的逻辑
Hello,
How to delete row in a gridview and database also using c# asp.net code?
I am not using wizard or SqlDataSource,
I want to delete row, my StoredProcedure.
Image Button not firing of gridview using OnDeleting Command.
How can we delete row from database and Gridview, Please Help Me.
My Following Code Is:-
<asp:GridView ID="GvMyCart" runat="server" CssClass="dataTable" AutoGenerateColumns="False" RowStyle-CssClass="odd_row" AlternatingRowStyle-CssClass="even_row" onrowdatabound="GvMyCart_RowDataBound" ShowFooter="True" OnRowDeleting="GvMyCart_RowDeleting" onrowcommand="GvMyCart_RowCommand"> <AlternatingRowStyle CssClass="even_row" /> <Columns> <asp:TemplateField HeaderStyle-CssClass="dataTableHeader"> <ItemTemplate> <asp:ImageButton runat="server" ID="IgmBtnDeleteCard" ImageUrl="~/images/remove-icon.png" CommandName="Delete" CommandArgument='<%#Eval("CardID")%>'> </asp:ImageButton> </ItemTemplate> <HeaderStyle CssClass="dataTableHeader" /> </asp:TemplateField> <asp:TemplateField HeaderStyle-CssClass="dataTableHeader" HeaderText="S.No."> <ItemTemplate> <%# ((GridViewRow)Container).RowIndex + 1%> </ItemTemplate> <HeaderStyle CssClass="dataTableHeader" /> </asp:TemplateField>
protected void GvMyCart_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Delete") { var id = Int32.Parse(e.CommandArgument.ToString()); SqlConnection con = new SqlConnection(connectionString); con.Open(); SqlCommand cmd1 = new SqlCommand("procdeleteCard", con); cmd1.CommandType = CommandType.StoredProcedure; cmd1.Parameters.Add("@CardID", SqlDbType.Int).Value = Convert.ToInt32(id); cmd1.ExecuteNonQuery(); con.Close(); BindGrid(); //Delete Code using the id //Rebind GridView } }
Please help me.
Thanks in Advance.
Ankit Agarwal
Web Developer解决方案void GvMyCart_RowCommand(Object sender, GridViewCommandEventArgs e) { if(e.CommandName=="Remove") { var id = Int32.Parse(e.CommandArgument); GVDetail.DeleteRow(id); //bind your Grid ,it must } }
I can see one problem for whichRowCommand
won't fire. Name of the event is case sensitive.
You have...
onrowcommand="GvMyCart_RowCommand"
But it should be..
OnRowCommand="GvMyCart_RowCommand"
in source view
<asp:gridview id="GvMyCart" runat="server" cssclass="dataTable" xmlns:asp="#unknown"> AutoGenerateColumns="False" --------------- -------------------------- OnRowDeleting="GvMyCart_RowDeleting" > <asp:imagebutton runat="server" id="IgmBtnDeleteCard"> ImageUrl="~/images/remove-icon.png" CommandName="Delete" > </asp:imagebutton> </asp:gridview>
and write your logic in GvMyCart_RowDeleting event
这篇关于如何使用c#asp.net代码删除gridview和数据库中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!