簡體   English   中英

在GridView和C#中從數據庫中刪除行

[英]Delete row from database in gridview and c#

我正在使用帶有編輯和刪除按鈕的gridview。

當我在gridview中刪除特定行時,它將被刪除。 但是我再次加載頁面,再次顯示已刪除的行。 我的意思是,行不會在數據庫中刪除。

這是我的代碼:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            if (e.CommandName == "EditRow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
                int index = gr.RowIndex; 
                hiddenfield.Value = index.ToString(); 
                Textid.Text = gr.Cells[1].Text;
                Textusername.Text = gr.Cells[2].Text;
                Textclass.Text = gr.Cells[3].Text;
                Textsection.Text = gr.Cells[4].Text;
                Textaddress.Text = gr.Cells[5].Text;
            }
            else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");

            }
        }

和asp文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True" 
        EnablePersistedSelection="True" BackColor="White" EnableViewState="true" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
            <asp:BoundField DataField="Section" HeaderText="Section" 
                SortExpression="Section" />
            <asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
            <asp:TemplateField HeaderText="Edit">
               <ItemTemplate>
                 <asp:Button runat="server" ID="btnedit" Text="Edit" CommandName="EditRow"></asp:Button>                    
               </ItemTemplate>
                <ControlStyle BorderColor="#CCFF66" />
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Delete">
          <ItemTemplate>
                 <asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Deleterow"></asp:Button>                    
               </ItemTemplate>
          </asp:TemplateField>            
        </Columns>
        <SelectedRowStyle BackColor="#FF66FF" />
    </asp:GridView>

存儲過程:

ALTER PROCEDURE StoredProcedure4
    (
    @id int 
)
AS
begin
Delete from Student where id=@id
End

我是.net的新手。 誰能幫我解決這個問題?

謝謝,

傳遞參數無效@id更改@ID

ALTER PROCEDURE StoredProcedure4
(
    @ID as int=0 
)
AS
begin
Delete from Student where id=@ID
End

另一種方式可能是

使用Commandargument的簡短答案 Pass ID

完整答案

更換

<asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Deleterow"></asp:Button>

通過

<asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Eval("ID") %>' CommandName="Deleterow"></asp:Button>

在aspx和cs中

更換

       else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");

            }

通過

     else if (e.CommandName == "Deleterow")
            {     
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", Convert.ToInt32(e.CommandArgument));
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");

            }

在刪除按鈕中將Id作為命令參數傳遞,並如下所示訪問后面的代碼。

 <asp:TemplateField HeaderText="Delete">
      <ItemTemplate>
             <asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Eval("Id") %>' CommandName="Deleterow"></asp:Button>                    
           </ItemTemplate>
      </asp:TemplateField>


else if (e.CommandName == "Deleterow")
        {     
            SqlCommand com = new SqlCommand("StoredProcedure4", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@id", Convert.ToInt32(e.CommandArgument));
            var id = Int32.Parse(e.CommandArgument.ToString());                
            GridView1.Rows[id].Visible = false;
            com.ExecuteNonQuery();
            Response.Redirect("studententry.aspx");

        }                                               

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM