简体   繁体   English

我有 gridview 从 sql 服务器获取数据,我有操作按钮编辑删除更新和取消

[英]I have gridview which fetches data from sql server, i have action buttons edit delete update and cancel

I have a gridview which fetches data from SQL server, I have action buttons edit delete update and cancel.我有一个 gridview 从 SQL 服务器获取数据,我有操作按钮编辑删除更新和取消。

I have a code but I don't know how to implement iti.e click events for the edit delete cancel update operations.我有一个代码,但我不知道如何为编辑删除取消更新操作实现 iti.e 点击事件。

GRidView 屏幕截图

asp code

<Columns>
<asp:BoundField DataField="group" HeaderText="group" SortExpression="group" />
<asp:BoundField DataField="key" HeaderText="key" SortExpression="key" />
<asp:BoundField DataField="label" HeaderText="label" SortExpression="label" />
<asp:CommandField ButtonType="Button" CancelText="" HeaderText="Action" ShowDeleteButton="True" ShowEditButton="True" />
</Columns>

code behind C# C# 后面的代码

 protected void GridView_RowEditing1(object sender, GridViewEditEventArgs e)
    {
        GridView.EditIndex = e.NewEditIndex;
        BindGrid();
    }

    protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        SqlConnection con = new SqlConnection(connectionString);
        GridViewRow row = (GridViewRow)GridView.Rows[e.RowIndex];
        Label lbldeleteid = (Label)row.FindControl("lblID");
        con.Open();
        SqlCommand cmd = new SqlCommand("delete FROM detail where id='" + Convert.ToInt32(GridView.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        BindGrid();
    }

    protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection con = new SqlConnection(connectionString);

        int Config_id = Convert.ToInt32(GridView.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        //TextBox txtname=(TextBox)gr.cell[].control[];  
        TextBox group = (TextBox)row.Cells[0].Controls[0];
        TextBox key = (TextBox)row.Cells[1].Controls[0];
        TextBox label = (TextBox)row.Cells[2].Controls[0];
        //TextBox textadd = (TextBox)row.FindControl("txtadd");  
        //TextBox textc = (TextBox)row.FindControl("txtc");  
        GridView.EditIndex = -1;
        con.Open();
        //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);  
        SqlCommand cmd = new SqlCommand("update detail set group='" + group.Text + "',key='" + key.Text + "',label ='" + label.Text + "'where id='" + Config_id + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        BindGrid();
        //GridView1.DataBind(); 
    }

    protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView.EditIndex = -1;
        BindGrid();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 GridView 中的数据保存到 SQL Server 2005? 如何在 Gridview 中编辑和删除行? - How to Save the data from a GridView to SQL Server 2005? How to Edit and Delete rows in the Gridview? 我的表中只有2条记录,但它从数据库中获取4条记录 - I have only 2 records in my table but it fetches 4 records from database 我有一个datagrid视图,该视图将数据保存在数据库中,但是删除和更新不起作用 - I have a datagrid view which saves data in the database but the delete and update not working 如何将动作按钮(例如编辑和删除)添加到动态gridview - How to add action buttons like edit and delete to dynamic gridview 我有一个简单的gridview,它不显示数据 - I have a simple gridview, it's not displaying data 带有Sqldatasource编辑/删除按钮的Gridview - Gridview with Sqldatasource Edit/Delete buttons 如何从两个有关系的表中删除数据? - How can I delete data from two tables that have a relationship? 所有数据在Gridview上具有复选框以供删除 - All Data have checkbox on Gridview for Delete 我有一个数据表,其中有一个带有十进制值的列 - I have a Data Table in which i have a column with decimal values 我必须从sql server 2008数据库中检索一个值 - I have to retrieve a value from sql server 2008 database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM