Telerik Forums
UI for WinForms Forum
4 answers
526 views
I am trying to create a hierarchy grid where the child template columns line up with the parent template columns.

To achieve this I am setting the width of the first column based on the hierarchy level.  I've found that a drop in 19 pixels makes up for the expander on the left of the row.  The other columns I have set dynamically based upon their parent column widths so that users can resize the grid and the columns will still fill the space.

The issue I am having is on initial load of the grid the columns get jumbled up.  However, after a user expands or collapses one of the columns everything lines up and works.  (See attached image).

Is there another way to achieve this that ensures that the child column are always lined up with the parent column widths?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace HierarchyGridExample
{
    public partial class Form1 : Form
    {
        List<MyObject> GridData = new List<MyObject>();
        int count = 1;
 
        public Form1()
        {
            InitializeComponent();
 
            grid.CellValueChanged += grid_CellValueChanged;
            grid.ColumnWidthChanged += grid_ColumnWidthChanged;
            grid.ChildViewExpanding += grid_ChildViewExpanding;
            grid.MultiSelect = true;
            grid.SelectionMode = GridViewSelectionMode.FullRowSelect;
 
            GridViewTemplate level2 = new GridViewTemplate(), level3 = new GridViewTemplate();
            CreateTemplate(grid.MasterTemplate);
            CreateTemplate(level2);
            CreateTemplate(level3);
 
            level2.ShowColumnHeaders = false;
            level3.ShowColumnHeaders = false;
 
            grid.MasterTemplate.Templates.Add(level2);
            level2.Templates.Add(level3);
             
            GridViewRelation rel1 = new GridViewRelation(grid.MasterTemplate, level2);
            rel1.ChildColumnNames.Add("Children");
            grid.Relations.Add(rel1);
 
            GridViewRelation rel2 = new GridViewRelation(level2, level3);
            rel2.ChildColumnNames.Add("Children");
            grid.Relations.Add(rel2);
 
            /*CreateTemplate(grid.MasterTemplate);
 
            GridViewTemplate template = new GridViewTemplate();
            CreateTemplate(template);
            grid.MasterTemplate.Templates.Add(template);
            template.ShowFilteringRow = false;
 
            // Setup child relation
            GridViewRelation relation = new GridViewRelation(grid.MasterTemplate, template);
            relation.RelationName = "Children";
            relation.ChildColumnNames.Add("TopChildren");
            grid.Relations.Add(relation);
 
            CreateChildTemplate(template, 0);*/
 
            LoadData(true);
            grid.MasterTemplate.ExpandAll();
        }
 
        #region Editing
 
        void grid_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            if (e.Column.Name == "Enabled")
            {
                (e.Row.DataBoundItem as MyObject).Enabled = (bool)e.Value;
            }
        }
 
        #endregion Editing
 
        #region Formatting
 
        private void grid_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.Name == "HLevel")
            {
                e.CellElement.Text = e.Row.HierarchyLevel.ToString();
            }
            if (e.Column.Name == "Name")
            {
                /*e.CellElement.TextImageRelation = TextImageRelation.ImageBeforeText;
                e.CellElement.TextAlignment = ContentAlignment.MiddleLeft;
                e.CellElement.ImageAlignment = ContentAlignment.MiddleLeft;
                e.CellElement.Image = Properties.Resources.Radio;*/
                if (e.Row.HierarchyLevel == 0 && e.Column.Width != 150)
                {
                    e.Column.Width = 150;
                    e.Column.MaxWidth = 150;
                    e.Column.MinWidth = 150;
                }
                else if (e.Column.Width != 131 && e.Row.HierarchyLevel > 0)
                {
                    e.Column.Width = 131;
                    e.Column.MaxWidth = 131;
                    e.Column.MinWidth = 131;
                }
            }
            else
            {
                e.CellElement.Image = null;
            }
        }
 
        private void grid_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            if (e.ColumnIndex > 0)
            {
                int masterWidth = grid.MasterTemplate.Columns[e.ColumnIndex].Width;
                int child1Width = grid.MasterTemplate.Templates[0].Columns[e.ColumnIndex].Width;
                int child2Width = grid.MasterTemplate.Templates[0].Templates[0].Columns[e.ColumnIndex].Width;
                if (masterWidth != child1Width)
                {
                    grid.MasterTemplate.Templates[0].Columns[e.ColumnIndex].Width = masterWidth;
                    //grid.MasterTemplate.Templates[0].Refresh();
                }
                if (masterWidth != child2Width)
                {
                    grid.MasterTemplate.Templates[0].Templates[0].Columns[e.ColumnIndex].Width = masterWidth;
                    //grid.MasterTemplate.Templates[0].Templates[0].Refresh();
                }
            }
        }
 
        #endregion Formatting
 
        #region Expand
 
        private void grid_ChildViewExpanding(object sender, ChildViewExpandingEventArgs e)
        {
            if (!e.IsExpanded && e.ParentRow.ChildRows.Any())
            {
                List<GridViewTemplate> temps = new List<GridViewTemplate>();
                if (grid.MasterTemplate.Templates.Any())
                {
                    temps.Add(grid.MasterTemplate.Templates[0]);
                    if (grid.MasterTemplate.Templates[0].Templates.Any())
                    {
                        temps.Add(grid.MasterTemplate.Templates[0].Templates[0]);
                    }
                }
                for (int x = 1; x < grid.MasterTemplate.Columns.Count; x++)
                {
                    temps.ForEach(c => c.Columns[x].Width = grid.MasterTemplate.Columns[x].Width);
                }
            }
        }
 
        private void RestoreExpanded(GridViewRowInfo g, HashSet<object> expanded)
        {
            using (grid.DeferRefresh())
            {
                if (g == null)
                {
                    foreach (var item in this.grid.Rows)
                    {
                        if (expanded.Contains(item.DataBoundItem))
                        {
                            item.IsExpanded = true;
                        }
                        foreach (var child in item.ChildRows)
                        {
                            RestoreExpanded(child, expanded);
                        }
                    }
                }
                else
                {
                    if (expanded.Contains(g.DataBoundItem))
                    {
                        g.IsExpanded = true;
                    }
                    foreach (var child in g.ChildRows)
                    {
                        RestoreExpanded(child, expanded);
                    }
                }
            }
        }
 
        private void SaveExpanded(GridViewRowInfo g, HashSet<object> items)
        {
            if (g == null)
            {
                foreach (var item in this.grid.Rows)
                {
                    if (item.IsExpanded)
                    {
                        items.Add(item.DataBoundItem);
                    }
                    foreach (var child in item.ChildRows)
                    {
                        SaveExpanded(child, items);
                    }
                }
            }
            else
            {
                if (g.IsExpanded)
                {
                    items.Add(g.DataBoundItem);
                }
                foreach (var child in g.ChildRows)
                {
                    SaveExpanded(child, items);
                }
            }
        }
 
        #endregion Expand
 
        #region Data
 
        private void LoadData(bool createTestData = false)
        {
            using (grid.DeferRefresh())
            {
                HashSet<object> expanded = new HashSet<object>();
                SaveExpanded(null, expanded);
 
                grid.DataSource = null;
                if (createTestData)
                {
                    GridData = new List<MyObject>();
                    MyObject m1 = new MyObject("Test " + count);
                    count++;
                    LoadChild(m1, 3);
                    GridData.Add(m1);
                    MyObject m2 = new MyObject("Test " + count);
                    count++;
                    LoadChild(m2, 3);
                    GridData.Add(m2);
                    MyObject m3 = new MyObject("Test " + count);
                    count++;
                    LoadChild(m3, 3);
                    GridData.Add(m3);
                }
                grid.DataSource = GridData;
                 
                RestoreExpanded(null, expanded);
            }
        }
 
        private void LoadChild(MyObject parent, int number, int level = 0)
        {
            level++;
            for (int i = 0; i < number; i++)
            {
                MyObject m2 = new MyObject("Test " + count, count % 2 == 0);
                count++;
                parent.Children.Add(m2);
                m2.Parent = parent;
                if (level < 2)
                {
                    LoadChild(m2, number, level);
                }
            }
        }
 
        private void CreateChild(MyObject parent = null)
        {
            if (parent != null)
            {
                MyObject m2 = new MyObject("Test " + count, count % 2 == 0);
                count++;
                parent.Children.Add(m2);
                m2.Parent = parent;
                //parent.Children.ResetBindings();
            }
            else
            {
                MyObject m2 = new MyObject("Test " + count, count % 2 == 0);
                count++;
                GridData.Add(m2);
            }
            LoadData();
        }
 
        #endregion Data
 
        #region Template
 
        private void CreateTemplate(GridViewTemplate t, bool isMaster = false)
        {
            t.Columns.Clear();
            t.AutoGenerateColumns = false;
            t.ShowFilteringRow = false;
            t.EnableGrouping = false;
            t.AllowAddNewRow = false;
            t.EnableFiltering = true;
            t.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            GridViewTextBoxColumn colName = new GridViewTextBoxColumn("Name");
            colName.HeaderText = "Name";
            colName.Name = "Name";
            t.Columns.Add(colName);
 
            GridViewTextBoxColumn colH = new GridViewTextBoxColumn();
            colH.HeaderText = "H Level";
            colH.Name = "HLevel";
            colH.ReadOnly = true;
            t.Columns.Add(colH);
 
            GridViewCheckBoxColumn colEnabled = new GridViewCheckBoxColumn("Enabled");
            colEnabled.HeaderText = "Enabled";
            colEnabled.Name = "Enabled";
            t.Columns.Add(colEnabled);
 
            GridViewTextBoxColumn col4 = new GridViewTextBoxColumn();
            col4.HeaderText = "Col 4";
            col4.Name = "Col4";
            col4.ReadOnly = true;
            t.Columns.Add(col4);
        }
 
        private void CreateChildTemplate(GridViewTemplate parent, int x)
        {
            GridViewTemplate template = new GridViewTemplate();
            CreateTemplate(template);
            template.ShowChildViewCaptions = false;
            template.ShowColumnHeaders = false;
            template.ShowFilteringRow = false;
 
            parent.Templates.Add(template);
            GridViewRelation r = new GridViewRelation(parent, template);
            r.ChildColumnNames.Add("Children");
            grid.Relations.Add(r);
 
            if (x < 10)
            {
                x++;
                CreateChildTemplate(template, x);
            }
        }
 
        private void UpdateTemplates(GridViewTemplate t)
        {
            t.Refresh();
            foreach (GridViewTemplate g in t.Templates)
            {
                UpdateTemplates(g);
            }
        }
 
        #endregion Template
 
        #region Buttons
 
        private void buttonReload_Click(object sender, EventArgs e)
        {
            LoadData();
        }
 
        private void buttonAddPeer_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRows.Count == 1 && grid.SelectedRows[0].DataBoundItem != null && grid.SelectedRows[0].DataBoundItem is MyObject)
            {
                CreateChild((grid.SelectedRows[0].DataBoundItem as MyObject).Parent);
            }
            else
            {
                CreateChild();
            }
        }
 
        private void buttonAddChild_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRows.Count == 1 && grid.SelectedRows[0].DataBoundItem != null)
            {
                CreateChild(grid.SelectedRows[0].DataBoundItem as MyObject);
            }
            else
            {
                CreateChild();
            }
        }
 
        private void buttonCollapse_Click(object sender, EventArgs e)
        {
            grid.MasterTemplate.CollapseAll();
        }
 
        private void buttonExpand_Click(object sender, EventArgs e)
        {
            grid.MasterTemplate.ExpandAll();
        }
 
        #endregion Buttons
    }
 
    public class MyObject
    {
        public MyObject Parent { get; set; }
 
        public string Name { get; set; }
 
        public bool Enabled { get; set; }
 
        public BindingList<MyObject> Children { get; set; }
 
        public MyObject(string name)
        {
            this.Enabled = true;
            this.Name = name;
            Children = new BindingList<MyObject>();
        }
        public MyObject(string name, bool enabled)
        {
            this.Enabled = enabled;
            this.Name = name;
            Children = new BindingList<MyObject>();
        }
    }
}
Dess | Tech Support Engineer, Principal
Telerik team
 answered on 24 Oct 2013
