所有数据有删除
问题描述:
在GridView的复选框我有一个Gridview
(Gridview1)和一个Button
(删除)。 而AVUKAT table
(列 - > HESAP,MUSTERI,AVUKAT)所有数据有删除
我Gridview
代码
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="GridviewDataSource" ForeColor="#333333"
GridLines="None" Width="329px" AllowSorting="True" >
我DataSource
代码
<asp:SqlDataSource ID="GridviewDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlServerCstr %>"
SelectCommand="SELECT * FROM [AVUKAT] ORDER BY [MUSTERI]">
和一个按钮(删除)
我想要的是,当我在GridView中显示数据时,所有数据都有一个复选框(如此图片)
然后,当我点击Delete Button
,删除所有检查的GridView和表DATAS。
我该怎么做?
最好的问候, Soner
答
下面是一篇文章,回答你在寻找什么,并配有完整的代码样本:当你点击删除按钮http://csharpdotnetfreak.blogspot.com/2009/04/delete-multiple-rows-gridview-checkbox.html
的奇迹发生。代码循环遍历gridview并检查每一行的复选框。当它找到一个复选框时,它将该行的ID存储在一个字符串集合中。一旦整个GridView被扫描完毕,它就会删除字符串集合中的所有ID。
protected void btnDelete_Click(object sender, EventArgs e)
{
//Create String Collection to store
//IDs of records to be deleted
StringCollection idCollection = new StringCollection();
string strID = string.Empty;
//Loop through GridView rows to find checked rows
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)
GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
idCollection.Add(strID);
}
}
}
//Call the method to Delete records
DeleteMultipleRecords(idCollection);
// rebind the GridView
GridView1.DataBind();
}
答
使用一个TemplateField,为ItemTemplate中,放在那里服务器端复选框。单击删除时,循环访问GridView.Rows集合中的项目或备用项目行,然后查找第一个单元格中的复选框,并查看它是否已选中,然后是否删除。
HTH。
看看你的项目代码在你提供的图像的页面。 – TheGeekYouNeed 2011-01-27 14:41:54