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

RadComboBox row-dependent data on Grid

1 Answer 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
archimede
Top achievements
Rank 1
archimede asked on 06 Sep 2010, 10:04 AM
I have an aspx page with a Grid. Each row on grid is identified by an id (idAzienda).
Each row on grid has also a column with a radComboBox (RepartoRadComboBox). I want this radComboBox to have a datasource (ObjectDataSourceRepartiAzienda) dependent on row parameter idAzienda.
How can I set this "dynamical" datasource?

My grid is:
<telerik:RadGrid ID="RadGridAziendeForUser" runat="server" DataSourceID="ObjectDataSourceAziendeUtente"
                EnableEmbeddedSkins="False" GridLines="None" Skin="MachinaWeb" AllowAutomaticUpdates="True"
                AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowMultiRowSelection="True"
                OnItemCommand="RadGridAziendeForUser_ItemCommand" OnItemDataBound="RadGridAziendeForUser_ItemDataBound"
                AllowPaging="True" PageSize="5">
                <ClientSettings EnablePostBackOnRowClick="True" Resizing-AllowColumnResize="false">
                    <Selecting AllowRowSelect="True" />
                </ClientSettings>
                <MasterTableView AutoGenerateColumns="False" DataSourceID="ObjectDataSourceAziendeUtente"
                    EditMode="InPlace" CommandItemDisplay="Top" DataKeyNames="IdUtente,IdAzienda,TimeStamp">
                    <CommandItemTemplate>
                        <div style="text-align: center; width: 95%; float: left;">
                            <asp:Label ID="LabelAziendeForUserTxt" runat="server" Text='<%$ Resources:Resource, AziendePerUtente_M %>'></asp:Label></div>
                        <div style="float: right; width: 5%; text-align: right;">
                            <asp:ImageButton ID="NewAziendaButton" runat="server" CausesValidation="False" CommandName="InitInsert"
                                ImageUrl="~/Images/AddRecord.png" Text="<%$ Resources:Resource, Inserisci %>" />
                        </div>
                    </CommandItemTemplate>
                    <Columns>
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" CancelImageUrl="~/Images/ImagesGrid/Cancel.gif"
                            EditImageUrl="~/Images/ImagesGrid/Edit.gif" UpdateImageUrl="~/Images/ImagesGrid/Update.gif"
                            InsertImageUrl="~/Images/ImagesGrid/Update.gif" UniqueName="EditCommandColumn"
                            ItemStyle-Width="40px">
                            <ItemStyle Width="40px" />
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn DataField="IdUtente" DataType="System.Int64" HeaderText="IdUtente"
                            ReadOnly="True" SortExpression="IdUtente" UniqueName="IdUtente" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="IdUtenteAsp" HeaderText="IdUtenteAsp" SortExpression="IdUtenteAsp"
                            UniqueName="IdUtenteAsp" DataType="System.Guid" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="UserName" HeaderText="UserName" ReadOnly="True"
                            SortExpression="UserName" UniqueName="UserName" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="IdCultura" DataType="System.Int64" HeaderText="IdCultura"
                            ReadOnly="True" SortExpression="IdCultura" UniqueName="IdCultura" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn>
                            <HeaderTemplate>
                                <asp:Label ID="Label46" runat="server" Text='<%$ Resources:Resource, Azienda %>'
                                    Width="130px"></asp:Label></HeaderTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="AziendaRadComboBox" runat="server" DataSourceID="ObjectDataSourceAzienda"
                                    DataTextField="NomeAzienda" DataValueField="IdAzienda" SelectedValue='<%# Bind("IdAzienda") %>'
                                    Width="120px" Enabled="false" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label47" runat="server" Width="120px" Text='<%# Bind("Azienda") %>'></asp:Label></ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn>
                            <HeaderTemplate>
                                <asp:Label ID="Label48" runat="server" Text='<%$ Resources:Resource, Cultura %>'
                                    Width="80px"></asp:Label></HeaderTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="CulturaRadComboBox" runat="server" DataSourceID="ObjectDataSourceCultura"
                                    DataTextField="Cultura" DataValueField="IdCultura" SelectedValue='<%# Bind("IdCultura") %>'
                                    Width="70px" AppendDataBoundItems="true" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label50" runat="server" Width="70px" Text='<%# Bind("Cultura") %>'></asp:Label></ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn>
                            <HeaderTemplate>
                                <asp:Label ID="Label49" runat="server" Text='<%$ Resources:Resource, Gruppo %>' Width="130px"></asp:Label></HeaderTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="GruppoRadComboBox" runat="server" DataSourceID="ObjectDataSourceGruppoUtente"
                                    DataTextField="Nome" DataValueField="IdGruppoUtente" SelectedValue='<%# Bind("IdGruppo") %>'
                                    Width="120px" AppendDataBoundItems="true" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label51" runat="server" Text='<%# Bind("Gruppo") %>' Width="120px"></asp:Label></ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn>
                            <HeaderTemplate>
                                <asp:Label ID="Label52" runat="server" Text='<%$ Resources:Resource, Stabilimento %>'
                                    Width="130px"></asp:Label></HeaderTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="StabilimentoRadComboBox" runat="server" DataSourceID="ObjectDataSourceRepartiAzienda"
                                    DataTextField="IdReparto" DataValueField="IdAzienda" Width="120px"
                                    AppendDataBoundItems="True" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label53" runat="server" Text='<%# Bind("Stabilimento") %>' Width="120px"></asp:Label></ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn UniqueName="RepartoRadComboBoxTC">
                            <HeaderTemplate>
                                <asp:Label ID="Label54" runat="server" Text='<%$ Resources:Resource, Reparto %>'
                                    Width="130px"></asp:Label></HeaderTemplate>
                            <EditItemTemplate>
                                <telerik:RadComboBox ID="RepartoRadComboBox" runat="server" Width="120px" AppendDataBoundItems="true" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label55" runat="server" Text='<%# Bind("Settore") %>' Width="120px"></asp:Label></ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridButtonColumn CommandName="Delete" ImageUrl="~/Images/ImagesGrid/Delete.gif"
                            UniqueName="DeleteColumn" ConfirmText="Do you really want to delete the selected row?"
                            ButtonType="ImageButton" />
                    </Columns>
                    <EditFormSettings>
                        <EditColumn>
                        </EditColumn>
                    </EditFormSettings>
                </MasterTableView><FilterMenu EnableEmbeddedSkins="False">
                </FilterMenu>
                <HeaderContextMenu EnableEmbeddedSkins="False">
                </HeaderContextMenu>
            </telerik:RadGrid>

