从gridview和c#中删除数据库中的行

时间:2015-11-05 05:05:06

标签: c# asp.net gridview

我使用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");

            }
        }

和asps文件:

<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>

StoredProcedure的:

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

我是.net的新手。任何人都可以帮我解决这个问题吗?

谢谢,

3 个答案:

答案 0 :(得分:0)

传递参数无效@id更改@ID

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

答案 1 :(得分:0)

替代方式可能是,

简短回答 使用命令参数传递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");

            }

答案 2 :(得分:0)

在删除按钮中将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");

        }