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

RadGridView fire RowSourceNeeded event

5 Answers 308 Views
GridView
This is a migrated thread and some comments may be shown as answers.
tphan
Top achievements
Rank 1
tphan asked on 10 Feb 2012, 06:17 AM

Do you know how to fire a RowSourceNeeded event manually ?

e.g. RowSourceNeeded(sender, new GridViewRowSourceNeededEventArgs(xxx,xxx,xxx));

Thanks.

 

 

5 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 14 Feb 2012, 03:37 PM
Hello Charles,

You can not call RowSourceNeeded event manually. To refresh the current values using RowSourceNeeded event in your RadGridView control in virtual mode, you can call the Update method of TableElement. Here is a sample:

private void button1_Click(object sender, EventArgs e)
{
    radGridView.TableElement.Update(GridUINotifyAction.DataChanged);
}

I hope this helps.

Kind regards,
Julian Benkov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
tphan
Top achievements
Rank 1
answered on 16 Feb 2012, 08:00 AM
That's Good !
Further more question, if the RadGridView contains master template and chlidren templates... How can I acheive the same result as well ?
0
Julian Benkov
Telerik team
answered on 20 Feb 2012, 02:27 PM
Hi Charles,

In order to refresh the ChildRows in the hierarchy mode, you should call the Refresh method of the MasterTemplate:
radGridView.MasterTemplate.Refresh();

I hope this helps.

All the best,
Julian Benkov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
tphan
Top achievements
Rank 1
answered on 20 Feb 2012, 03:03 PM
Hi Julian,

I have RadGridView in hierarchy mode, I have expand the children template in several layers or levels.... but when I expand other master template.. the expansion of children template is lost. How can I keep it remain expands when master template is refresh()... since when I call refresh() for the master template, the children template become collapse =(

As you can see in the fiqure 2, I add a new row at the bottom and call refresh() it... the master template is refresh() and it made the children template in other branch collapse.....

or I update any upper levels...the below levels is collapse.


0
Julian Benkov
Telerik team
answered on 23 Feb 2012, 10:23 AM
Hello Charles,

To achieve this scenario, you should save and restore the ChildRow before and after the Refresh method call using some data field like a key in a HashSet. Here is a sample:

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.GridView
{
    public partial class SaveRestoreHierarchyGrid : 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);
        private HashSet<object> expanded = new HashSet<object>();
 
        public SaveRestoreHierarchyGrid()
        {
            InitializeComponent();
 
            gridView.Dock = DockStyle.Fill;
            this.Controls.Add(gridView);
            this.button1.BringToFront();
        }
 
        private void HierarchyGridForm_Load(object sender, EventArgs e)
        {
            LoadData();
            CreateRelationHierarchy();
        }
 
        private void CreateRelationHierarchy()
        {
            GridViewTemplate template = new GridViewTemplate();
            template.DataSource = childTable;
            gridView.Templates.Add(template);
 
            GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
            GridViewSummaryItem summaryItem = new GridViewSummaryItem("ChildValue3", "", GridAggregateFunction.Sum);
            summaryItem.FormatString = "{0:N3}";
 
            summaryRowItem.Add(summaryItem);
            template.SummaryRowsBottom.Add(summaryRowItem);
 
            GridViewRelation relation = new GridViewRelation(gridView.MasterTemplate, template);
            relation.ChildColumnNames.Add("ParentID");
            relation.ParentColumnNames.Add("ID");
            gridView.Relations.Add(relation);
        }
 
        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]);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            this.expanded = SaveExpanded(this.gridView);
 
            gridView.DataSource = null;
            gridView.DataSource = parentTable;
 
            RestoreExpanded(this.expanded, this.gridView);
        }
 
        private void RestoreExpanded(HashSet<object> expanded, RadGridView rg)
        {
            foreach (var item in rg.Rows)
            {
                if (expanded.Contains(((DataRowView)item.DataBoundItem)[0]))
                {
                    item.IsExpanded = true;
                }
            }
        }
 
        private HashSet<object> SaveExpanded(RadGridView rg)
        {
            HashSet<object> items = new HashSet<object>();
            foreach (var item in rg.Rows)
            {
                if (item.IsExpanded)
                {
                    items.Add(((DataRowView)item.DataBoundItem)[0]);
                }
            }
 
            return items;
        }
    }
}

If you have other questions, do not hesitate to contact me again. 

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