Why RadGrid never delete child row after user deleting parent row?

2 posts, 0 answers
  1. Stanley
    Stanley avatar
    32 posts
    Member since:
    Apr 2012

    Posted 27 Aug 2012 Link to this post

    Please refer the video below
    http://www.youtube.com/watch?v=YwCubFXo8bg 
    From the video, you can see 5 child row record.
    After deleting a child row, child row count = 4.
    After deleting a parent row, child row count still is 4.

    For the source code, please get it from url below
    File name is  DeleteParentRow.zip 
    http://sdrv.ms/PU4ZgZ

    Auto delete child row function, any way can be done?
    Thanks.
      

  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 29 Aug 2012 Link to this post

    Hi Stanley,

    Thank you for writing.

    You can use the UserDeletingRow 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());
            }
        }
    }

    I hope this helps.

    Kind 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 >>
Back to Top