I have a radgridview setup wth a hierarchy. I would like this grid to refresh every ten seconds as new information could be added to the database from another source, so it needs to be kept up-to-date. I did have a timer which would fire every ten seconds which reloaded the data.
The only problem is, if I have a row expanded or select a row and the grid reloads the selected row is lost and the sub grids close. Is there any way I can keep this open while the data refreshes? ie the new row would be added at the bottom?
I have followed the http://www.telerik.com/community/forums/winforms/gridview/radgridview-hierarchy-refreshing-hierarchy-grid-closes.aspx example given when i debug the code i can see it is storing the expanded rows but while Restoring
if
(expanded.Contains(item.DataBoundItem))
{
item.IsExpanded =
true;
}
the condition is not satisfyting. Can U suggest Me How to do the above Task
Thanks
5 Answers, 1 is accepted
In this scenario you should use the key column of you parent table. When saving/restoring data you can use the 'ID' column. Consider the following 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; } }}I hope this helps.
Greetings,
Julian Benkov
the Telerik team
SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).
Thank You so much it is working fine now i am able to restore the expanded ones.
Regards,
Nagaraju.
I am glad to hear that my answer was helpful. Do not hesitate to contact us if you have other questions.
Kind regards,Julian Benkov
the Telerik team
SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).
I need to restore the selected row also so i have wriiten the same thing
public HashSet<object> SaveSelectedRow(RadGridView rg, string primaryKey)
{
HashSet<object> items = new HashSet<object>();
foreach (var item in rg.Rows)
{
if (item.IsSelected)
{
items.Add(((
DataRowView)item.DataBoundItem)[primaryKey]);
}
}
return items;
}
public void RestoreSelectedRow(HashSet<object> expanded, RadGridView rg, string primaryKey)
{
foreach (var item in rg.Rows)
{
if (expanded.Contains(((DataRowView)item.DataBoundItem)[primaryKey]))
{
item.IsSelected =
true;
}
}
}
I am able to get the slected row state and making it as selected but the problem i am facing is getting error message "X" column not found some times. If i have 3 columns the error is displaying 3times.
I have called the code as below
slectGrid.ClearSelection();
slectGrid.RestoreSelectedRow(
this._gridSelectedRow, this.slectGrid, "primaryKey");
Could u help me in this.
Please check the colums schema of your bound DataTable object. In order to have a property working implementation, the used primaryKey string must exist for the previous and current DataTable Columns Name property after the rebinding operation.
If you continue to experience the issue, please send me a simple project to investigate it locally and find the best solution for the case.
Please note that you have to open a new support ticket in order to be able to attach your project.
Julian Benkov
the Telerik team