DataSources are:
<asp:ObjectDataSource ID="ObjectDataSourceRepartiAzienda" runat="server"
                SelectMethod="getAllByAzienda" TypeName="MachinaWeb.Services.BO_Reparto">
                <SelectParameters>
                    <asp:Parameter Name="idAzienda" Type="Int64" />
                </SelectParameters>
            </asp:ObjectDataSource>
            <asp:ObjectDataSource ID="ObjectDataSourceAziendeUtente" runat="server"
                DataObjectTypeName="MachinaWeb.Services.BO_Utente" DeleteMethod="deleteAziendaUtente"
                EnablePaging="True" InsertMethod="insertAziendaUtente" SelectMethod="getUtentiFromIdUtenteAsp"
                TypeName="MachinaWeb.Services.BO_Utente" UpdateMethod="updateAziendaUtente" MaximumRowsParameterName="maxRows"
                StartRowIndexParameterName="startRows" SelectCountMethod="GetUtentiCount" OnUpdated="ObjectDataSourceAziendeUtente_Updated">
                <SelectParameters>
                    <asp:ControlParameter ControlID="HiddenFieldGuidUtenteAspSelezionatoOnRadGridAspnetUsers"
                        DbType="Guid" Name="idUtenteAsp" PropertyName="Value" />
                    <asp:ControlParameter ControlID="HiddenFieldIdCultura" Name="idCultura" PropertyName="Value"
                        Type="Int64" />
                </SelectParameters>
            </asp:ObjectDataSource>

Code for business object reparto is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatiMachinaWebEntityFramework;
using System.Data.Objects;
 
