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

RadGrid doesn't sort a column populated in ItemDataBound

1 Answer 13 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Emanuele
Top achievements
Rank 1
Veteran
Emanuele asked on 27 Nov 2020, 02:38 PM

Hi, i've a problem sorting a RadGrid

When i try to sort, rows data are sorted all except the last column "Allegati" that remains fixed. That column is compiled on the ItemDataBound event, based on the datatable. How can I correct this behavior? Radgrid is under Ajax. Thanks

<telerik:RadGrid ID="rgRichieste" runat="server" CellSpacing="-1" Culture="it-IT" GridLines="Both" AllowSorting="True" AutoGenerateColumns="False" PageSize="5" ShowStatusBar="True" AllowPaging="True"
        MasterTableView-NoMasterRecordsText="Nessun record da visualizzare." RenderMode="Lightweight" OnItemDataBound="rgRichieste_ItemDataBound" OnNeedDataSource="rgRichieste_NeedDataSource" FilterMenu-EnableScreenBoundaryDetection="True" Enabled="True">
    <SortingSettings SortToolTip="Click qui per ordinare" SortedAscToolTip="ordinato crescente" SortedDescToolTip="ordinato decrescente"></SortingSettings>
 
    <ExportSettings FileName="richieste" IgnorePaging="True" ExportOnlyData="True" OpenInNewWindow="True">
        <Pdf PageWidth="" AllowPrinting="False"></Pdf>
    </ExportSettings>
 
    <MasterTableView DataKeyNames="IDCompilazione">
 
        <RowIndicatorColumn Visible="False">
        </RowIndicatorColumn>
        <ExpandCollapseColumn Created="True">
        </ExpandCollapseColumn>
     
        <Columns>
            <telerik:GridBoundColumn DataField="IDModulo" HeaderText="ID" SortExpression="IDModulo" UniqueName="IDModulo" FilterControlAltText="Filter IDModulo column" Visible="False">
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn DataField="IDCompilazione" HeaderText="ID" SortExpression="IDCompilazione" UniqueName="IDCompilazione" FilterControlAltText="Filter IDCompilazione column" Visible="False">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="NumeroDomanda" HeaderText="#" SortExpression="NumeroDomanda" UniqueName="NumeroDomanda" FilterControlAltText="Filter NumeroDomanda column" Visible="True">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Data" HeaderText="Data" SortExpression="Data" UniqueName="Data" DataType="System.DateTime" FilterControlAltText="Filter Data column" DataFormatString="{0:dd/MM/yyyy}">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Nome" HeaderText="Nome" SortExpression="Nome" UniqueName="Nome" FilterControlAltText="Filter Nome column">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Cognome" HeaderText="Cognome" SortExpression="Cognome" UniqueName="Cognome" FilterControlAltText="Filter Cognome column" >
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="CodCorso" HeaderText="Codice CdS" SortExpression="CodCorso" UniqueName="CodCorso" FilterControlAltText="Filter CodCorso column" >
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn HeaderText="Allegati" UniqueName="Allegati" >
            </telerik:GridBoundColumn>
 
            <telerik:GridTemplateColumn HeaderText="Azione" AllowFiltering="false" UniqueName="Azione" Visible="True">
                <ItemTemplate>
                     
                    <telerik:RadButton ID="btnMerge" runat="server" CssClass="rbLeftMargin" Text="Unisce file" ButtonType="StandardButton" ToolTip="unisce i file allegati in un unico PDF" OnClick="btnMerge_Click" CommandArgument='<%# Eval("IDCompilazione").ToString()%>'>
                        <ContentTemplate>
                            <i class="fa fa-compress"></i>
                        </ContentTemplate>
                    </telerik:RadButton>
 
                    <telerik:RadButton ID="btnDownloadFile" runat="server" CssClass="rbLeftMargin" Text="Scarica file" ButtonType="LinkButton" ToolTip="scarica PDF unico" CommandArgument='<%# Eval("IDCompilazione").ToString()%>' Target="_blank" UseSubmitBehavior="False" AutoPostBack="False">
                        <ContentTemplate>
                            <i class="fa fa-file-pdf-o"></i>
                        </ContentTemplate>
                    </telerik:RadButton>
 
                    <telerik:RadButton ID="btnModificaAllegati" runat="server" CssClass="rbLeftMargin" Text="Modifica allegati" ButtonType="LinkButton" ToolTip="modifica allegati" CommandArgument='<%# Eval("IDCompilazione").ToString()%>' Target="_blank" UseSubmitBehavior="False" AutoPostBack="False" OnClientClicked="ShowExistingW">
                        <ContentTemplate>
                            <i class="fa fa-files-o"></i>
                        </ContentTemplate>
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
     </MasterTableView>
