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

Using RadGridView in Unbound mode and Load-On-Demand

6 Answers 328 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Henri
Top achievements
Rank 1
Henri asked on 24 Apr 2014, 02:43 PM
Hi,

Is there a sample that shows me how use use a RadGridView in Unbound Mode, 4 Levels deep, using Load-On-Demand?
I found samples for Unbound Mode, samples for Load-On-Demand, but nothing for a combination of the two.

Thanks...

6 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Apr 2014, 11:04 AM
Hello Henri,

Thank you for writing.

In order to achieve your goal, please follow the code snippet below:
public Form1()
{
    InitializeComponent();
 
    this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
    this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
    GridViewRowInfo rowInfo = this.radGridView1.Rows.AddNew();
    rowInfo.Cells[0].Value = "A1";
    rowInfo.Cells[1].Value = "B1";
    rowInfo = this.radGridView1.Rows.AddNew();
    rowInfo.Cells["A"].Value = "A2";
    rowInfo.Cells["B"].Value = "B2";
    this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    GridViewTemplate template1 = new GridViewTemplate();
    template1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    template1.Columns.Add("C");
    template1.Columns.Add("D");
    this.radGridView1.MasterTemplate.Templates.Add(template1);
    template1.HierarchyDataProvider = new GridViewEventDataProvider(template1);
    this.radGridView1.RowSourceNeeded += new GridViewRowSourceNeededEventHandler(radGridView1_RowSourceNeeded);
 
    GridViewTemplate template2 = new GridViewTemplate();
    template2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    template2.Columns.Add("E");
    template2.Columns.Add("F");
    template1.Templates.Add(template2);
    template2.HierarchyDataProvider = new GridViewEventDataProvider(template2);
 
    GridViewTemplate template3 = new GridViewTemplate();
    template3.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    template3.Columns.Add("G");
    template3.Columns.Add("H");
    template2.Templates.Add(template3);
    template3.HierarchyDataProvider = new GridViewEventDataProvider(template3);
}
 
void radGridView1_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        GridViewRowInfo row = e.Template.Rows.NewRow();
 
        switch (e.ParentRow.HierarchyLevel)
        {
            case 0:
                row.Cells["C"].Value = "C" + i;
                row.Cells["D"].Value = "D" + i;
                break;
            case 1:
                row.Cells["E"].Value = "E" + i;
                row.Cells["F"].Value = "F" + i;
                break;
            case 2:
                row.Cells["G"].Value = "G" + i;
                row.Cells["H"].Value = "H" + i;
                break;
            default:
                break;
        }
 
        e.SourceCollection.Add(row);
    }
}

Our Unbound Mode and Load-On-Demand Hierarchy help articles introduce more detailed information.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Henri
Top achievements
Rank 1
answered on 28 Oct 2014, 08:43 AM
Hello,

That worked fine, but now I have another problem.
I want to add a new row programmatically on the first child level.
So, in a Button-Click Event I added the following code:

GridViewRowInfo child = radGridView1.MasterTemplate.Templates[0].Rows.AddNew();

The child is ALWAYS null, I cannot seem to add a row to any child level.

I upgraded to the latest Version Q3'14 but that didn't help.

Can you please point me in the right direction?

Thanks!
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Oct 2014, 07:48 AM
Hello Henri,

Thank you for writing back.

When you use load on demand approach for populating the grid templates with data, the RowSourceNeeded event is the place where you can add rows for the respective level. That is why the AddNew method returns null in this case. I would suggest you to store the data for each template in a DataTable for example and on the RadButton.Click event to add a new row for the respective DataTable. In order to force the RowSourceNeeded event to fire you should call the Refresh method for the respective template. Thus, you will fill the template's data from the DataTable containing the new row.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Henri
Top achievements
Rank 1
answered on 29 Oct 2014, 11:53 AM
Hello Desislave,

This means that in order to be able to add a row, the gridview has to be bound, instead of unbound.
Is there a way to mimic load-on-demand with for instance the Expanding event?
This way the grid can remain unbound.

Regards,
Henri
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Oct 2014, 09:20 AM
Hello Henri,

