Multiple Deletes in Gridview

M

MasterChief

I have a customer that likes the gridview but instead of using the
standard Delete command for each row he wants to be able to put a
checkbox next to the row so he can chose multiple records and then
chose a delete button up top. What is the best way to accomplish that.
I have made a template in the gridview and added a checkbox. Then I
made a command button and changed is CommandName to Delete but after
that I don't really know what to do next.
 
S

S. Justin Gengo [MCP]

MasterChief,

Inside of the delete handler loop through the rows of the grid and look for
each checkbox. Then if the box is checked run a delete command based on that
row's id. Make certain that you set the GridView's DataKeyNames property to
the primary key field of your database so you can retrieve the row's id:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="pk_InventoryId">


Dim CheckBox As CheckBox
Dim KeyId As Int32

For Each GridViewRow As GridViewRow In GridView1.Rows
'---Look at each checkbox here
CheckBox = CType(GridViewRow.FindControl("MyCheckBox"), CheckBox)

If CheckBox.Selected
'---Get the database id
KeyId = CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value,
Int32)

'---Use the KeytId to delete from the database.
End If
Next

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
G

George Ter-Saakov

Actually in cases like that the simplest way is to fall back to regular ASP
style.
Meaning just add checkbox (not a server control) to the column and give them
name like
"chkDelete" + RecordId ( concatenate with a recordId)

then in you handler for the button you can have following code

foreach( string sKey in Request.Form )
{
if( sKey.IndexOf("chkDelete") == 1 )
{
......
}
}


George.
 
M

MasterChief

I see how that works but my next question is about the delete code.
What is the best way to connect to the database and do a delete in the
code behind file?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top