开发者

All Data have checkbox on Gridview for Delete

I have a one Gridview (Gridview1) and one Button (Delete). And AVUKAT table (Columns--> HESAP, MUSTERI, AVUKAT)

My Gridview code is

<asp:GridView ID="GridView1" 
            runat="server" AutoGenerateColumns="False" 
            CellPadding="4" DataSourceID="GridviewDataSource" ForeColor="#333333" 
            GridLines="None" Width="329px" AllowSorting="True" >

My DataSource code is

<asp:SqlDataSource ID="GridviewDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SqlServerCstr %>"开发者_JAVA百科 
            SelectCommand="SELECT * FROM [AVUKAT] ORDER BY [MUSTERI]">

And one Button (Delete)

What i want is, when i show the data in Gridview all data have a checkbox (Like this picture)

All Data have checkbox on Gridview for Delete

And then, When i click Delete Button, deleting all checked datas on gridview and table.

How can i do that?

Best Regards, Soner


Here is an article that answers exactly what you're looking for and is complete with full code samples: http://csharpdotnetfreak.blogspot.com/2009/04/delete-multiple-rows-gridview-checkbox.html

The magic happens when you click the Delete button. The code loops through the gridview and checks each row for a checked checkbox. When it finds a checked checkbox, it stores the ID of the row in a stringcollection. Once the entire GridView has been scanned, it then deletes all IDs in the stringcollection.

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


Use a TemplateField, for the ItemTemplate, put there a server-side checkbox. When clicking delete, loop through the GridView.Rows collection, for item or alternate item rows, then find the checkbox in the first cell, and see if its checked, then delete if it is.

HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