|
|
 |
| SQL Server 2005 General discussion of SQL Server *2005* version only. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2005 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

December 6th, 2007, 02:49 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Location: new delhi, new delhi, India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Deleting multiple rows in data view or grid
<script language="javascript" type="text/javascript">
function select_deselectAll (chkVal, idVal)
{
var frm = document.forms[0];
// Loop through all elements
for (i=0; i<frm.length; i++)
{
// Look for our Header Template's Checkbox
if (idVal.indexOf ('CheckAll') != -1)
{
// Check if main checkbox is checked, then select or deselect datagrid checkboxes
if(chkVal == true)
{
frm.elements[i].checked = true;
}
else
{
frm.elements[i].checked = false;
}
// Work here with the Item Template's multiple checkboxes
}
else if (idVal.indexOf ('DeleteThis') != -1)
{
// Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
if(frm.elements[i].checked == false)
{
frm.elements[1].checked = false; //Uncheck main select all checkbox
}
}
}
}
function confirmDelete (frm)
{
// loop through all elements
for (i=0; i<frm.length; i++)
{
// Look for our checkboxes only
if (frm.elements[i].name.indexOf("DeleteThis") !=-1)
{
// If any are checked then confirm alert, otherwise nothing happens
if(frm.elements[i].checked)
{
return confirm ('Are you sure you want to delete your selection(s)?')
}
}
}
}
</script>
<asp:GridView ID="DgAddress" runat="server" PageSize="40" Width="278px" AutoGenerateColumns="False">
<PagerStyle BackColor="#C000C0" />
<AlternatingRowStyle BackColor="#C0C0FF" BorderColor="#C0C0FF" BorderStyle="Solid"
ForeColor="#8080FF" />
<EmptyDataTemplate>
<asp:Button ID="BtnGrid" runat="server" Text="Delete"/>
</EmptyDataTemplate>
<Columns>
<asp:BoundField HeaderText="ID" DataField="Id" Visible="false" />
<asp:BoundField HeaderText="Employee Id" DataField="EmpId"/>
<asp:BoundField HeaderText="Employee Name" DataField="EmpName"/>
<asp:CommandField InsertText="Delete" ShowDeleteButton="True"/>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox Id="CheckAll" onClick="javascript: return select_deselectAll (this.checked, this.id);" runat=server/>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="chkid" runat="server" Value=<%#Container.DataItem("Id")%>/>
<asp:CheckBox Id="DeleteThis" onClick="javascript: return select_deselectAll (this.checked, this.id);" runat=server/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Delete Items" ID="Confirm" runat="server" />
Protected Sub Confirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Confirm.Click
Dim dgIDs As String = ""
Dim BxsChkd As Boolean
Dim i As Integer
Dim deleteChkBxItem As CheckBox
Dim sqlconn As New SqlConnection("Initial Catalog=master;Data Source=tcs139054;user id=sa;password=kolkata@1")
BxsChkd = False
i = 2
For Each gv As GridViewRow In DgAddress.Rows
deleteChkBxItem = gv.FindControl("DeleteThis")
If deleteChkBxItem.Checked = True Then
BxsChkd = True
dgIDs += CType(gv.FindControl("chkid"), HiddenField).Value.ToString + ","
End If
i += 1
Next
If BxsChkd Then
Try
Dim deleteSQL As String = "delete from employee where Id IN (" & dgIDs.Substring(0, dgIDs.LastIndexOf(",")) & ")"
Dim cmd As SqlCommand = New SqlCommand(deleteSQL, sqlconn)
sqlconn.Open()
cmd.ExecuteNonQuery()
FillGrid()
Catch Err As SqlException
Response.Write(Err.Message.ToString)
Finally
sqlconn.Close()
End Try
End If
End Sub
Private Sub FillGrid()
Dim sqlconn As New SqlConnection("Initial Catalog=master;Data Source=tcs139054;user id=sa;password=kolkata@1")
Dim sqlcmd As New SqlCommand
Dim sqldr As SqlDataReader
sqlconn.Open()
sqlcmd.Connection = sqlconn
sqlcmd.CommandType = Data.CommandType.Text
sqlcmd.CommandText = "select id,empid,empname from employee"
sqldr = sqlcmd.ExecuteReader
DgAddress.DataSource = sqldr
DgAddress.DataBind()
sqldr.Close()
sqlconn.Close()
End Sub
Deepak Kumar Sharma
PGMS Inc(Noida)(India)
Cell No:-9719395072/9313107703
__________________
Deepak Kumar Sharma
PGMS Inc(Noida)(India)
Cell No:-9719395072/9313107703
|

December 7th, 2007, 02:19 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2006
Location: Bangalore, Karnataka, India.
Posts: 232
Thanks: 0
Thanked 1 Time in 1 Post
|
|
is it is your solution or you have some doubt so that i should read code.
thanks......
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |