Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have a gridview and which contain checkbox field too.If the checkbox is clicked and then delete button click, relevent record is deleted.This delete function is working properly.
-------------
problem
Even though record is deleted,it does not affected in gridview.Deleted record is still remaining in the gridview.What will be the way that should I follow solve this problem?

Thank you.

--------------
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var getAllContacts = from c in dcon.contact_contactInfos
select new{ c.fName,c.lastName,c.title,c.accountName,c.officePhone,c.assignTo};

GridView1.DataSource = getAllContacts;
GridView1.DataBind();
}

}


protected void deleteContact_Click(object sender, EventArgs e)
{
for (int i = 0; i<gridview1.rows.count; i++)
="" {
="" checkbox="" cb="(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
" if="" ((cb="" !="null)&&(cb.Checked))
" var="" getallcontacts="from" c="" in="" dcon.contact_contactinfos
="" select="" new="" {="" c.cid="" };

="" foreach="" (var="" item="" getallcontacts)
="" deletecontactaddress="con.contact_contactInfos.Single(contact" =="">contact.cid == item.cid);
dcon.contact_contactInfos.DeleteOnSubmit(deleteContactAddress);
dcon.SubmitChanges();

}
}

}
}
Posted

Rebind the Gridview like which you have called in Page_Load. Make this code block as generic procedure & call that which places you want such as After Edit, After Delete, After New.
 
Share this answer
 
i have created a method named bindGrid() and called it in page load and on delete.

It is all clear that when u have deleted the record that means your collection and database has changed so it is always better to fetch the data again same applies while updating and inserting the records...


C#
protected void bindGrid()
{
var getAllContacts = from c in dcon.contact_contactInfos
select new{ c.fName,c.lastName,c.title,c.accountName,c.officePhone,c.assignTo};
GridView1.DataSource = getAllContacts;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindGrid();

}
}

protected void deleteContact_Click(object sender, EventArgs e)
{
for (int i = 0; i
{
CheckBox cb=(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if ((cb != null)&&(cb.Checked))
{
var getAllContacts = from c in dcon.contact_contactInfos
select new { c.cid };
foreach (var item in getAllContacts)
{
var deleteContactAddress = con.contact_contactInfos.Single(contact =>contact.cid == item.cid);
dcon.contact_contactInfos.DeleteOnSubmit(deleteContactAddress);
dcon.SubmitChanges();
bindGrid();
}
}
}
}
 
Share this answer
 
v2

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