gridview删除

 

前台:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID"
                OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_CancelingEdit"
                OnRowDeleting="GridView1_RowDeleting" PageSize="15" AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging"
                ShowHeader="true">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label runat="server" Text="ID"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" Text=‘<%#Bind("ID") %>‘ ID="Label0"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label runat="server" Text="姓名"></asp:Label></HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" Text=‘<%#Bind("UserName") %>‘ ID="Label2"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox Text=‘<%#Bind("UserName") %>‘ runat="server" ID="UserName"></asp:TextBox><asp:RequiredFieldValidator
                                runat="server" ErrorMessage="不能为空" ControlToValidate="UserName" Display="Dynamic">*</asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label runat="server" Text="金额"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label runat="server" Text=‘<%#Bind("Money") %>‘ ID="Label33"></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox Text=‘<%#Bind("Money")%>‘ runat="server" ID="Money"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="不能为空"
                                ControlToValidate="Money" Display="Dynamic">*</asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="整数或浮点数"
                                ValidationExpression="^\d*$|[1-9]\d*\.\d*|0\.\d*[1-9]\d*$" ControlToValidate="Money"
                                Display="Dynamic">*</asp:RegularExpressionValidator>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
                </Columns>
                <HeaderStyle BackColor="blue" ForeColor="white" />
            </asp:GridView>

后台代码:
string myConnectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
    SqlConnection myConnection = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection.ConnectionString = myConnectionString;
        if (!IsPostBack)
        {
            T();
        }
    }

    public void T()
    {
        myConnection.Open();
        string sql = "Select * From Test";
        SqlDataAdapter da = new SqlDataAdapter(sql, myConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "Test");
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "ID" };
        GridView1.DataBind();
        myConnection.Close();
    }

    public void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        myConnection.Open();
        GridViewRow row = GridView1.Rows[e.RowIndex];
        int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        string userName = ((TextBox)row.FindControl("UserName")).Text.ToString();
        double Money = Convert.ToDouble(((TextBox)row.FindControl("Money")).Text.ToString());
        SqlDataReader dr;
        string sql = "Update Test Set UserName=‘" + userName + "‘,Money=" + Money + " Where ID=" + ID + "";
        SqlCommand myCommand = new SqlCommand(sql, myConnection);
        dr = myCommand.ExecuteReader();
        GridView1.EditIndex = -1;
        myConnection.Close();
        T();
    }

    public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        myConnection.Open();
        GridViewRow row = GridView1.Rows[e.RowIndex];
        int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        SqlDataReader dr;
        string sql = "Delete From Test Where ID=" + ID + "";
        SqlCommand myCommand = new SqlCommand(sql, myConnection);
        dr = myCommand.ExecuteReader();
        GridView1.EditIndex = -1;
        myConnection.Close();
        T();
    }

    public void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        T();
    }

    public void GridView1_CancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        T();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        T();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值