[英]All Data have checkbox on Gridview for Delete
我有一個Gridview
(Gridview1)和一個Button
(Delete)。 和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和表上的所有選中數據。
我怎樣才能做到這一點?
最好的問候,Soner
這是一篇可以准確回答您所尋找內容的文章,並附有完整的代碼示例: http : //csharpdotnetfreak.blogspot.com/2009/04/delete-multiple-rows-gridview-checkbox.html
當您單擊“刪除”按鈕時,魔術就發生了。 該代碼循環遍歷gridview並檢查每一行是否有選中的復選框。 找到選中的復選框后,會將行的ID存儲在stringcollection中。 掃描完整個GridView后,它將刪除stringcollection中的所有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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.