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

Nested Radgrid - Duplicate Parent Removal

2 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Keith
Top achievements
Rank 1
Keith asked on 11 Jul 2016, 05:21 PM

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 Answers, 1 is accepted

Sort by
0
Keith
Top achievements
Rank 1
answered on 11 Jul 2016, 05:49 PM

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;
    }
}
0
Keith
Top achievements
Rank 1
answered on 12 Jul 2016, 12:20 PM

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.

Tags
Grid
Asked by
Keith
Top achievements
Rank 1
Answers by
Keith
Top achievements
Rank 1
Share this question
or