Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi..

I am populating my gridview as follows:-

C#
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??
Posted
Updated 27-Dec-11 20:24pm
v2

 
Share this answer
 
Comments
bhagyap 28-Dec-11 3:00am    
Hi..

I tried the same but yet unable to delete checked rows..

Please guide me..
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.
 
Share this answer
 
Comments
bhagyap 28-Dec-11 3:00am    
Hi..

I tried the same but yet unable to delete checked rows..

Please guide me..
Hi Bhagya

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

C#
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();


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
 
Share this answer
 
Comments
bhagyap 28-Dec-11 3:18am    
I am trying with the following code but unable to delete rows,help me..

protected void Button2_Click(object sender, EventArgs e)
{


foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox CheckBox1 = (CheckBox)row.Cells[0].FindControl("CheckBox1");

if (CheckBox1 != null)
{
if (CheckBox1.Checked)
{

int id = GridView1.SelectedIndex;
GridView1.DeleteRow(id);
}
}
}
}
string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sda = new SqlDataAdapter("Select * from DropDownFilter", con);
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

Am getting server error as follows:-

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Suman Zalodiya 28-Dec-11 3:36am    
int id=GridView1.SelectedIndex won't work in this scenarion.

Please use the code which i have mentioned above
bhagyap 28-Dec-11 4:33am    
I am still getting the same error..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900