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

Edit and Update the Checkbox List with in a GridView or SpGridView from SharePoint List using Multi LookUp Column in the Backend



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="BCBLGridViewUserControl.ascx.cs" Inherits="BCBLGridView.BCBLGridView.BCBLGridViewUserControl" %>
<asp:GridView ID="dgvCBLBindsample" runat="server" AutoGenerateColumns="False"
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"
     AutoGenerateEditButton="True"
     DataKeyNames="ID" Width=100%>
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="ItemID">
            <ItemTemplate>
                <center><%# Eval("ID") %></center>
            </ItemTemplate>
            <EditItemTemplate>
                <center><asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>' Columns="3" /></center>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <center><%# Eval("Title") %></center>
            </ItemTemplate>
            <EditItemTemplate>
                <center><asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>'  /></center>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Category">
            <ItemTemplate>
               <center><%# Eval("SampleCategory") %></center>
            </ItemTemplate>
            <EditItemTemplate>
                <center><asp:CheckBoxList ID="chkCategory" runat="server" RepeatColumns="3"></asp:CheckBoxList></center>
            </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 BCBLGridView.BCBLGridView
{
    public partial class BCBLGridViewUserControl : UserControl
    {
        CheckBoxList cblSampleCategory;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                dgvCBLBindsample.RowDataBound += new GridViewRowEventHandler(dgvCBLBindsample_RowDataBound);
                dgvCBLBindsample.RowEditing += new GridViewEditEventHandler(dgvCBLBindsample_RowEditing);
                dgvCBLBindsample.RowUpdating += new GridViewUpdateEventHandler(dgvCBLBindsample_RowUpdating);
                dgvCBLBindsample.RowCancelingEdit += new GridViewCancelEditEventHandler(dgvCBLBindsample_RowCancelingEdit);
                if (!IsPostBack)
                {
                    getData();
                }
            }
            catch (Exception Ex)
            {

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

        void dgvCBLBindsample_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                dgvCBLBindsample.EditIndex = e.NewEditIndex;
                getData();

            }
            catch (Exception Ex)
            {

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

        void dgvCBLBindsample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowState == DataControlRowState.Edit || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPWeb currentWeb = SPContext.Current.Web;
                        SPList lst = currentWeb.Lists["Sample Category"];
                        SPQuery sQuery = new SPQuery();
                        sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                        SPListItemCollection myColl = lst.GetItems(sQuery);
                        CheckBoxList chkCategoryNew = e.Row.FindControl("chkCategory") as CheckBoxList;
                        if (myColl.Count > 0)
                        {
                            chkCategoryNew.DataValueField = "ID";
                            chkCategoryNew.DataTextField = "Title";
                            chkCategoryNew.DataSource = myColl.GetDataTable();
                            chkCategoryNew.DataBind();
                        }
                        int strItemID = int.Parse(dgvCBLBindsample.DataKeys[e.Row.RowIndex].Value.ToString());
                        SPList lstChildList = currentWeb.Lists["Multi LookUp"];
                        SPQuery strFindLookUpValue = new SPQuery();
                        strFindLookUpValue.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + strItemID + "</Value></Eq></Where>";
                        SPListItemCollection MultiLookupColl = lstChildList.GetItems(strFindLookUpValue);
                        if (MultiLookupColl.Count > 0)
                        {
                            SPListItem item = MultiLookupColl[0];
                            SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(item["SampleCategory"].ToString());
                            chkCategoryNew.ClearSelection();
                            foreach (SPFieldLookupValue value in values)
                            {
                                foreach (ListItem oItem in chkCategoryNew.Items)
                                {
                                    int i = int.Parse(oItem.Value.ToString());
                                    int j = int.Parse(value.LookupId.ToString());
                                    if (i == j)
                                    {
                                        oItem.Selected = true;
                                    }
                                }
                            }
                        }
                    });
                }
                else
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            SPWeb currentWeb = SPContext.Current.Web;
                            int strItemID = int.Parse(dgvCBLBindsample.DataKeys[e.Row.RowIndex].Value.ToString());
                            SPList lstChildList = currentWeb.Lists["Multi LookUp"];
                            SPQuery strFindLookUpValue = new SPQuery();
                            strFindLookUpValue.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + strItemID + "</Value></Eq></Where>";
                            SPListItemCollection MultiLookupColl = lstChildList.GetItems(strFindLookUpValue);
                            if (MultiLookupColl.Count > 0)
                            {
                                SPListItem item = MultiLookupColl[0];
                                SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(item["SampleCategory"].ToString());
                                string strLookUpValues = string.Empty;
                                foreach (SPFieldLookupValue value in values)
                                {
                                    strLookUpValues += value.LookupValue + ";";
                                }

                                e.Row.Cells[3].Text = strLookUpValues;
                            }
                        });

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

                lblMessage.Text = Ex.ToString();
            }
        }
        void dgvCBLBindsample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                dgvCBLBindsample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {

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

        void dgvCBLBindsample_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                Label lbl = (Label)dgvCBLBindsample.Rows[e.RowIndex].Cells[1].FindControl("lblItemID");
                TextBox txtTitle1 = (TextBox)dgvCBLBindsample.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
                cblSampleCategory = (CheckBoxList)dgvCBLBindsample.Rows[e.RowIndex].Cells[3].FindControl("chkCategory");
                int LookUpValue = int.Parse(cblSampleCategory.SelectedValue.ToString());
                updateRow(lbl.Text, txtTitle1.Text);
                dgvCBLBindsample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }
        }
        public void updateRow(string ItemID1, string Title)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Multi LookUp"];
            SPListItemCollection myColl = lst.Items;
            SPFieldLookupValueCollection itemValues = new SPFieldLookupValueCollection();
            foreach (SPListItem item in myColl)
            {
                if (item.ID.ToString() == ItemID1)
                {
                    Title = item.Title;
                    foreach (ListItem oItem in cblSampleCategory.Items)
                    {
                        if (oItem.Selected)
                        {
                            itemValues.Add(new SPFieldLookupValue(int.Parse(oItem.Value), oItem.Text));
                        }
                    }
                    item["SampleCategory"] = itemValues;
                    item.Update();
                }
            }
        }
    }


}

No comments:

Post a Comment