删除记录时如何刷新gridview? [英] How refresh gridview when record is deleted??

查看:136
本文介绍了删除记录时如何刷新gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个gridview并且也包含复选框字段.如果单击复选框然后单击删除按钮,则删除相关记录.此删除功能正常工作.
-------------
问题
即使删除了记录,它也不会在gridview中受影响.已删除的记录仍然保留在gridview中.应该如何解决该问题?

谢谢.

--------------
受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!Page.IsPostBack)
{
var getAllContacts =来自dcon.contact_contactInfos中的c
选择新建{c.fName,c.lastName,c.title,c.accountName,c.officePhone,c.assignTo};

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

}


受保护的void deleteContact_Click(对象发送者,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();

}
}

}
}


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

}
}

}
}

推荐答案

重新绑定您在Page_Load中调用的Gridview.使此代码块成为通用过程并调用所需的位置,例如编辑后",删除后",新建后".
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.


我创建了一个名为bindGrid()的方法,并在页面中对其进行了调用加载并删除.

显而易见,当您删除记录后,意味着您的集合和数据库已更改,因此最好总是再次获取数据,这同样适用于更新和插入记录...


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...


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();
}
}
}
}


这篇关于删除记录时如何刷新gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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