This is a migrated thread and some comments may be shown as answers.

Binding GridDropDownColumn inside RadGrid

1 Answer 158 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kalaiselvi Kumarasamy
Top achievements
Rank 1
Kalaiselvi Kumarasamy asked on 27 Apr 2010, 03:27 AM

I've a SharePoint List named Promotions and following are sample data in it.
PromotionID    Promotions        Active
1                          Promotion 1         Yes
2                          Promotion 2         Yes
3                          Promotion 3          No
4                          Promotion 4          No

I also one another SharePoint list called StorePromotions
StorePromoID    Promotions(Promotions List Look Up Column)    Region(Regions List Look Up Column) ...    
1                            Promotion 4                                                                    Region 3
2                            Promotion 3                                                                    Region 5

When I try to update the StorePromotion List, I choose Promotions from GridDropDownColumn(which I bind earlier with Promotions List) by getting the value from SelectedValue property(ie., storePromo.TradeName = tradeEditor.SelectedValue;). But when I update the list I get error, because I need the "PromotionID" and NOT the SelectedValue. Can anyone tell me how I can get that?

For more information, here is my code
StorePromotions.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StorePromotionsByTrade.aspx.cs"
    Inherits="Heritage.Internet.UI.Pages.ContentManager.StorePromotionsByTrade,Heritage.Internet.UI.Pages.ContentManager,Culture=neutral, Version=1.0.0.0, PublicKeyToken=1202d49d749de830"
    MasterPageFile="~/_layouts/HP/application.master" EnableSessionState="True"%>

<%@ Register Assembly="Telerik.Web.UI, Version=2010.1.309.20, Culture=neutral, PublicKeyToken=121fae78165ba3d4"
    Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="~/_controltemplates/ButtonSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="~/_controltemplates/InputFormSection.ascx" %>
<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ID="Content1" contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server"></asp:Content>
<asp:Content ID="Content2" contentplaceholderid="PlaceHolderPageTitle" runat="server"></asp:Content>
<asp:Content ID="Content3" contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server">Store Promotions assigned by TradeName</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderId="PlaceHolderPageDescription" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <telerik:RadGrid ID="radGridStorePromotions" runat="server" GridLines="None" AutoGenerateColumns="false"
        AllowPaging="true" AllowSorting="true" OnNeedDataSource="radGridStorePromotions_OnNeedDataSource"
        OnItemDataBound="radGridStorePromotions_OnItemDataBound" OnItemCreated="radGridStorePromotions_ItemCreated"
        OnUpdateCommand="radGridStorePromotions_OnUpdateCommand" >
        <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView TableLayout="Fixed" Height="100%"  AllowFilteringByColumn="True">
            <Columns>
                <telerik:GridNumericColumn HeaderText="ID" DataField="ID" ReadOnly="true" Visible="false" UniqueName="PromoIdUniqueName" />
                <telerik:GridDropDownColumn HeaderText="Promotion" UniqueName="PromotionsUniqueName"
                         DataField="Promotion" ListTextField="Promotion" ListValueField="Promotion" ListDataMember="Title"  /> 
                <telerik:GridDropDownColumn HeaderText="TradeName" UniqueName="TradeNameUniqueName"
                         DataField="TradeName" ListTextField="TradeName" ListValueField="TradeName" ListDataMember="Title" 
                         DropDownControlType="RadComboBox" />
                <telerik:GridDropDownColumn HeaderText="StoreLocation" UniqueName="StoreLocUniqueName"
                         DataField="StoreLocation" ListTextField="StoreLocation" ListValueField="StoreLocation"
                         ListDataMember="Title" />
                <telerik:GridDateTimeColumn HeaderText="Start Date" UniqueName="StartDateUniqueName" DataField="StartDate" />
                <telerik:GridDateTimeColumn HeaderText="End Date" UniqueName="EndDateUniqueName" DataField="EndDate" />
                <telerik:GridCheckBoxColumn HeaderText="Active" UniqueName="ActiveUniqueName" DataField="Active" />
                <telerik:GridEditCommandColumn HeaderText="Edit" UniqueName="EditCommandColumn" ButtonType="LinkButton"
                         EditText="Edit" UpdateText="Update" CancelText="Cancel" />
                <telerik:GridButtonColumn HeaderText="Delete" UniqueName="DeleteColumn" Text="Delete" CommandName="Delete" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
