to implement this functionality. Here is the changed example:
using
System;
using
System.Data;
using
System.Windows.Forms;
using
Telerik.WinControls.UI;
namespace
ChildGridLoadOnDemand
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
radGridView1.UserDeletingRow +=
new
GridViewRowCancelEventHandler(radGridView1_UserDeletingRow);
}
void
radGridView1_UserDeletingRow(
object
sender, GridViewRowCancelEventArgs e)
{
GridViewTemplate childTemplate = radGridView1.Templates[0];
using
(childTemplate.DeferRefresh())
{
for
(
int
i = 0; i < e.Rows.Length; i++)
{
int
index = 0;
while
(index < childTemplate.Rows.Count)
{
if
(((
int
)childTemplate.Rows[index].Cells[
"ID"
].Value).CompareTo((
int
)e.Rows[i].Cells[
"ID"
].Value) == 0)
{
childTemplate.Rows.RemoveAt(index);
}
else
{
index++;
}
}
}
}
}
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;
}
private
void
radGridView1_UserDeletedRow(
object
sender, GridViewRowEventArgs e)
{
MessageBox.Show(((DataTable)radGridView1.Templates[0].DataSource).Rows.Count.ToString());
}
}
}