RadGrid with GridTemplateColumn with Radcombobox bind programmatically

4 posts, 0 answers
  1. archimede
    archimede avatar
    103 posts
    Member since:
    Jul 2006

    Posted 16 Sep 2011 Link to this post

    Hi,
    I explain my situation.
    I have a grid with many template columns.
    One template column (uniquename: "ValoreTextBoxComboBox")  has two controls  a label ("LabelDescrizioneI") binded and a radcombobox ("RadComboBoxValore") not binded. 
    Then programmatically  "OnItemDatabound " event  of the radgrid i decided which control is visible and in case this control is the radcombobox i bind it.
    Everything works until i click "Edit" on the radgrid, the combobox lost everithing ( data and selectedvalue) .

    <telerik:RadGrid ID="rdgParamDatiAziendali" AllowPaging="true" DataSourceID="edsGridParam" runat="server" OnUpdateCommand="rdgParamDatiAziendalie_UpdateCommand"
               GridLines="None" Width="97%" AllowSorting="true" PageSize="20" OnItemDataBound ="rdgParamDatiAziendali_ItemDataBound">    
            <MasterTableView AutoGenerateColumns="False" EditMode="InPlace" DataKeyNames="IdConfigurazioneProgrammaCultura"
               OverrideDataSourceControlSorting="true" TableLayout="Auto">
               <CommandItemTemplate>
               </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="50px" HeaderStyle-Width="50px" />
               <telerik:GridBoundColumn DataField="IdCultura" DataType="System.Int64" HeaderText="IdCultura"
                   SortExpression="IdCultura" UniqueName="IdCultura" ReadOnly="true" Visible="false">
               </telerik:GridBoundColumn>
               <telerik:GridBoundColumn DataField="IdConfigurazioneProgrammaCultura" DataType="System.Int64" HeaderText="Id" SortExpression="IdConfigurazioneProgrammaCultura"
                   UniqueName="IdConfigurazioneProgrammaCultura" Display="false">
               </telerik:GridBoundColumn>                       
                <telerik:GridBoundColumn DataField="Descrizione" DataType="System.String" HeaderText="Descrizione" SortExpression="Descrizione"
                   UniqueName="Descrizione"  ReadOnly="true" HeaderStyle-Width="405px" ItemStyle-Height="13px"
               </telerik:GridBoundColumn>
               <mwc:GridBoundColumn DataField="Id" DataType="System.Int64" HeaderText="Id" SortExpression="Id"
                   UniqueName="Id" Display="false"></mwc:GridBoundColumn
                    <mwc:GridBoundColumn DataField="IdParametroAzienda" DataType="System.Int64" HeaderText="IdParametroAzienda" SortExpression="IdParametroAzienda"
                   UniqueName="IdParametroAzienda" Display="false">
               </mwc:GridBoundColumn>
                  <telerik:GridTemplateColumn Resizable="true" HeaderStyle-Width="275px" UniqueName="ValoreTextBoxComboBox"
                       SortExpression="ValoreTextBoxComboBox" ItemStyle-Height="13px">
                       <HeaderTemplate>
                           <mwc:Label ID="LabelUnificata" runat="server" LabelResources="valore_simi"
                               ParentType="RadGrid" Height="13px"></mwc:Label>
                       </HeaderTemplate>
                       <ItemTemplate>
                           <mwc:Label ID="LabelDescrizioneI" runat="server" Text='<%# Bind("DescrizioneTextBox") %> '
                               Width="270px" ReadOnly="true" ParentType="RadGrid" Height="13px"></mwc:Label>
                           <telerik:RadComboBox ID="RadComboBoxValore" runat="server" Width="270px" AutoPostBack="false"
                               ParentType="RadGrid" ReadOnly="true" Height="13px">
                           </telerik:RadComboBox>
                           <mwc:HiddenField ID="HiddenFieldQueryRiga" runat="server" Value='<%# Bind("QueryComboBox") %>' />
                           <mwc:HiddenField ID="HiddenFieldTipoControllo" runat="server" Value='<%# Bind("IdTipoParametro") %>' />
                           <mwc:HiddenField ID="HiddenFieldSelectedValueRadComboBoxValore" runat="server" Value='<%# Bind("ValoreComboBox") %>' />
                       </ItemTemplate>
                       <EditItemTemplate>
                           <mwc:TextBox ID="TextBoxDescrizione" runat="server" Width="300px" Text='<%# Bind("DescrizioneTextBox") %>'></mwc:TextBox>
                           <telerik:RadComboBox ID="RadComboBoxValore" runat="server" Width="300px" AutoPostBack="false">
                           </telerik:RadComboBox>
                           <mwc:HiddenField ID="HiddenFieldQueryRiga" runat="server" Value='<%# Bind("QueryComboBox") %>' />
                           <mwc:HiddenField ID="HiddenFieldTipoControllo" runat="server" Value='<%# Bind("IdTipoParametro") %>' />
                           <mwc:HiddenField ID="HiddenFieldSelectedValueRadComboBoxValore" runat="server" Value='<%# Bind("ValoreComboBox") %>' />
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
                   <mwc:GridTemplateColumn Resizable="false" HeaderStyle-Width="60px" UniqueName="Abilitato"
                       ItemStyle-Height="13px">
                       <HeaderTemplate>
                           <mwc:Label ID="LabelAbilitato" runat="server" LabelResources="abilitato_simi"
                               ParentType="RadGrid" Width="60px" Height="13px"></mwc:Label>
                       </HeaderTemplate>
                       <ItemTemplate>
                           <div style="text-align: center;">
                               <mwc:CheckBox ID="CheckBoxAbilitatoI" runat="server" Checked='<%# Bind("Abilitato") %>'
                                   Enabled="false" Width="55px" Height="13px" />
                           </div>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <mwc:CheckBox ID="CheckBoxAbilitatoE" runat="server" Checked='<%# Bind("Abilitato") %>'
                               Width="55px" Height="13px" />
                       </EditItemTemplate>
                   </mwc:GridTemplateColumn>
           </Columns
           </MasterTableView
           </telerik:RadGrid>

    The c# code:
    protected void rdgParamDatiAziendali_ItemDataBound(object source, GridItemEventArgs e)
       {
           GridDataItem dataBoundItem = e.Item as GridDataItem;
           if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)
           //item is about to be edit  
           {
               long tipoParametro = long.Parse(((HiddenField)e.Item.FindControl("HiddenFieldTipoControllo")).Value);
               if (tipoParametro == (int)CommonParameters.TipoParamDatiAziendali.CheckBox_ComboBox || tipoParametro == (int)CommonParameters.TipoParamDatiAziendali.ComboBox)
                   //RadComboBoxValore load values and selectedvalue
               ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataSource = Utility.ValoriComboBox(((HiddenField)e.Item.FindControl("HiddenFieldQueryRiga")).Value, long.Parse(hdfIdCultura.Value));
               ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataValueField = "Valore";
               ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataTextField = "Descrizione";
               ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).SelectedValue = ((HiddenField)e.Item.FindControl("HiddenFieldSelectedValueRadComboBoxValore")).Value;
               try
               {
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataBind();
               }
               catch (ArgumentOutOfRangeException exc)
               {
                   ErrorMessage.generatePopUpError(RadWindowErrori, ((SessionInformation)Session["sessionData"]).IdCultura, exc, null);
               }
               try
               {
                   switch (tipoParametro)
                   {
                       case (int)CommonParameters.TipoParamDatiAziendali.CheckBox :
                           //CheckBox
                           ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).Visible = false;
                           ((TextBox)e.Item.FindControl("TextBoxDescrizione")).Visible = false;
                           ((CheckBox)e.Item.FindControl("CheckBoxAbilitatoE")).Visible = true;
                           break;
                       case (int)CommonParameters.TipoParamDatiAziendali.CheckBox_ComboBox :
                           //CheckBox-ComboBox
                           ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).Visible = true;
                           ((TextBox)e.Item.FindControl("TextBoxDescrizione")).Visible = false;
                           ((CheckBox)e.Item.FindControl("CheckBoxAbilitatoE")).Visible = true;
                           break;
                       case (int)CommonParameters.TipoParamDatiAziendali.TextBox :
                           //TextBox
                           ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).Visible = false;
                           ((TextBox)e.Item.FindControl("TextBoxDescrizione")).Visible = true;
                           ((CheckBox)e.Item.FindControl("CheckBoxAbilitatoE")).Visible = false;
                           break;
                       case (int)CommonParameters.TipoParamDatiAziendali.ComboBox :
                           //ComboBox
                           ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).Visible = true;
                           ((TextBox)e.Item.FindControl("TextBoxDescrizione")).Visible = false;
                           ((CheckBox)e.Item.FindControl("CheckBoxAbilitatoE")).Visible = false;
                           break;
                       case (int)CommonParameters.TipoParamDatiAziendali.CheckBox_TextBox :
                           //CheckBox-TextBox
                           ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).Visible = false;
                           ((TextBox)e.Item.FindControl("TextBoxDescrizione")).Visible = true;
                           ((CheckBox)e.Item.FindControl("CheckBoxAbilitatoE")).Visible = true;
                           break;
                   }
               }
               catch (NullReferenceException)
               {
               }
                 
           }
           else if (e.Item is GridDataItem)
           {
               //RadComboBoxValore load values and selectedvalue
               if (!String.IsNullOrEmpty(((HiddenField)e.Item.FindControl("HiddenFieldQueryRiga")).Value) && (((HiddenField)e.Item.FindControl("HiddenFieldTipoControllo")).Value == "4"))
               {
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataSource = Utility.ValoriComboBox(((HiddenField)e.Item.FindControl("HiddenFieldQueryRiga")).Value, 0);
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataValueField = "Key";
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataTextField = "Value";
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).SelectedValue = ((HiddenField)e.Item.FindControl("HiddenFieldSelectedValueRadComboBoxValore")).Value;
                   ((Label)e.Item.FindControl("LabelDescrizioneI")).Visible = false;
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).DataBind();
               }
               else
               {
                   ((RadComboBox)e.Item.FindControl("RadComboBoxValore")).Visible = false;
               }
           }
       }

    I really don't understand why the radcombobox lost everithing when i click the Edit column. I used the same tecniche to fill the combo if " IsInEditMode" or  if (e.Item is GridDataItem).

    Thanks everybody !!!!

     

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Sep 2011 Link to this post

    Hello Archimede,

    The following code snippet worked as expected at my end. Please make a double check with your code. Here is a sample code.
    C#:
    protected void grid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
                GridEditableItem item = (GridEditableItem)e.Item;
                RadComboBox combo = ((RadComboBox)item.FindControl("RadComboBoxValore"));
                combo.DataSource = SqlDataSource2;
                combo.DataValueField = "EmployeeID";
                combo.DataTextField = "EmployeeID";
                combo.DataBind();   
            }
        }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. archimede
    archimede avatar
    103 posts
    Member since:
    Jul 2006

    Posted 19 Sep 2011 Link to this post

    Hi Shinu,
    As you said, if i put as combobox.Datasource  an specific datasource ( I try entitydatasource). It works.
    But in my case, i have multiple radcombox and any one of then has different datasource.
    Because i didn't want to create a lot of datasources, i create in an external class an static methos that executes the query and return a hashtable of data.

    public static Hashtable ValoriComboBox(string _query, long _idCultura)
        {
            //_query = "SELECT DISTINCT(ValoreComboBox), DescrizioneTextBox, IdCultura FROM [ArchiviServizio].[Configurazione_Programma_Valori_Culture]";
            using (DMWEntities context = new DMWEntities())
            {
                Hashtable lista = new Hashtable();
     
                if (!String.IsNullOrEmpty(_query))
                {
                    ObjectResult<EseguiQuery_Result> listaValoriDescrizioniPerComboBox = context.EseguiQuery(_query, _idCultura);
     
                    foreach (EseguiQuery_Result lvd in listaValoriDescrizioniPerComboBox)
                    {
                        lista.Add(lvd.Valore, lvd.Descrizione);
                    }
                }
     
                return lista;
            }
        }

    But as i said before. It works for ItemData but on edit, i loss everything.
    Where is the mistake?.

    Thanks again!!
  5. archimede
    archimede avatar
    103 posts
    Member since:
    Jul 2006

    Posted 19 Sep 2011 Link to this post

    Hi Shinu,
    I resolved the problem, it was a parameter not inizialized correctly.
    Now it works !!!!
    :)
Back to Top