Search This Blog

Thursday, March 12, 2015

Basic Functionality of Edit, Update and Delete in GridView or SpGridView from SharePoint List and Populating Dropdown in Grid row



1- Creating Two  list 
a.  Country LookUp"---Country
b.  Global Clients -- Title,Country



<%@ 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="IEUDDLGridUserControl.ascx.cs" Inherits="IEUDDLGrid.IEUDDLGrid.IEUDDLGridUserControl" %>
<asp:GridView ID="dgvDDLBindsample" runat="server" AutoGenerateColumns="False" Width="600px"
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
    GridLines="None"  AutoGenerateDeleteButton="True"
     AutoGenerateEditButton="True"
     DataKeyNames="ID">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="ItemID" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("ID") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>' Columns="3" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Client Name" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("Title") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>' Columns="3" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country" HeaderStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <%# Eval("Country") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
</asp:GridView>
<asp:Label ID="lblMessage" runat="server"></asp:Label>





using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace IEUDDLGrid.IEUDDLGrid
{
    public partial class IEUDDLGridUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dgvDDLBindsample.RowDataBound += new GridViewRowEventHandler(dgvDDLBindsample_RowDataBound);
            dgvDDLBindsample.RowEditing += new GridViewEditEventHandler(dgvDDLBindsample_RowEditing);
            dgvDDLBindsample.RowUpdating += new GridViewUpdateEventHandler(dgvDDLBindsample_RowUpdating);
            dgvDDLBindsample.RowDeleting += new GridViewDeleteEventHandler(dgvDDLBindsample_RowDeleting);
            dgvDDLBindsample.RowCancelingEdit += new GridViewCancelEditEventHandler(dgvDDLBindsample_RowCancelingEdit);
            if (!IsPostBack)
            {
                getData();
            }
        }

        void dgvDDLBindsample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowState == DataControlRowState.Edit || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Country LookUp"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    DropDownList ddlCountryNew = e.Row.FindControl("ddlCountry") as DropDownList;
                    if (myColl.Count > 0)
                    {
                        ddlCountryNew.DataValueField = "ID";
                        ddlCountryNew.DataTextField = "Title";
                        ddlCountryNew.DataSource = myColl.GetDataTable();
                        ddlCountryNew.DataBind();
                    }
                    ddlCountryNew.Items.Insert(0, new ListItem("--Select Country--"));
                    int strItemID = int.Parse(dgvDDLBindsample.DataKeys[e.Row.RowIndex].Value.ToString());
                    SPList lstGlobalClients = currentWeb.Lists["Global Clients"];
                    SPQuery strFindLookUpValue = new SPQuery();
                    strFindLookUpValue.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + strItemID + "</Value></Eq></Where>";
                    SPListItemCollection GlobalClientsColl = lstGlobalClients.GetItems(strFindLookUpValue);
                    if (GlobalClientsColl.Count > 0)
                    {
                        SPListItem item = GlobalClientsColl[0];
                        SPFieldLookupValue value = new SPFieldLookupValue(item["ID"].ToString());
                        for (int i = 0; i < ddlCountryNew.Items.Count; i++)
                        {
                            if (value.LookupId.ToString() == ddlCountryNew.Items[i].Value)
                            {
                                ddlCountryNew.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
        public void updateRow(string ItemID1, string Title, string CountryLookUP)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Global Clients"];
            SPListItem item = null;
            item = lst.GetItemById(int.Parse(ItemID1));
            currentWeb.AllowUnsafeUpdates = true;
            item["Title"] = Title;
            item["Country"] = CountryLookUP;
            item.Update();
            lst.Update();
            currentWeb.AllowUnsafeUpdates = false;

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

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



        void dgvDDLBindsample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                dgvDDLBindsample.EditIndex = -1;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    getData();
                });
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        void dgvDDLBindsample_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                Label lbl = (Label)dgvDDLBindsample.Rows[e.RowIndex].Cells[1].FindControl("lblItemID");
                TextBox txtTitle1 = (TextBox)dgvDDLBindsample.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
                DropDownList ddlCountryNew = (DropDownList)dgvDDLBindsample.Rows[e.RowIndex].Cells[3].FindControl("ddlCountry");
                int LookUpValue = int.Parse(ddlCountryNew.SelectedValue.ToString());
                string LookUpValue1 = ddlCountryNew.SelectedItem.ToString();
                updateRow(lbl.Text, txtTitle1.Text, LookUpValue1);
                dgvDDLBindsample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
        string strItemID1;
        protected void dgvDDLBindsample_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                strItemID1 = dgvDDLBindsample.DataKeys[e.RowIndex].Value.ToString();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    DeleteRow(strItemID1);
                    getData();
                });
            }
            catch (Exception ex)
            {

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

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

        }
        void dgvDDLBindsample_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                dgvDDLBindsample.EditIndex = e.NewEditIndex;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    getData();
                });
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
    }
}



No comments:

Post a Comment