Search This Blog

Tuesday, September 9, 2014

Edit,Update and Delete in SharePoint List From a GridView or SpGridView

Note--Very -Very Importent Blog Prasad Bolla's SharePoint Blog




Edit,Update and Delete in SharePoint List From a GridView or SpGridView :-

Note:-

I have done some more operations with a GridView.

    1.        Multiple Deletion of SharePoint ListItems in GridView.
    2.        Using Asp.Net Dropdowlist in GridView using SharePoint Data.
    3.        Using Asp.Net CheckBoxlist in GridView using SharePoint Data.
    4.        Using Asp.Net RadioButtonList in GridView using SharePoint Data.
    5.        Bulk Edit and Update of SharePoint ListItems within a GridView. 
    6.        Insertion into a SharePoint List using GridView.

       


      Ascx
      <%@ 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="EUDGridViewUserControl.ascx.cs"
          Inherits="EUDGridView.EUDGridView.EUDGridViewUserControl" %>
      <asp:GridView ID="dgvEditUpdateDelete" runat="server" AutoGenerateColumns="False"
          CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"
          AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"OnRowDeleting="dgvEditUpdateDelete_RowDeleting"
          OnRowEditing="dgvEditUpdateDelete_RowEditing"OnRowUpdating="dgvEditUpdateDelete_RowUpdating"
          OnRowCancelingEdit="dgvEditUpdateDelete_RowCancelingEdit" DataKeyNames="ID">
          <AlternatingRowStyle BackColor="White" />
          <Columns>
              <asp:TemplateField HeaderText="ItemID">
                  <ItemTemplate>
                      <%# Eval("ID"%>
                  </ItemTemplate>
                  <EditItemTemplate>
                      <asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>' Columns="3" />
                  </EditItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="Title">
                  <ItemTemplate>
                      <%# Eval("Title"%>
                  </ItemTemplate>
                  <EditItemTemplate>
                      <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>' Columns="3" />
                  </EditItemTemplate>
              </asp:TemplateField>
          </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>
      <asp:Label ID="lblMessage" runat="server"></asp:Label>


      Ascx.Cs
      using System;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using Microsoft.SharePoint;
      using System.Security;
      namespace EUDGridView.EUDGridView
      {
          public partial class EUDGridViewUserControl : UserControl
          {
              protected void Page_Load(object sender, EventArgs e)
              {
                  try
                  {
                      SPWeb currentWeb = SPContext.Current.Web;
                      SPList lst = currentWeb.Lists["Sample List001"];
                      SPListItemCollection myColl = lst.Items;
                      if (myColl.Count > 0)
                      {
                          if (!IsPostBack)
                          {
                              dgvEditUpdateDelete.DataSource = myColl.GetDataTable();
                              dgvEditUpdateDelete.DataBind();
                          }
                      }
                  }
                  catch (Exception ex)
                  {

                      Response.Write(ex.ToString());
                  }
              }
              public void getData()
              {
                  SPWeb currentWeb = SPContext.Current.Web;
                  SPList lst = currentWeb.Lists["Sample List001"];
                  SPListItemCollection myColl = lst.Items;
                  if (myColl.Count > 0)
                  {
                          dgvEditUpdateDelete.DataSource = myColl.GetDataTable();
                          dgvEditUpdateDelete.DataBind();
                  }
              }
              string strItemID1;
              protected void dgvEditUpdateDelete_RowDeleting(object sender, GridViewDeleteEventArgs e)
              {
                  try
                  {
                      strItemID1 = dgvEditUpdateDelete.DataKeys[e.RowIndex].Value.ToString();
                      SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
                          DeleteRow(strItemID1);
                          getData();
                      });
                  }
                  catch (Exception ex)
                  {

                      Response.Write(ex.ToString());
                  }
              }

              protected void dgvEditUpdateDelete_RowEditing(object sender, GridViewEditEventArgs e)
              {
                  try
                  {
                       dgvEditUpdateDelete.EditIndex = e.NewEditIndex;
                       SPSecurity.RunWithElevatedPrivileges(delegate()
                       {
                           getData();
                       });
                 
                  }
                  catch (Exception ex)
                  {

                      Response.Write(ex.ToString());
                  }
              }
              protected void dgvEditUpdateDelete_RowUpdating(object sender, GridViewUpdateEventArgs e)
              {
                  try
                  {
                      Label lbl = (Label)dgvEditUpdateDelete.Rows[e.RowIndex].Cells[2].FindControl("lblItemID");
                      TextBox txtTitle1 = (TextBox)dgvEditUpdateDelete.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
                      SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
                          updateRow(lbl.Text, txtTitle1.Text);
                          dgvEditUpdateDelete.EditIndex = -1;
                          getData();
                      });
                  }
                  catch (Exception ex)
                  {

                      Response.Write(ex.ToString());
                  }
              }
              public void updateRow(string ItemID1,string Title)
              {
                  SPWeb currentWeb = SPContext.Current.Web;
                  SPList lst = currentWeb.Lists["Sample List001"];
                  SPListItem item = null;
                  item = lst.GetItemById(int.Parse(ItemID1));
                  currentWeb.AllowUnsafeUpdates = true;
                  item["Title"] = Title;
                  item.Update();
                  lst.Update();
                  currentWeb.AllowUnsafeUpdates = false;

              }
              public void DeleteRow(string ItemIDNew)
              {
                  SPWeb currentWeb = SPContext.Current.Web;
                  SPList lst = currentWeb.Lists["Sample List001"];
                  SPListItem item = null;
                  item = lst.GetItemById(int.Parse(ItemIDNew));
                  currentWeb.AllowUnsafeUpdates = true;
                  item.Delete();
                  lst.Update();
                  currentWeb.AllowUnsafeUpdates = false;

              }
              protected void dgvEditUpdateDelete_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
              {
                  dgvEditUpdateDelete.EditIndex = -1;
                  SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
                      getData();
                  });
              }
          }
      }


      No comments:

      Post a Comment