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 using Radio Button List in GridView with 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="BRBLGridViewUserControl.ascx.cs" Inherits="BRBLGridView.BRBLGridView.BRBLGridViewUserControl" %>
 <asp:GridView ID="dgvRBLBindsample" runat="server" AutoGenerateColumns="False"
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="Both"
     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:RadioButtonList ID="rblCategory" runat="server" RepeatColumns="3"></asp:RadioButtonList></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 BRBLGridView.BRBLGridView
{
    public partial class BRBLGridViewUserControl : UserControl
    {
        RadioButtonList rblSampleCategory;
        protected void Page_Load(object sender, EventArgs e)
        {
            dgvRBLBindsample.RowDataBound += new GridViewRowEventHandler(dgvRBLBindsample_RowDataBound);
            dgvRBLBindsample.RowEditing += new GridViewEditEventHandler(dgvRBLBindsample_RowEditing);
            dgvRBLBindsample.RowCancelingEdit += new GridViewCancelEditEventHandler(dgvRBLBindsample_RowCancelingEdit);
            dgvRBLBindsample.RowUpdating += new GridViewUpdateEventHandler(dgvRBLBindsample_RowUpdating);

            if (!IsPostBack)
            {
                getData();
            }
        }

        void dgvRBLBindsample_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                Label lbl = (Label)dgvRBLBindsample.Rows[e.RowIndex].Cells[1].FindControl("lblItemID");
                TextBox txtTitle1 = (TextBox)dgvRBLBindsample.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
                rblSampleCategory = (RadioButtonList)dgvRBLBindsample.Rows[e.RowIndex].Cells[3].FindControl("rblCategory");
                updateRow(lbl.Text, txtTitle1.Text, rblSampleCategory.SelectedValue.ToString());
                dgvRBLBindsample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }
        }
        public void updateRow(string ItemID1, string Title, string LookUpValue)
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["RBL Sample"];
                SPListItemCollection myColl = lst.Items;
                foreach (SPListItem item in myColl)
                {
                    if (item.ID.ToString() == ItemID1)
                    {
                        Title = item.Title;
                        item["SampleCategory"] = LookUpValue;
                        item.Update();
                        break;
                    }
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
        void dgvRBLBindsample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                dgvRBLBindsample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {

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

        void dgvRBLBindsample_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                dgvRBLBindsample.EditIndex = e.NewEditIndex;
                getData();
            }
            catch (Exception Ex)
            {

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

        void dgvRBLBindsample_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);
                        RadioButtonList rblCategoryNew = e.Row.FindControl("rblCategory") as RadioButtonList;
                        if (myColl.Count > 0)
                        {
                            rblCategoryNew.DataValueField = "ID";
                            rblCategoryNew.DataTextField = "Title";
                            rblCategoryNew.DataSource = myColl.GetDataTable();
                            rblCategoryNew.DataBind();
                        }
                        int strItemID = int.Parse(dgvRBLBindsample.DataKeys[e.Row.RowIndex].Value.ToString());
                        SPList lstChildList = currentWeb.Lists["RBL Sample"];
                        SPQuery strFindLookUpValue = new SPQuery();
                        strFindLookUpValue.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + strItemID + "</Value></Eq></Where>";
                        SPListItemCollection LookupColl = lstChildList.GetItems(strFindLookUpValue);
                        if (LookupColl.Count > 0)
                        {
                            SPListItem item = LookupColl[0];
                            SPFieldLookupValue value = new SPFieldLookupValue(item["SampleCategory"].ToString());
                            foreach (ListItem oItem in rblCategoryNew.Items)
                            {
                                int i = int.Parse(oItem.Value.ToString());
                                int j = int.Parse(value.LookupId.ToString());
                                if (i == j)
                                {
                                    oItem.Selected = true;
                                    break;
                                }
                            }

                        }

                    });
                }
            }
            catch (Exception Ex)
            {

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

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

No comments:

Post a Comment