1 answer
130 views
Hello,

is there a opportunity to exclude the Suggests from the SelectedIndexChange?

I want to create a function, which reacts only, when the user really chooses an item by clicking or pressing the enter key. In the moment my function fires every time, when the user goes with the keyboard arrows through the suggested items.

Maybe theres another way then to react to the SelectedIndexChange?

Thanks for advices!

Greets
Dimitar
Telerik team
 answered on 24 Oct 2013
1 answer
199 views
I'm sure I might be going about this the wrong way, but here is what I'm trying to accomplish.

My UI has two sides, on the left a user entry form, and on the right a RTB that is formatted to look like a report might.  On load, I define bookmarks to note the start and end of areas.  These start and end bookmarks are to help me identify where to place any updates I'll need to do through code.

When the user types in a value or performs an action on the user entry form, some corresponding changes will need to happen within the RTB document.  I'll need to find the location within the document and perform the update.

Ideally, I can simply identify where I need to select text within the document, delete it, and replace it with whatever.  Also worth noting is that most updates will occur within a Textbox TextChanged event, so speed is a factor as well.

Am I approaching this the right way?  This is my first go with the RTB, and I want to make sure I'm doing things somewhat correct.  lol

Sample range identification:

this.txtCareplanPreview.InsertLineBreak();
this.txtCareplanPreview.InsertBookmark("InterventionNotes" + intItem.CCGInterventionId.ToString());
this.txtCareplanPreview.Insert("Notes: " + intItem.Description);
this.txtCareplanPreview.InsertBookmark("InterventionNotesEnd" + intItem.CCGInterventionId.ToString());
this.txtCareplanPreview.InsertLineBreak();

