Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, January 22, 2013

Performing Edit and Update Operations with Dropdown List inside a GridView or SharePoint GridView from SharePoint List using LookUp Column in the Backend



Note:-
The Same way we can perform operations for Checkbox List and Radio Button List inside GridView or SpGridVIew.

Sample Code for Checkbox List:-

Sample Code for Radio Button List:-
Output:-

Backend List:-

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="IEUDDLGridUserControl.ascx.cs" Inherits="IEUDDLGrid.IEUDDLGrid.IEUDDLGridUserControl" %>
<asp:GridView ID="dgvDDLBindsample" runat="server" AutoGenerateColumns="False"
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"
     AutoGenerateEditButton="True"
     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="Client Name">
            <ItemTemplate>
                <%# Eval("Title") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>' Columns="3" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <%# Eval("Country") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>
            </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 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.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["Country"].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, int 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());
                updateRow(lbl.Text, txtTitle1.Text,LookUpValue);
                dgvDDLBindsample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        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