本文介绍了从Gridview删除多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

我正在如下填充我的gridview:-

Hi..

I am populating my gridview as follows:-

protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlDataAdapter sda = new SqlDataAdapter("Select * from DropDownFilter", con);
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }



然后我现在每行都有一个复选框,我想在另一个按钮事件中删除选中的行,我该如何实现呢?

谁能帮我吗?



and then i have checkbox for every row now i want to delete the checked rows on another button event,how can i achieve this??

Can anyone please help me??

推荐答案



try
        {
             //Check for CheckBox in everyrow of Gridview
            foreach (GridViewRow row in gvDeleteUser.Rows)
            {
                CheckBox chkbox = (CheckBox)row.FindControl("chkDeleteUser");
                //Check if the CheckBox for perticular row is Checked then delete it.
                if (chkbox.Checked)
                {
                    //Fetch the Primary Key value for that Row and pass to delet eit.
                    int uid = (int)gvDeleteUser.DataKeys[row.RowIndex].Value;

                    //Write here logic of the delet euser
                    //For example call the stored procedure with parameter as uid
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        //once you delete the all data load gridview again to reflect the deleted users
        loadGriedData();



请不要忘记在网格视图中添加DataKeyName ="PrimaryKeyField"属性,如

< asp:gridview id ="Gridview1" run =服务器" datakeynames ="CustID" xmlns:asp =#unknown">

请检查一下,如有任何疑问,请通知我.

如果解决了,请使其解决.

感谢与问候
苏曼·扎洛迪亚(Suman Zalodiya)



Please don''t forget to add the DataKeyName="PrimaryKeyField" property in the grid view like

<asp:gridview id="Gridview1" run="server" datakeynames="CustID" xmlns:asp="#unknown">

Please check it and let me know in case of any concerns.

And please make it as resolved if it resolves.

Thanks and Regards
Suman Zalodiya


这篇关于从Gridview删除多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:47