Search This Blog

Thursday, March 12, 2015

Multiple Row Delete in GridView using SharePoint List Data





<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DMRGridViewUserControl.ascx.cs" Inherits="EUDGridView.DMRGridView.DMRGridViewUserControl" %>
<script type="text/javascript" language="javascript">
    function DeleteConfirmation() {
        if (confirm("Are you sure, you want to delete selected records ?") == true)
            return true;
        else
            return false;
    }
</script>
<table align="left" bgcolor="navy">
<tr>
<td>
<span>
<asp:Button ID="btnCheckAll" runat="server" Text="Check All"
        onclick="btnCheckAll_Click" />
</span>
<span>
<asp:Button ID="btnUncheckAll" runat="server" Text="UnCheck All"
        onclick="btnUncheckAll_Click" />
</span>
<span>
<asp:Button ID="btnDelete" runat="server" Text="Delete"
        OnClientClick="return DeleteConfirmation();" onclick="btnDelete_Click" /></span>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="dgvMultipleRecordsDelete" runat="server"
    AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" Width="600px">
    <AlternatingRowStyle BackColor="White"   />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkMultiple" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemID">
<ItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>'  />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Player Name" DataField="Title" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</td>
</tr>
<tr>
<td bgcolor="white">
<asp:Label ID="lblMessage"  runat="server"></asp:Label>
</td>
</tr>
</table>


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Collections;
namespace EUDGridView.DMRGridView
{
    public partial class DMRGridViewUserControl : UserControl
    {
        string strListID = string.Empty;
        ArrayList ArrItem = new ArrayList();
        int count;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)

                    getData();

            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
        public void getData()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Players Location"];
                    strListID = lst.ID.ToString();
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    count = myColl.Count;
                    if (myColl.Count > 0)
                    {
                        dgvMultipleRecordsDelete.DataSource = myColl.GetDataTable();
                        dgvMultipleRecordsDelete.DataBind();
                    }
                });
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleDelete;
                foreach (GridViewRow gi in dgvMultipleRecordsDelete.Rows)
                {
                    chkMultipleDelete = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    if (chkMultipleDelete.Checked == true)
                    {
                        ArrItem.Add(dgvMultipleRecordsDelete.DataKeys[gi.RowIndex].Value);
                    }

                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Players Location"];
                    string strBatch = string.Empty;
                    strBatch += "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>";
                    for (int i = 0; i < ArrItem.Count; i++)
                    {
                        strBatch += "<Method><SetList Scope=\"Request\">" + lst.ID + "</SetList><SetVar Name=\"ID\">" + ArrItem[i].ToString() + "</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
                    }
                    strBatch += "</Batch>";
                    string strProcessBatch = currentWeb.ProcessBatchData(strBatch);
                    getData();
                    if (count == 0)
                    {
                        Response.Redirect(currentWeb.Url + "/UserPages/Multiple Row Delete in GridView.aspx");
                    }
                });

            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnCheckAll_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleDelete;
                foreach (GridViewRow gi in dgvMultipleRecordsDelete.Rows)
                {
                    chkMultipleDelete = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    chkMultipleDelete.Checked = true;
                }
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnUncheckAll_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleDelete;
                foreach (GridViewRow gi in dgvMultipleRecordsDelete.Rows)
                {
                    chkMultipleDelete = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    if (chkMultipleDelete.Checked == true)
                    {
                        chkMultipleDelete.Checked = false;
                    }
                }
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }                                                         
        }
    }
}





No comments:

Post a Comment