</asp:Content>

And this is my StorePromotions.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Heritage.Internet.BLL.Promotions;
using Heritage.Internet.BOL.Promotions;
using Heritage.Internet.BLL.Regions;
using Heritage.Internet.BOL.Regions;
using Heritage.Internet.BLL.StoreLocations;
using Heritage.Internet.BOL.StoreLocations;
using Heritage.Internet.BLL.StorePromotions;
using Heritage.Internet.BOL.StorePromotions;
using Heritage.Internet.BLL.TradeNames;
using Heritage.Internet.BOL.TradeNames;
using Telerik.Web.UI;

namespace Heritage.Internet.UI.Pages.ContentManager
{
    public partial class StorePromotionsByTrade : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StorePromotionsBindData();
            }
        }

        protected void radGridStorePromotions_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (Session["AllStorePromotions"] != null)
            {
                radGridStorePromotions.DataSource = Session["AllStorePromotions"];
            }
            else
            {
                StorePromotionsBindData();
            }
        }

        protected void radGridStorePromotions_OnItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem gridEditFormItem = (GridEditableItem)e.Item;
                GridEditManager gridEditMan = gridEditFormItem.EditManager;

                RadComboBox ddlTradeName = gridEditFormItem["TradeNameUniqueName"].Controls[0] as RadComboBox;

                GridDropDownColumnEditor promotionsEditor = gridEditMan.GetColumnEditor("PromotionsUniqueName") as GridDropDownColumnEditor;
                GridDropDownColumnEditor tradeEditor = gridEditMan.GetColumnEditor("TradeNameUniqueName") as GridDropDownColumnEditor;
                GridDropDownColumnEditor storeEditor = gridEditMan.GetColumnEditor("StoreLocUniqueName") as GridDropDownColumnEditor;
               
                //Get all ACTIVE Promotions in ASCENDING order and bind it to Promotions DropDownList
                /****************************************************************/
                //Storing the promotions in Session variable doesn't work.
                //It binds StorePromotions varibales to Promotions DropDownList.
                /****************************************************************/
                Promotion promotionBLL = new Promotion();
                List<PromotionEntity> promotions = new List<PromotionEntity>();
                promotions = promotionBLL.GetAllPromotions();
                object tempPromotions = promotionsEditor.DataSource;
                promotionsEditor.DataSource = promotions;
                promotionsEditor.DataBind();
                promotionsEditor.DataSource = tempPromotions;
               
                //Get ACTIVE Trade Names in ASCENDING order and bind it to TradeName DropDownList
                TradeName tradeNameBLL = new TradeName();
                List<TradeNameEntity> tradeNames = new List<TradeNameEntity>();
                tradeNames = tradeNameBLL.GetAllTradeNames();
                object tempTradeNames = tradeEditor.DataSource;
                tradeEditor.DataSource = tradeNames;
                tradeEditor.DataBind();
                tradeEditor.SelectedValue = ddlTradeName.SelectedValue;
                tradeEditor.DataSource = tempTradeNames;

                //Get ACTIVE Stores filtered by Selected TardeName and bind it to StoreLocations DropDownList
                StoreLocation storeLocBLL = new StoreLocation();
                List<StoreLocationListEntity> storeLocationsList = new List<StoreLocationListEntity>();
                storeLocationsList = storeLocBLL.GetStoresByTradeName(ddlTradeName.SelectedValue);
                object tempDataSource = storeEditor.DataSource;
                storeEditor.DataSource = storeLocationsList;
                storeEditor.DataBind();
                storeEditor.DataSource = tempDataSource;
            }
        }

        protected void radGridStorePromotions_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //the dropdown list will be the first control in the Controls collection of the corresponding cell
                RadComboBox ddlTradeName = (e.Item as GridEditableItem)["TradeNameUniqueName"].Controls[0] as RadComboBox;
               
                //attach SelectedIndexChanged event for the combobox control
                ddlTradeName.AutoPostBack = true;
                ddlTradeName.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(ddlTradeName_OnSelectedIndexChanged);
            }
        }

        protected void ddlTradeName_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
            RadComboBox ddlStoreLoc = editedItem["StoreLocUniqueName"].Controls[0] as RadComboBox;

            StoreLocation storeLocBLL = new StoreLocation();
            List<StoreLocationListEntity> storeLocationsList = new List<StoreLocationListEntity>();

            storeLocationsList = storeLocBLL.GetStoresByTradeName((editedItem["TradeNameUniqueName"].Controls[0] as RadComboBox).SelectedValue);
            ddlStoreLoc.ClearSelection();
            ddlStoreLoc.DataSource = storeLocationsList;
            ddlStoreLoc.DataBind();
        }

        protected void radGridStorePromotions_OnUpdateCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem gridEditFormItem = (GridEditableItem)e.Item;
            GridEditManager gridEditMan = gridEditFormItem.EditManager;

            GridNumericColumnEditor idEditor = gridEditMan.GetColumnEditor("PromoIdUniqueName") as GridNumericColumnEditor;
            GridDropDownColumnEditor promotionsEditor = gridEditMan.GetColumnEditor("PromotionsUniqueName") as GridDropDownColumnEditor;
            GridDropDownColumnEditor tradeEditor = gridEditMan.GetColumnEditor("TradeNameUniqueName") as GridDropDownColumnEditor;
            GridDropDownColumnEditor storeEditor = gridEditMan.GetColumnEditor("StoreLocUniqueName") as GridDropDownColumnEditor;
            GridDateTimeColumnEditor startDtEditor = gridEditMan.GetColumnEditor("StartDateUniqueName") as GridDateTimeColumnEditor;
            GridDateTimeColumnEditor endDtEditor = gridEditMan.GetColumnEditor("EndDateUniqueName") as GridDateTimeColumnEditor;
            GridCheckBoxColumnEditor activeEditor = gridEditMan.GetColumnEditor("ActiveUniqueName") as GridCheckBoxColumnEditor;

            StorePromotion storePromoBLL = new StorePromotion();
            StorePromotionListEntity storePromo = new StorePromotionListEntity();

            storePromo.ID = Convert.ToInt32(idEditor.Text);
            storePromo.Promotion = promotionsEditor.SelectedValue;
            int s = promotionsEditor.SelectedIndex;
            string d = promotionsEditor.SelectedText;
            storePromo.TradeName = tradeEditor.SelectedValue;
            storePromo.StoreLocation = storeEditor.SelectedValue;
            storePromo.StartDate = Convert.ToDateTime(startDtEditor.Text);
            storePromo.EndDate = Convert.ToDateTime(endDtEditor.Text);
            storePromo.Active = activeEditor.Value;

            storePromoBLL.UpdateStorePromotionToList(storePromo);

            StorePromotionsBindData();
        }

        private void StorePromotionsBindData()
        {
            //Get ALL Store Promotions in ASCENDING Order
            StorePromotion storePromosBLL = new StorePromotion();
            List<StorePromotionListEntity> storePromos = new List<StorePromotionListEntity>();
            storePromos = storePromosBLL.GetStorePromotionsAssignedByTrade();

            radGridStorePromotions.DataSource = storePromos;
            Session["AllStorePromotions"] = storePromos;
            radGridStorePromotions.DataBind();
        }
    }
}


I've finish this task by EOD tomorrow. If anyone could help me find this, it'd be really helpful.

Thank you.
K

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 29 Apr 2010, 01:14 PM
Hi Kalaiselvi,

This scenario is not support. You should have PromotionID  in your StorePromotions list. You currently have the Promotions field related to your Promotions list. GridDropDownColumn needs both DataField and ListValueField to point to the foreign key in your primary grid table. To be able to get the PromotionID as the selected value from the editor dropdown, you need to have ListValueField="PromotionID". However, this would also require that the primary table is linked to the Promotions table through the PromotionID field, so you would also need DataField="PromotionID". In other words, data field of the column and the value field that is used to get the dropdown values need to match.

Best wishes,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Kalaiselvi Kumarasamy
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or