namespace MachinaWeb.Services
{
    public class BO_Reparto
    {
        public long IdReparto { get; set; }
        public string CodReparto { get; set; }
        public string Reparto { get; set; }
        public long IdStabilimento { get; set; }
        public string CodStabilimento { get; set; }
        public string Stabilimento { get; set; }
        public long  IdAzienda { get; set; }
        public string codice_reparto_aux  { get; set; }
        public string Denominazione { get; set; }
        public long IdResponsabile { get; set; }
        public string  Telefono { get; set; }
        public long IdCdc { get; set; }
        public long IdTurno { get; set; }
        public Int16?   RepartiPosizione { get; set; }
        public Boolean  EscludeDomenica { get; set; }
        public Boolean  EscludeSabato { get; set; }
        public Boolean EscludeFestivi { get; set; }
        public long IdCitta { get; set; }
        public long IdVia { get; set; }
        public string Indirizzo { get; set; }
        public String NCivico { get; set; }
        public string Interno { get; set; }
        public string Telefono2 { get; set; }
        public string Fax { get; set; }
        public string Internet { get; set; }
        public string Email { get; set; }
        public long? IdCommessa { get; set; }
        public long IdUtente { get; set; }
        public DateTime DataCreazione { get; set; }
        public DateTime DataAggiornamento { get; set; }
        public string  Note_memo  { get; set; }
        public long IdSicurezza { get; set; }
        public DateTime? DataDismissione { get; set; }
        public byte[] TimeStamp { get; set; }
 
        public BO_Reparto()
        {
            IdReparto = -1;
            CodReparto = String.Empty;
            Reparto = String.Empty;
        }
 
        public BO_Reparto(Reparti_Get_ByIdAzienda_Result repSp)
        {
            IdReparto = repSp.IdReparto;
            Reparto = repSp.Reparto;
            CodReparto = repSp.CodReparto;
            IdStabilimento = repSp.IdStabilimento;
            CodStabilimento = repSp.CodStabilimento;
            Stabilimento = repSp.Denominazione;
              
        }
 
        public List<BO_Reparto> getAllByAzienda(long idAzienda)
        {
            List<BO_Reparto> lista = new List<BO_Reparto>();
 
            using (DatiMachinaWeb_BeniPianiInterventiEntities context = new DatiMachinaWeb_BeniPianiInterventiEntities())
            {
                IEnumerable<Reparti_Get_ByIdAzienda_Result> repartiSp = context.Reparti_Get_ByIdAzienda(idAzienda);
 
                foreach (Reparti_Get_ByIdAzienda_Result repSp in repartiSp)
                {
                    lista.Add(new BO_Reparto(repSp));
                }
            }
 
            return lista;
        }
 
        public List<BO_Reparto> getAllByAzienda(long idAzienda, long maxRows, long startRows)
        {
            List<BO_Reparto> lista = new List<BO_Reparto>();
 
            using (DatiMachinaWeb_BeniPianiInterventiEntities context = new DatiMachinaWeb_BeniPianiInterventiEntities())
            {
                IEnumerable<Reparti_Get_ByIdAzienda_Result> repartiSp = context.Reparti_Get_ByIdAzienda(idAzienda).Skip((int)startRows).Take((int)maxRows);
 
                foreach (Reparti_Get_ByIdAzienda_Result repSp in repartiSp)
                {
                    lista.Add(new BO_Reparto(repSp));
                }
            }
 
            return lista;
        }
 
        public List<BO_Reparto> getRepartiExample()
        {
            List<BO_Reparto> lista = new List<BO_Reparto>();
            long idAzienda = 1;
 
            using (DatiMachinaWeb_BeniPianiInterventiEntities context = new DatiMachinaWeb_BeniPianiInterventiEntities())
            {
                IEnumerable<Reparti_Get_ByIdAzienda_Result> repartiSp = context.Reparti_Get_ByIdAzienda(idAzienda).Take(5);
 
                foreach (Reparti_Get_ByIdAzienda_Result repSp in repartiSp)
                {
                    lista.Add(new BO_Reparto(repSp));
                }
            }
 
            return lista;
        }
 