Thank you for writing back.

The MasterTemplate.Rows.AddNew method successfully adds a new row to the master template as it is not in load on demand mode. It uses unbound mode as it is demonstrated in the code snippet below. Load on demand hierarchy approach requires a HierarchyDataProvider for the child GridViewTemplate where in the RowSourceNeeded event you can populate the desired child rows on demand ,e.g. when the master row is expanded. This provider is responsible for the data population. In order to add a new row to the first child level when a button is clicked you can use a simple flag or counter to indicate how many rows to be added to the specific template. Hence, when the button is clicked you can increase the counter and refresh the respective template in order to force the RowSourceNeeded. However, it is up to you how you will store the data that will be added.
public Form1()
{
    InitializeComponent();
 
    this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
    this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
    GridViewRowInfo rowInfo = this.radGridView1.Rows.AddNew();
    rowInfo.Cells[0].Value = "A1";
    rowInfo.Cells[1].Value = "B1";
    rowInfo = this.radGridView1.Rows.AddNew();
    rowInfo.Cells["A"].Value = "A2";
    rowInfo.Cells["B"].Value = "B2";
    this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    GridViewTemplate template1 = new GridViewTemplate();
    template1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    template1.Columns.Add("C");
    template1.Columns.Add("D");
    this.radGridView1.MasterTemplate.Templates.Add(template1);
    template1.HierarchyDataProvider = new GridViewEventDataProvider(template1);
    this.radGridView1.RowSourceNeeded += new GridViewRowSourceNeededEventHandler(radGridView1_RowSourceNeeded);
 
    GridViewTemplate template2 = new GridViewTemplate();
    template2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    template2.Columns.Add("E");
    template2.Columns.Add("F");
    template1.Templates.Add(template2);
    template2.HierarchyDataProvider = new GridViewEventDataProvider(template2);
 
    GridViewTemplate template3 = new GridViewTemplate();
    template3.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    template3.Columns.Add("G");
    template3.Columns.Add("H");
    template2.Templates.Add(template3);
    template3.HierarchyDataProvider = new GridViewEventDataProvider(template3);
}
 
void radGridView1_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
{
    switch (e.ParentRow.HierarchyLevel)
    {
        case 0:
            for (int i = 0; i < counterLevel0; i++)
            {
                GridViewRowInfo row = e.Template.Rows.NewRow();
                row.Cells["C"].Value = "C" + i;
                row.Cells["D"].Value = "D" + i;  
                e.SourceCollection.Add(row);
            }
                
            break;
        case 1:
            for (int i = 0; i < counterLevel1; i++)
            {
                GridViewRowInfo row = e.Template.Rows.NewRow();
                row.Cells["E"].Value = "E" + i;
                row.Cells["F"].Value = "F" + i;
                e.SourceCollection.Add(row);
            }
             
            break;
        case 2:
            for (int i = 0; i < counterLevel2; i++)
            {
                GridViewRowInfo row = e.Template.Rows.NewRow();
                row.Cells["G"].Value = "G" + i;
                row.Cells["H"].Value = "H" + i;
                e.SourceCollection.Add(row);
            }
             
            break;
        default:
            break;
    }
}
 
int counterLevel0 = 5;
int counterLevel1 = 5;
int counterLevel2 = 5;
 
private void radButton1_Click(object sender, EventArgs e)
{
    counterLevel0++;
    this.radGridView1.MasterTemplate.Templates[0].Refresh();
}

I would like to note that RadGridView allows different approaches for setting up the hierarchy: bound mode, unbound mode, load on demand. These are separate ways which manipulate data differently. You can find additional information in the referred help articles.

I hope this information helps. If you have any additional questions, please let me know.
 
Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Henri
Top achievements
Rank 1
answered on 30 Oct 2014, 10:30 AM
Hello Desislava,

Thank you, I came to the same solution here on my side, I created a child Row, placed it in a private variable, forced a refresh, and added it to the SourceCollection when it was not null.

Best regards,
Henri
Tags
GridView
Asked by
Henri
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Henri
Top achievements
Rank 1
Share this question
or