Update 10/21/2013: I'm attempting to dig into annotations now, but I'm unable to find any good information regarding their usage, either in the documentation or here in the forum.  I believe as this point, annotations are what I'm looking for.  I'd still like some input on how I can best accomplish my goals.
Thanks!

Wayne
Ivan Petrov
Telerik team
 answered on 23 Oct 2013
1 answer
155 views
Is there a way to get the ListDataItem object that the mouse is hovering over in a RadListControl?
I've been trying several approach's but been drawing a blank.  I'm thinking the item should already be available somehow since its being highlighted/formatted by the ListControl already...
Thx
Stefan
Telerik team
 answered on 23 Oct 2013
1 answer
381 views
I have some problems with copying required columns from one gridview to another gridview!
Firstly, I download table from Excel, then I give my own HeaderText to some columns using ContextMenu.
And I need to copy only these columns with my HeaderText to another gridview.
I tried some variants but it didn't work!
How can I do that?

Thanks in advance.
Dimitar
Telerik team
 answered on 23 Oct 2013
1 answer
283 views
I'm adding some text programmatically and then allowing the user to format it for their needs and saved it as a Word document.  In the text that I am programmatically adding I am adding some numbers as a superscript. Currently I turn those blue, then make them smaller to offset them from the main text.  But I really want them to be forced to the top of the line, not the bottom.  In Word I would just set them as superscript.

