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

Why the radGridView never trigger RowSourceNeeded? Load-On-Demand Hierarchy

1 Answer 155 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stanley
Top achievements
Rank 1
Stanley asked on 24 Aug 2012, 11:12 AM
Why radGridView1_RowSourceNeeded cannot be accessed?
When I click expand, system never fire RowSourceNeeded event?

What I want to do under RowSourceNeeded is:
1. Trigger SQL statement
2. Fill to datatable
3. Foreach DataTable.Row fill in to child radGridView.

private void radGridView1_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
{
    MessageBox.Show("Yes");
}


Please download my souce code from link below
file name is ChildGridLoadOnDemand.zip
http://sdrv.ms/T2TNNA

1 Answer, 1 is accepted

Sort by
0
Accepted
Julian Benkov
Telerik team
answered on 28 Aug 2012, 03:10 PM
Hi Stanley,

Thank you for writing.

The problem is the added GridViewRelation in your application. In the load-on-demand hierarchy mode the GridViewRelation is not needed for the setup. Please comment the GridViewRelation related code and RowSourceNeeded event will be raised properly.
using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace ChildGridLoadOnDemand
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            populateGridHeader();
        }
 
        private void populateGridHeader()
        {
            CreateMasterTemplate();
 
            GridViewTemplate childTemplate = CreateChildTemplate();
            childTemplate.DataSource = GetDetTable();
 
            //GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate, childTemplate);
            //relation.RelationName = "childTemplate";
            //relation.ParentColumnNames.Add("ID");
            //relation.ChildColumnNames.Add("ID");
 
            //this.radGridView1.Relations.Add(relation);
 
            this.radGridView1.RowSourceNeeded += new GridViewRowSourceNeededEventHandler(radGridView1_RowSourceNeeded);
        }
 
        private void radGridView1_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
        {
            MessageBox.Show("Yes");
        }
 
        private void CreateMasterTemplate()
        {
 
            //ID
            GridViewTextBoxColumn ColID = new GridViewTextBoxColumn();
            ColID.FieldName = "ID";
            ColID.HeaderText = "ID";
            ColID.Name = "ID";
            ColID.Width = 100;
            radGridView1.MasterTemplate.Columns.Add(ColID);
 
            //Brand
            GridViewTextBoxColumn ColBrand = new GridViewTextBoxColumn();
            ColBrand.FieldName = "Brand";
            ColBrand.HeaderText = "Brand";
            ColBrand.Name = "Brand";
            ColBrand.Width = 100;
            radGridView1.MasterTemplate.Columns.Add(ColBrand);
 
            radGridView1.DataSource = GetHdrTable();
        }
         
        private GridViewTemplate CreateChildTemplate()
        {
            GridViewTemplate childTemplate = new GridViewTemplate();
            this.radGridView1.Templates.Add(childTemplate);
             
            //ID
            GridViewTextBoxColumn ColID = new GridViewTextBoxColumn();
            ColID.FieldName = "ID";
            ColID.HeaderText = "ID";
            ColID.Name = "ID";
            ColID.Width = 100;
            childTemplate.Columns.Add(ColID);
 
            //Model
            GridViewTextBoxColumn ColumnModel = new GridViewTextBoxColumn();
            ColumnModel.FieldName = "Model";
            ColumnModel.HeaderText = "Model";
            ColumnModel.Name = "Model";
            ColumnModel.Width = 100;
            childTemplate.Columns.Add(ColumnModel);
 
            childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            childTemplate.HierarchyDataProvider = new GridViewEventDataProvider(childTemplate);
 
            return childTemplate;
        }
 
        static DataTable GetHdrTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Brand", typeof(string));
            //
            // Here we add five DataRows.
            //
            table.Rows.Add(1, "Apple");
            table.Rows.Add(2, "Samsung");
 
            return table;
        }
 
        static DataTable GetDetTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Model", typeof(string));
            //
            // Here we add five DataRows.
            //
            table.Rows.Add(1, "Ipad");
            table.Rows.Add(1, "Iphone");
            table.Rows.Add(1, "Macbook");
            table.Rows.Add(2, "Galaxy Tab");
            table.Rows.Add(2, "Galaxy S3");
 
            return table;
        }
    }
}

I hope this helps.

Regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Stanley
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or