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

Binding DetailTable to List<> in MasterTable DataSource

2 Answers 163 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John Mann
Top achievements
Rank 1
John Mann asked on 12 Jan 2017, 10:25 PM

I am using a custom class as the DataSource for a radgrid. My class has a structure like this (greatly simplified).

 

public class Batch
{
  public Batch() { }
 
  public int BatchID { get; set; }
  public string FirstName { get; set; }
  public string MiddleName { get; set; }
  public string LastName { get; set; }
  public DateTime BatchDate { get; set; }
  public List<Payment> Payments { get; set; }
}
 
public class Payment
{
  public Payment() { }
 
  public int PaymentID { get; set; }
  public string CheckNumber { get; set; }
  public decimal Amount { get; set; }
}

My grid is defined like this.

<telerik:RadGrid ID="RadGrid1" runat="server">
  <mastertableview datakeynames="BatchID">
    <Columns>
      <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" UniqueName="gbcFirstName" />
      <telerik:GridBoundColumn DataField="MiddleName" HeaderText="Middle Name" UniqueName="gbcMiddleName" />
      <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" UniqueName="gbcLastName" />
      <telerik:GridBoundColumn DataField="BatchDate" HeaderText="Batch Date" UniqueName="gbcBatchDate" />
    </Columns>
    <DetailTables>
      <telerik:GridTableView runat="server" HierarchyLoadMode="ServerBind" Name="gtvPayments" DataKeyNames="Payments.BatchID">
        <ParentTableRelation>
          <telerik:GridRelationFields MasterKeyField="BatchID" DetailKeyField="Payments.BatchID" />
        </ParentTableRelation>
        <Columns>
          <telerik:GridBoundColumn DataField="Payments.CheckNumber" HeaderText="Check Number" UniqueName="gbcCheckNumber" />
          <telerik:GridNumericColumn DataField="Payments.Amount" HeaderText="Amount" UniqueName="gncAmount" />
        </Columns>
      </telerik:GridTableView>
    </DetailTables>
  </mastertableview>
</telerik:RadGrid>

 

I assign the datasource to the grid in the NeedDataSource event.

void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
  List<Batch> batch = new List<Batch>(1500);
  RadGrid1.DataSource = batch;
}

Since each row in the master table datasource contains a list of its detail items is it possible to create a Master/Detail grid relationship directly from this or do I need to do something in the DetailTableDataBind event?

Thanks

2 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 17 Jan 2017, 04:16 PM
Hello,

Find attached a sample page which shows how you can achieve the desired functionality. Give it a try and let me know if you need additional assistance.

Regards,
Pavlina
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
John Mann
Top achievements
Rank 1
answered on 26 Jan 2017, 07:38 PM

Thank you, Pavlina. Yes, that works and is the way I have done it previously, but I was hoping that since each row of my data source contains it's own detail information that there would be a way to do the bind declaratively.

John

Tags
Grid
Asked by
John Mann
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
John Mann
Top achievements
Rank 1
Share this question
or