        public int GetRepartiCount(long idAzienda)
        {
            using (DatiMachinaWeb_BeniPianiInterventiEntities context = new DatiMachinaWeb_BeniPianiInterventiEntities())
            {
                int repartiCount = (from r in context.Reparti_cli_for
                                   where   r.IdAzienda== idAzienda
                                   select r.IdReparto ).Count();
 
                return repartiCount;
            }
        }
        public BO_Reparto(Reparto_GetByIdReparto_Result getRepartoByIdRepartoResult, long IdReparto)
        {
            using (DatiMachinaWeb_AccessoEntities context = new DatiMachinaWeb_AccessoEntities())
            {
                IdUtente = getRepartoByIdRepartoResult.IdUtente;
                IdAzienda = getRepartoByIdRepartoResult.IdAzienda;
                IdStabilimento = getRepartoByIdRepartoResult.IdStabilimento;
                CodReparto = getRepartoByIdRepartoResult.CodReparto;
                codice_reparto_aux = getRepartoByIdRepartoResult.codice_reparto_aux;
                IdReparto = getRepartoByIdRepartoResult.IdReparto;
                Denominazione = getRepartoByIdRepartoResult.Denominazione ;
                IdResponsabile = getRepartoByIdRepartoResult.IdResponsabile;
                IdVia = getRepartoByIdRepartoResult.IdVia;
                IdCitta = getRepartoByIdRepartoResult.IdCitta;
                Indirizzo = getRepartoByIdRepartoResult.Indirizzo;
                NCivico=getRepartoByIdRepartoResult.NCivico;
                Interno=getRepartoByIdRepartoResult.Interno;
                IdCdc = getRepartoByIdRepartoResult.IdCdc;
                IdTurno = getRepartoByIdRepartoResult.IdTurno;
                RepartiPosizione = getRepartoByIdRepartoResult.RepartiPosizione;
                EscludeDomenica = getRepartoByIdRepartoResult.EscludeDomenica;
                EscludeSabato = getRepartoByIdRepartoResult.EscludeSabato;
                EscludeFestivi = getRepartoByIdRepartoResult.EscludeFestivi;
                Telefono = getRepartoByIdRepartoResult.Telefono;
                Telefono2 = getRepartoByIdRepartoResult.Telefono2;
                Fax = getRepartoByIdRepartoResult.Fax;
                IdCommessa = getRepartoByIdRepartoResult.IdCommessa;
                DataCreazione = getRepartoByIdRepartoResult.DataCreazione;
                DataAggiornamento = getRepartoByIdRepartoResult.DataAggiornamento;
                Email = getRepartoByIdRepartoResult.Email;
                Internet = getRepartoByIdRepartoResult.Internet;
                Note_memo  = getRepartoByIdRepartoResult.Note_memo;
                IdSicurezza = getRepartoByIdRepartoResult.IdSicurezza;
                DataDismissione = getRepartoByIdRepartoResult.DataDismissione;
                TimeStamp = getRepartoByIdRepartoResult.TimeStamp;
            }
 
        }
        public BO_Reparto getRepartoByIdReparto(long IdReparto)
        {
            using (DatiMachinaWeb_BeniPianiInterventiEntities context = new DatiMachinaWeb_BeniPianiInterventiEntities())
            {
                Reparto_GetByIdReparto_Result repartosp = context.Reparto_GetByIdReparto(IdReparto).FirstOrDefault();
                return new BO_Reparto(repartosp,IdReparto );
            }
        }
    }
     
    //public BO_Utente getUtenteFromIdUtente(long idUtente, long idCultura)
    //    {
    //        using (DatiMachinaWeb_AccessoEntities context = new DatiMachinaWeb_AccessoEntities())
    //        {
    //            GetUtenteFromIdUtente_Result utenteSp = context.GetUtenteFromIdUtente(idUtente).FirstOrDefault();
    //            return new BO_Utente(utenteSp, idCultura);
    //        }
    //    }
}

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Sep 2010, 12:56 PM
Hello,

If you want to populate RadComboBox based on the field 'idAzienda' (which is DataKeyValue), then one suggestion is storing the keyvalue in a session variable and in ObjectDataSource, populate the RadComboBox  by using SessionParameter which identifies the name stored in the current Session object.

C#:
protected void RadGridAziendeForUser_ItemDataBound(object sender, GridItemEventArgs e)
   {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           GridEditableItem editItem = (GridEditableItem)e.Item;
           Session["idAzienda"] = editItem.GetDataKeyValue("idAzienda").ToString(); // store the DataKeyValue in a Session variable
           RadComboBox combo = (RadComboBox)editItem.FindControl("RepartoRadComboBox");
           combo.DataSourceID = "ObjectDataSourceRepartiAzienda";
           combo.DataTextField = " ";// assign your DataTextField
           combo.DataValueField = "";// assign your DataValueField
       }
   }

DataSource:
<asp:ObjectDataSource ID="ObjectDataSourceRepartiAzienda" runat="server"
                SelectMethod="getAllByAzienda" TypeName="MachinaWeb.Services.BO_Reparto">
       <SelectParameters>
            <asp:SessionParameter SessionField="idAzienda" Name="idAzienda" />
       </SelectParameters>
</asp:ObjectDataSource>

Hope this helps.
Princy.
Tags
Grid
Asked by
archimede
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or