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

Remove Child From GridView

1 Answer 135 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 20 Dec 2011, 03:25 PM
I have a GridView in which the child is loaded manually via a RowSourceNeeded event.  I am using the ChildViewExpanded event to detect that the child is being collapsed (!IsExpanded).  As part of the collapse, I would like to delete all of the child rows, thus forcing another RowSourceNeeded event (Data refresh) when the child view is expanded.  I have tried the following and many others without success.

private void radGridViewDatabases_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
        {
            if (e.IsExpanded)
            {
                e.ChildRow.Height = Math.Min(90 + (e.ChildViewInfo.ChildRows.Count * 18), 500);
            }
            else
            {
                if (e.ChildViewInfo.HasChildRows())
                {
                    foreach (GridViewRowInfo oGridViewInfo in e.ChildViewInfo.Rows)
                    {
                        oGridViewInfo.Delete();
                    }
                }
            }

            return;
        }

Any ideas?

Thanks in advance


1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 23 Dec 2011, 10:13 AM
Hello Thomas,

You can Refresh data in ChildViewExpanded event handler to support a fresh copy of the data on every expand operation. Here is a simple example:

using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.Collections;
 
namespace Lab.GridView
{
    public partial class HierarchyGridForm : Form
    {
        private RadGridView gridView = new RadGridView();
        private DataTable parentTable = new DataTable();
        private DataTable childTable = new DataTable();
        private Random random = new Random((int)DateTime.Now.Ticks);
 
        public HierarchyGridForm()
        {
            InitializeComponent();
 
            gridView.Dock = DockStyle.Fill;
            this.panel1.Controls.Add(gridView);
        }
 
        private void HierarchyGridForm_Load(object sender, EventArgs e)
        {
            LoadData();
 
            CreateLoadOnDemandHierarchy();
            gridView.ChildViewExpanded += new ChildViewExpandedEventHandler(gridView_ChildViewExpanded);
        }
 
        void gridView_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
        {
            if (e.IsExpanded)
            {
                childTable.Rows[0][4] = random.Next(500);
                e.ChildViewInfo.Refresh();
                e.ChildRow.Height = Math.Min(90 + (e.ChildViewInfo.ChildRows.Count * 18), 500);
            }
        }
 
        private void CreateLoadOnDemandHierarchy()
        {
            GridViewTemplate template = new GridViewTemplate();
            template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            gridView.Templates.Add(template);
 
            GridViewTextBoxColumn idColumn = new GridViewTextBoxColumn("ID");
            GridViewTextBoxColumn parentColumn = new GridViewTextBoxColumn("ParentID");
            GridViewTextBoxColumn childColumn1 = new GridViewTextBoxColumn("ChildValue1");
            GridViewTextBoxColumn childColumn2 = new GridViewTextBoxColumn("ChildValue2");
            GridViewTextBoxColumn childColumn3 = new GridViewTextBoxColumn("ChildValue3");
 
 
            template.Columns.AddRange(idColumn,
                parentColumn,
                childColumn1,
                childColumn2,
                childColumn3);
 
            template.HierarchyDataProvider = new GridViewEventDataProvider(template);
            gridView.RowSourceNeeded += new GridViewRowSourceNeededEventHandler(gridView_RowSourceNeeded);
        }
 
        void gridView_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
        {
            DataRowView rowView = e.ParentRow.DataBoundItem as DataRowView;
            DataRow[] rows = rowView.Row.GetChildRows("MyRelation");
 
            foreach (DataRow dataRow in rows)
            {
                GridViewRowInfo row = e.Template.Rows.NewRow();
                row.Cells["ID"].Value = dataRow["ID"];
                row.Cells["ParentID"].Value = dataRow["ParentID"];
                row.Cells["ChildValue1"].Value = dataRow["ChildValue1"];
                row.Cells["ChildValue2"].Value = dataRow["ChildValue2"];
                row.Cells["ChildValue3"].Value = dataRow["ChildValue3"];
 
                e.SourceCollection.Add(row);
            }
        }
         
        private void LoadData()
        {
            parentTable.Columns.Add("ID", typeof(int));
            parentTable.Columns.Add("ParentValue1", typeof(int));
            parentTable.Columns.Add("ParentValue2");
            parentTable.Columns.Add("ParentValue3", typeof(double));
            parentTable.Rows.Add(1, 100, "Value100", 100.1);
            parentTable.Rows.Add(2, 200, "Value200", 200.2);
            parentTable.Rows.Add(3, 300, "Value300", 300.3);
            parentTable.Rows.Add(4, 400, "Value400", 400.4);
            parentTable.Rows.Add(5, 500, "Value500", 500.3);
            gridView.DataSource = parentTable;
 
            childTable.Columns.Add("ID", typeof(int));
            childTable.Columns.Add("ParentID", typeof(int));
            childTable.Columns.Add("ChildValue1", typeof(int));
            childTable.Columns.Add("ChildValue2");
            childTable.Columns.Add("ChildValue3", typeof(double));
            childTable.Rows.Add(1, 1, 10, "Child100", 10.1);
            childTable.Rows.Add(2, 1, 20, "Child200", 20.2);
            childTable.Rows.Add(3, 2, 30, "Child300", 30.3);
            childTable.Rows.Add(4, 2, 40, "Child400", 40.4);
            childTable.Rows.Add(5, 2, 50, "Child500", 50.3);
            childTable.Rows.Add(6, 4, 60, "Child600", 60.3);
            childTable.Rows.Add(7, 5, 70, "Child700", 70.3);
            childTable.Rows.Add(8, 5, 80, "Child800", 80.3);
            childTable.Rows.Add(9, 5, 90, "Child900", 90.3);
            childTable.Rows.Add(10, 5, 100, "Child1000", 100.3);
 
            DataSet set = new DataSet();
            set.Tables.Add(parentTable);
            set.Tables.Add(childTable);
 
            set.Relations.Add("MyRelation", parentTable.Columns[0], childTable.Columns[1]);
        }
    }
}

I hope this helps. 

Greetings,
Julian Benkov
the Telerik team

Q3’11
of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or