Nested Radgrid - Duplicate Parent Removal

3 posts, 0 answers
  1. Keith
    Keith avatar
    23 posts
    Member since:
    Jan 2013

    Posted 11 Jul Link to this post

    I have a Radgrid nested two levels, parent/child.  When loading the grid I get all values in the parent grid when I just want distinct values on the parent.

     

    <telerik:RadGrid RenderMode="Lightweight" ID="grdChartofAccounts" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowMultiRowSelection="false" OnNeedDataSource="grdChartofAccounts_NeedDataSource">
        <MasterTableView DataKeyNames="JobID" CommandItemDisplay="Bottom">
            <DetailTables>
                <telerik:GridTableView>
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="JobID" MasterKeyField="JobID" />
                    </ParentTableRelation>
     
                    <Columns>
                        <telerik:GridBoundColumn HeaderText="Task" UniqueName="Task" DataField="TaskID_Job" />
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
                <telerik:GridBoundColumn HeaderText="Job" UniqueName="Job" DataField="JobID" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

     

    protected void grdChartofAccounts_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        List<tasks> taskList = new List<tasks>();
     
        for (int i = 1; i <= 5; i++)
            for (int x = 1; x <= 5; x++)
            {
                tasks task = new tasks();
     
                task.JobID = i;
                task.TaskID = x;
     
                taskList.Add(task);
            }
     
        DataTable dt = new DataTable();
     
        dt.Columns.Add("JobID");
        dt.Columns.Add("TaskID_Job");
     
        foreach (tasks task in taskList)
        {
            DataRow dr = dt.NewRow();
     
            dr["JobID"] = task.JobID.ToString();
            dr["TaskID_Job"] = task.TaskID;
     
            dt.Rows.Add(dr);
        }
     
        this.grdChartofAccounts.DataSource = dt;
    }

     

    I've attached two images of my existing output and my desired output.

  2. Keith
    Keith avatar
    23 posts
    Member since:
    Jan 2013

    Posted 11 Jul in reply to Keith Link to this post

    I was able to resolve my issue by scanning the grid during PreRender and toggling the display to False if it was a duplicate.  This may not be the best approach but it works.

     

     

    protected void grdChartofAccounts_PreRender(object sender, EventArgs e)
    {
        string lastJobID = string.Empty;
        bool first = true;
     
        foreach (GridDataItem item in this.grdChartofAccounts.MasterTableView.Items)
        {
            if (item["JobID"].Text == lastJobID)
                item.Display = false;
            else
                lastJobID = item["JobID"].Text;
        }
    }
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Keith
    Keith avatar
    23 posts
    Member since:
    Jan 2013

    Posted 12 Jul Link to this post

    OK that didn't work either.  There were too many "but now this doesn't work" events.  I found the actual solution here: http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/programmatic-hierarchy/defaultcs.aspx

     

    Following this demo I was able to bind both the parent and child grid separately the way Telerik intended.

Back to Top