I have styles for these that is separate from the main text.  I was hoping I can just format their style in a way to either make them an actual superscript, or at least align them to the top to make them look like it.  I would prefer to make the superscript, as this will be exported as a Word document.

Do you have any documentation about how to control the styles for things like this, maybe how to add footnotes and other things as well?  The online documentation only shows various font formatting.  Does the RadRichTextBox allow any more formatting than what is detailed in the documentation?
George
Telerik team
 answered on 23 Oct 2013
5 answers
161 views
I am using the hierarchy grid to display almost all of the data for my program and the inability for the user to be able to print the grid with it displaying the grid like it shows is a huge problem. Any who reads please vote for this to get added soon  here: http://www.telerik.com/support/pits.aspx#/details/Issue=11527 . Also if anyone knows any way to work around this that would be great!
George
Telerik team
 answered on 23 Oct 2013
2 answers
257 views
Hello, i have a tree data structure like the following:

public class MyTreeNode
{
    public string Display{get;set;}
    public List<MyTreeNode> childs {get;set;}
}

how can I bind to this datastructure?

Using your WPF Treeview I was able to do using the following code (data context is a root node that is not displayed)

<TreeView  Name="treeView1" ItemsSource="{Binding Childs}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Childs}">
            <StackPanel Orientation="Horizontal" Height="18">
                <TextBlock Text="{Binding Display}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

I do not understand how to do with winforms version of treeview control.

Can anybody please help?
Andrea
Top achievements
Rank 1
 answered on 23 Oct 2013
