从Gridview删除多行 [英] Delete Multiple Rows From Gridview

查看:94
本文介绍了从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??

推荐答案

请参阅此处
deleting-multiple-rows-in-gridview- using-checkbox
See here
deleting-multiple-rows-in-gridview-using-checkbox


,您需要使用 .FindControl()方法的帮助.在gridview中查找复选框并检查它是否被选中",然后删除具有任何唯一值的该行.
参见 [ ^ ]链接.
you need to take help of .FindControl() method. to find checkboxes in gridview and check if it "checked" then delete that row with any unique value.
see this[^] link.


嗨Bhagya

假设GridView的CheckBox的ID为chkDeleteUser,然后使用填充代码从GridView删除行.
Hi Bhagya

Suppose ID of your CheckBox of your GridView is chkDeleteUser then use the fillowing code to delete the Rows from GridView

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删除多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