</telerik:RadGrid>

 

Here the code:

protected void rgRichieste_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
     // salva i dati in memoria
    _dt = pc.GetDataTable(_query, connDb);
    // bind
    rgRichieste.DataSource = _dt;
}
 
protected void rgRichieste_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    int i = 0;
 
    string path = Master.targetFolder;
 
    if (e.Item is GridDataItem) // GridDataItem
    {
        GridDataItem item = (GridDataItem)e.Item;
 
        int id = (int)item.OwnerTableView.DataKeyValues[item.ItemIndex]["IDCompilazione"];
        int indiceRiga = item.DataSetIndex;
 
        // link ai file allegati
 
         // IDCorsoDiStudio serve per puntare alla cartella corretta
        string idCdS = _dt.Rows[indiceRiga]["IDCorsoDiStudio"].ToString();
 
        // imposta lista campi in funzione del modulo
        string listaCampi = "";
        int idModulo = Convert.ToInt32(_dt.Rows[indiceRiga]["IDModulo"].ToString());
        if (idModulo == 1)
            listaCampi = ProcComuni.listaCampiNome[0]; //"FileCurriculumVitae,FileBachelorDegree,FilePersonalStatement";
        else
            listaCampi = ProcComuni.listaCampiNome[1]; // "FileDeclarationForm,FileCurriculumVitae,FileResidenceCertificate,FileUniversityDegree,FileAcademicRecord,FileEnglishProficiency,FileIdentityDocument,FileMotivationLetter,FileReferenceLetter1,FileReferenceLetter2,FileOther";
 
        string[] hlFields = listaCampi.Split(new Char[] { ',' });
        int nFields = hlFields.Count<string>();
        // per ogni campo crea il link
        int nCampi = 0;
        foreach (string hlField in hlFields)
        {
            nCampi++;
            string nomeCampo = hlField.Trim();
            // questo valore potrebbe essere a sua volta una lista separata da '|'
            string valoreCampo = _dt.Rows[indiceRiga][nomeCampo].ToString();
 
            if (valoreCampo != "")
            {
                string[] nomiSingoli = valoreCampo.Split(new Char[] { '|' });
 
                int nNomi = 0;
                foreach (string nomeSingolo in nomiSingoli)
                {
                    nNomi++;
                    string nome = nomeSingolo.Trim();
 
                    HyperLink lnkFile = new HyperLink();
                    lnkFile.Text = nome;
                    lnkFile.NavigateUrl = path + idCdS + "/" + lnkFile.Text;
                    lnkFile.Target = "_blank";
 
                    if (nNomi > 1)
                        item["Allegati"].Controls.Add(new LiteralControl(" | "));
 
                    item["Allegati"].Controls.Add(lnkFile);
                }
 
                if (nCampi < nFields)
                    item["Allegati"].Controls.Add(new LiteralControl("<br />"));
            }
        }
 
        // imposta i pulsanti per la gestione del pdf unico
        string fileUnito = _dt.Rows[indiceRiga]["FileUnito"].ToString();
        RadButton rbMerge = (RadButton)item["Azione"].FindControl("btnMerge");
        RadButton rbDownloadFile = (RadButton)item["Azione"].FindControl("btnDownloadFile");
 
        if (idModulo < 2)
        {
            rbMerge.Visible = false;
            rbDownloadFile.Visible = false;
        }
        else
        {
            // modulo 2,3: gestione abilitata
 
            switch (fileUnito)
            {
                case "":
                    rbDownloadFile.Visible = false;
                    break;
 
                case "ERRORE":
                    rbDownloadFile.Visible = false;
                    rbMerge.BackColor = System.Drawing.Color.Bisque;
                    rbMerge.ToolTip = "Ci sono stati problemi nell'ultimo tentativo di unione.";
                    break;
 
                default:
                    rbMerge.Visible = false;
                    rbDownloadFile.NavigateUrl = path + idCdS + "/" + fileUnito;
                    break;
            }
        }
    }
}

 

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 02 Dec 2020, 12:24 PM

Hi Emanuele,

The Allegati column is a bound column but is not bound initially, while some items are created in the ItemDataBound event inside it. Can you, please, ensure, that the column is bound to the target database field and see if the problem persists? Can you also try replacing it with GridTemplate column?

                    <telerik:GridBoundColumn HeaderText="Allegati" UniqueName="Allegati">
                    </telerik:GridBoundColumn>

Regards,
Vessy
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Emanuele
Top achievements
Rank 1
Veteran
Answers by
Vessy
Telerik team
Share this question
or