3 answers
237 views
So i have a grid with multiple columns and one is a ComboBox. The problem I encountered is that each row has to have diferent items in that ComboBox. I tried setting the DataSource on the Column, bit it just messes up every row. Also tried to find the ComboBox from the Rows property with no success.
I was also thinking of the performance impact this would have? If i have 1000 rows with 1000 ComboBoxes an 1000 DataSources? Probably not good :/
Would really appreciate any help or tips anyone can give me.
George
Telerik team
 answered on 23 Oct 2013
7 answers
164 views
I want to place a row of buttons underneath a grid View such that they always lie under the last row, with no white space (except margin).
When the grid expands to the bottom of the form, the buttons are still visible (at the bottom), and the grid displays vertical scroll bars.

There must be some combination of gridViewElements, panels, docking, etc. that does this? 
Dess | Tech Support Engineer, Principal
Telerik team
 answered on 23 Oct 2013
Narrow your results
Selected tags
Tags
GridView
General Discussions
Scheduler and Reminder
Treeview
Dock
RibbonBar
Themes and Visual Style Builder
ChartView
Calendar, DateTimePicker, TimePicker and Clock
DropDownList
Buttons, RadioButton, CheckBox, etc
ListView
ComboBox and ListBox (obsolete as of Q2 2010)
Form
Chart (obsolete as of Q1 2013)
PageView
MultiColumn ComboBox
TextBox
RichTextEditor
PropertyGrid
Menu
RichTextBox (obsolete as of Q3 2014 SP1)
Panelbar (obsolete as of Q2 2010)
PivotGrid and PivotFieldList
Tabstrip (obsolete as of Q2 2010)
MaskedEditBox
PdfViewer and PdfViewerNavigator
CommandBar
ListControl
Carousel
GanttView
Diagram, DiagramRibbonBar, DiagramToolBox
Panorama
New Product Suggestions
VirtualGrid
Toolstrip (obsolete as of Q3 2010)
Label
AutoCompleteBox
Spreadsheet
ContextMenu
Panel
Visual Studio Extensions
TitleBar
SplitContainer
Documentation
Map
DesktopAlert
CheckedDropDownList
ProgressBar
MessageBox
TrackBar
Rotator
SpinEditor
CheckedListBox
StatusStrip
Wizard
CollapsiblePanel
LayoutControl
ShapedForm
SyntaxEditor
TextBoxControl
Conversational UI, Chat
DateTimePicker
TabbedForm
CAB Enabling Kit
WaitingBar
GroupBox
DataEntry
ScrollablePanel
ScrollBar
ImageEditor
Tools - VSB, Control Spy, Shape Editor
BrowseEditor
DataFilter
FileDialogs
ColorDialog
Gauges (RadialGauge, LinearGauge, BulletGraph)
ApplicationMenu
RangeSelector
CardView
WebCam
NavigationView
BindingNavigator
RibbonForm
Styling
Barcode
PopupEditor
TaskBoard
Callout
ColorBox
PictureBox
FilterView
Accessibility
VirtualKeyboard
DataLayout
Licensing
ToastNotificationManager
ValidationProvider
CalculatorDropDown
Localization
TimePicker
BreadCrumb
ButtonTextBox
FontDropDownList
BarcodeView
Overlay
Security
LocalizationProvider
Dictionary
TreeMap
StepProgressBar
SplashScreen
Flyout
Separator
SparkLine
ToolbarForm
NotifyIcon
DateOnlyPicker
AI Coding Assistant
Rating
TimeSpanPicker
Calculator
OfficeNavigationBar
TaskbarButton
HeatMap
SlideView
PipsPager
AIPrompt
TaskDialog
TimeOnlyPicker
SpeechToTextButton
SmartPasteButton
+? more
Top users last month
Jake
Top achievements
Rank 1
RBarnes
Top achievements
Rank 1
Iron
Iron
Rami
Top achievements
Rank 1
Iron
Veteran
Nathan
Top achievements
Rank 1
Kumeri
Top achievements
Rank 1
Iron
Veteran
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Jake
Top achievements
Rank 1
RBarnes
Top achievements
Rank 1
Iron
Iron
Rami
Top achievements
Rank 1
Iron
Veteran
Nathan
Top achievements
Rank 1
Kumeri
Top achievements
Rank 1
Iron
Veteran
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?