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

Promblew with tabbed views

4 Answers 98 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nikita
Top achievements
Rank 1
Nikita asked on 03 Apr 2014, 04:16 AM
Hi everyone!
We are doing project with grid hierarchy and tabbed views.
But there are 2 problems:
1. If there is no records in both fields of DataSource, both tabs are not displayed. If only first field has no records, second tab also is not displayed.
2. PageViewMode change does not affect the type of grouping tabs. 

I made little project to repeat second problem and partly first.
001.public partial class Form1 : Form
002.{
003.    public Form1()
004.    {
005.        InitializeComponent();
006. 
007.        var myPrograms = new List<MyProgram>();
008. 
009.        for (int i = 0; i < 5; i++)
010.        {
011.            var actions = new List<Action>();
012.            for (int j = 0; j < 3; j++)
013.            {
014.                var newServices1 = new List<Service1>();
015.                for (int k = 0; k < 3; k++)
016.                {
017.                    newServices1.Add(new Service1 { Code1 = "code1" + k });
018.                }
019. 
020.                var newServices2 = new List<Service2>();
021.                for (int k = 0; k < 3; k++)
022.                {
023.                    var newService2 = new Service2 { Code2 = "code2" + k };
024.                    newService2.Internals = new List<InternalService>();
025.                    for (int l = 0; l < 3; l++)
026.                    {
027.                        newService2.Internals.Add(new InternalService {Internal = "Internal" + l});
028.                    }
029.                    newServices2.Add(newService2);
030.                }
031. 
032.                actions.Add(new Action
033.                {
034.                    Type = "Type" + j,
035.                 //   Services1 = newServices1,
036.                    Services2 = newServices2
037.                });
038.            }
039. 
040.            var newOne = new MyProgram
041.            {
042.                Name = "Name " + i,
043.                Actions = actions
044.            };
045. 
046.            myPrograms.Add(newOne);
047.        }
048. 
049. 
050. 
051. 
052.        var first = new RadGridViewTemplate
053.        {
054.            Columns = new BindingColumn[] { new BindingColumn { FieldName = "Name" } },
055.            Relation = null,
056.        };
057. 
058.        var second = new RadGridViewTemplate
059.        {
060.            ParentTemplate = first,
061.            Columns = new BindingColumn[] { new BindingColumn { FieldName = "Type" } },
062.            Relation = new RadRelation("ProgramToAction",
063.                null, new[]
064.                {
065.                    "Actions"
066.                })
067.        };
068. 
069.        first.ChildTemplates.Add(second);
070. 
071.        var third1 = new RadGridViewTemplate
072.        {
073.            ParentTemplate = second,
074.            Columns = new BindingColumn[] { new BindingColumn { FieldName = "Code1" } },
075.            Relation = new RadRelation("ActionToOneTimeService", null,
076.                new[]
077.                {
078.                    "Services1"
079.                }),
080.            CaptionText = "Разовые"
081.        };
082.        var third2 = new RadGridViewTemplate
083.        {
084.            ParentTemplate = second,
085.            Columns = new BindingColumn[] { new BindingColumn { FieldName = "Code2" } },
086.            Relation = new RadRelation("ActionToRegularService", null,
087.                new[]
088.                {
089.                    "Services2"
090.                }),
091.            CaptionText = "Регулярные"
092.        };
093. 
094.        second.ChildTemplates.Add(third1);
095.        second.ChildTemplates.Add(third2);
096. 
097.        var fourth = new RadGridViewTemplate
098.        {
099.            ParentTemplate = third2,
100.            Columns = new BindingColumn[] { new BindingColumn { FieldName = "Internal" } },
101.            Relation = new RadRelation("ToRegularSeviceInfo", null,
102.                new[]
103.                {
104.                    "Internals"
105.                })
106.        };
107. 
108.        third2.ChildTemplates.Add(fourth);
109. 
110.        AddBindingColumnRange(rgvMain.MasterTemplate, first.Columns);
111.        AddGridTemplateRecursivly(rgvMain, rgvMain.MasterTemplate, first.ChildTemplates);
112. 
113.        rgvMain.DataSource = myPrograms;
114.        rgvMain.TableElement.PageViewMode = PageViewMode.Stack;
115.    }
116. 
117.    private void AddBindingColumnRange(GridViewTemplate gridViewTemplate, IEnumerable<BindingColumn> list)
118.    {
119.        foreach (BindingColumn column in list)
120.        {
121.            gridViewTemplate.Columns.Add(column.ToGridViewDataColumn());
122.        }
123.    }
124. 
125.    private void AddGridTemplateRecursivly(RadGridView forRelations, GridViewTemplate parentTemplate,
126.        List<RadGridViewTemplate> radGridViewTemplates)
127.    {
128.        if (parentTemplate == null || radGridViewTemplates.Count == 0)
129.        {
130.            return;
131.        }
132. 
133.        foreach (var template in radGridViewTemplates)
134.        {
135.            // add child template
136.            GridViewTemplate childTemplate = template.ToGridViewTemplate();
137.            AddBindingColumnRange(childTemplate, template.Columns);
138. 
139.            parentTemplate.Templates.Add(childTemplate);
140.            //parentTemplate.AllowCellContextMenu = false;
141.            //childTemplate.AllowCellContextMenu = false;
142. 
143.            //add relation
144.            GridViewRelation firstRelation = template.Relation.ToGridViewRelation();
145.            firstRelation.ChildTemplate = childTemplate;
146.            firstRelation.ParentTemplate = parentTemplate;
147.            forRelations.Relations.Add(firstRelation);
148. 
149.            AddGridTemplateRecursivly(forRelations, childTemplate, template.ChildTemplates);
150.        }
151.    }
152.}
153. 
154. 
155.class MyProgram
156.{
157.    public string Name { get; set; }
158. 
159.    public List<Action> Actions { get; set; }
160.}
161. 
162.class Action
163.{
164.    public string Type { get; set; }
165. 
166.    public List<Service1> Services1 { get; set; }
167.    public List<Service2> Services2 { get; set; }
168.}
169. 
170.class Service1
171.{
172.    public string Code1 { get; set; }
173.}
174. 
175.class Service2
176.{
177.    public string Code2 { get; set; }
178. 
179.    public List<InternalService> Internals { get; set; }
180.}
181. 
182.class InternalService
183.{
184.    public string Internal { get; set; }
185.}
186. 
187.public class BindingColumn
188.{
189.    public BindingColumn()
190.    {
191.        IsVisible = true;
192.        IsReadOnly = true;
193.    }
194. 
195.    public string FieldName { get; set; }
196.    public string UniqueName { get; set; }
197.    public string HeaderText { get; set; }
198.    public bool IsVisible { get; set; }
199.    public bool IsReadOnly { get; set; }
200. 
201.    public GridViewDataColumn ToGridViewDataColumn()
202.    {
203.        string uniqueName = string.IsNullOrEmpty(UniqueName) ? FieldName + Guid.NewGuid() : UniqueName;
204. 
205.        GridViewDataColumn column = new GridViewTextBoxColumn(uniqueName, FieldName)
206.        {
207.            IsVisible = IsVisible,
208.            ReadOnly = IsReadOnly
209.        };
210. 
211.        if (!string.IsNullOrEmpty(HeaderText))
212.        {
213.            column.HeaderText = HeaderText;
214.        }
215. 
216.        return column;
217.    }
218.}
219. 
220.public class RadGridViewTemplate
221.{
222.    public IList<BindingColumn> Columns { get; set; }
223.    public RadGridViewTemplate ParentTemplate { get; set; }
224.    public List<RadGridViewTemplate> ChildTemplates { get; private set; }
225.    public RadRelation Relation { get; set; }
226.    public string CaptionText { get; set; }
227. 
228.    public RadGridViewTemplate()
229.    {
230.        ChildTemplates = new List<RadGridViewTemplate>();
231.    }
232. 
233.    public GridViewTemplate ToGridViewTemplate()
234.    {
235.        var result = new GridViewTemplate
236.        {
237.            AllowAddNewRow = false,
238.            AllowColumnChooser = false,
239.            AllowDeleteRow = false,
240.            AutoGenerateColumns = false,
241.            AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill,
242.            Caption = CaptionText
243.        };
244. 
245.        return result;
246.    }
247.}
248. 
249.public class RadRelation
250.{
251.    public RadRelation(string relationName, IList<string> parentColumnNames, IList<string> childColumnNames)
252.    {
253.        RelationName = relationName;
254.        ParentColumnNames = parentColumnNames;
255.        ChildColumnNames = childColumnNames;
256.    }
257. 
258.    public string RelationName { get; set; }
259.    public IList<string> ParentColumnNames { get; set; }
260.    public IList<string> ChildColumnNames { get; set; }
261. 
262.    public GridViewRelation ToGridViewRelation()
263.    {
264.        var r = new GridViewRelation {RelationName = RelationName};
265. 
266.        if (ChildColumnNames != null)
267.        {
268.            foreach (string columnName in ChildColumnNames)
269.            {
270.                r.ChildColumnNames.Add(columnName);
271.            }
272.        }
273. 
274.        if (ParentColumnNames != null)
275.        {
276.            foreach (string columnName in ParentColumnNames)
277.            {
278.                r.ParentColumnNames.Add(columnName);
279.            }
280.        }
281.        return r;
282.    }
283.}



Also the source code of our class, which we use to abstraction GridView.
namespace ProjectControls
{
    public sealed partial class RadCommonDataListControl : UserControl, IGridViewExtended
    {
        private readonly Dictionary<GridViewCommandColumn, Func<object, bool>> _commandColumnDictionary =
            new Dictionary<GridViewCommandColumn, Func<object, bool>>();
 
        private readonly CommandManager _commandManager;
        private readonly RadCommonDataListControlPresenter _presenter;
        private readonly int _rcbHeight;
        private bool _commandInited;
 
        private readonly IList<string> _customSortingFieldNames = new List<string>();
 
        public RadCommonDataListControl()
        {
            InitializeComponent();
            _presenter = new RadCommonDataListControlPresenter();
            _commandManager = CommandManager.Create(components);
            _rcbHeight = rcbMain.Height;
            rgvMain.AllowCellContextMenu = false;
        }
 
        #region ShowButtons
 
        [Browsable(true)]
        public bool ShowAddButton
        {
            [UsedImplicitly] get { return cbbAdd.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbAdd.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiAdd.Visibility = cbbAdd.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowExportButton
        {
            [UsedImplicitly] get { return cbbExport.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbExport.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiExport.Visibility = cbbExport.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowImportButton
        {
            [UsedImplicitly] get { return cbbImport.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbImport.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiImport.Visibility = cbbImport.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowClientCardButton
        {
            [UsedImplicitly]
            get { return cbbShowClientCard.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbShowClientCard.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiShowClientCard.Visibility = cbbShowClientCard.Visibility;
            }
        }
 
         
        [Browsable(true)]
        public bool ShowPrintButton
        {
            [UsedImplicitly] get { return cbbPrint.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbPrint.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiPrint.Visibility = cbbPrint.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowHistoryButton
        {
            [UsedImplicitly] get { return cbbShowHistory.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbShowHistory.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiShowHistory.Visibility = cbbShowHistory.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowAddExtraButton
        {
            [UsedImplicitly] get { return cbbAddExtra.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbAddExtra.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiAddExtra.Visibility = cbbAddExtra.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowArchivedToogleButton
        {
            [UsedImplicitly]
            get { return cbbShowArchived.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbShowArchived.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiShowArchived.Visibility = cbbShowArchived.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowEditButton
        {
            [UsedImplicitly]
            get { return cbbEdit.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbEdit.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiEdit.Visibility = cbbEdit.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowDeleteButton
        {
            [UsedImplicitly]
            get { return cbbDelete.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbDelete.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiDelete.Visibility = cbbDelete.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowAddExtraButtonMulti
        {
            [UsedImplicitly]
            get { return cbbAddExtraMulti.Visibility == ElementVisibility.Visible; }
            set { cbbAddExtraMulti.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed; }
        }
 
        [Browsable(true)]
        public bool ShowRestoreButton
        {
            [UsedImplicitly]
            get { return cbbRestore.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbRestore.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiRestore.Visibility = cbbRestore.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowRefreshButton
        {
            [UsedImplicitly]
            get { return cbbRefresh.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbRefresh.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiRefresh.Visibility = cbbRefresh.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowExpandAllButton
        {
            [UsedImplicitly]
            get { return cbbExpandAll.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbExpandAll.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiExpandAll.Visibility = cbbExpandAll.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowCollapseAllButton
        {
            [UsedImplicitly]
            get { return cbbCollapseAll.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbCollapseAll.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiCollapseAll.Visibility = cbbCollapseAll.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowCheckAllButton
        {
            [UsedImplicitly]
            get { return cbbCheckAll.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbCheckAll.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiCheckAll.Visibility = cbbCheckAll.Visibility;
            }
        }
 
        [Browsable(true)]
        public bool ShowClearSelectionButton
        {
            [UsedImplicitly]
            get { return cbbClearSelection.Visibility == ElementVisibility.Visible; }
            set
            {
                cbbClearSelection.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed;
                rmiClearSelection.Visibility = cbbClearSelection.Visibility;
            }
        }
 
        #endregion
 
        [Browsable(true)]
        public string AddExtraButtonText
        {
            [UsedImplicitly] get { return cbbAddExtra.Text; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbAddExtra.Text = value;
                    rmiAddExtra.Text = value;
                }
            }
        }
 
        [Browsable(true)]
        public string AddButtonText
        {
            [UsedImplicitly] get { return cbbAdd.Text; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbAdd.Text = value;
                    rmiAdd.Text = value;
                }
            }
        }
 
        [Browsable(true)]
        public bool ShowCommandBar
        {
            [UsedImplicitly] get { return rcbMain.Visible; }
            set
            {
                rcbMain.Height = value ? _rcbHeight : 0;
                rcbMain.Visible = value;
                rcbMain.Enabled = value;
            }
        }
 
        public int RowCount
        {
            get { return rgvMain.RowCount; }
        }
 
        [Browsable(true)]
        public string ToolTipAddButton
        {
            [UsedImplicitly] get { return cbbAdd.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbAdd.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipImportButton
        {
            [UsedImplicitly]
            get { return cbbImport.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbImport.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipClientCardButton
        {
            [UsedImplicitly]
            get { return cbbShowClientCard.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbShowClientCard.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipAddExtraButton
        {
            [UsedImplicitly] get { return cbbAddExtra.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbAddExtra.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipEditButton
        {
            [UsedImplicitly] get { return cbbEdit.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbEdit.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipDeleteButton
        {
            [UsedImplicitly] get { return cbbDelete.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbDelete.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipRefreshButton
        {
            [UsedImplicitly] get { return cbbRefresh.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbRefresh.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipArchivedButton
        {
            [UsedImplicitly] get { return cbbShowArchived.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbShowArchived.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipExpandAll
        {
            [UsedImplicitly]
            get { return cbbExpandAll.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbExpandAll.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipCollapseAll
        {
            [UsedImplicitly]
            get { return cbbCollapseAll.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbCollapseAll.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipCheckAllButton
        {
            [UsedImplicitly] get { return cbbCheckAll.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbCheckAll.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipClearSelectionButton
        {
            [UsedImplicitly] get { return cbbClearSelection.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbClearSelection.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipAddExtraMulti
        {
            [UsedImplicitly] get { return cbbAddExtraMulti.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbAddExtraMulti.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipRestoreButton
        {
            [UsedImplicitly] get { return cbbRestore.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbRestore.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipHistoryButton
        {
            [UsedImplicitly] get { return cbbShowHistory.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbShowHistory.ToolTipText = value;
                }
            }
        }
 
        [Browsable(true)]
        public string ToolTipPrintButton
        {
            [UsedImplicitly] get { return cbbPrint.ToolTipText; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    cbbPrint.ToolTipText = value;
                }
            }
        }
 
        public bool IsNeedShowArchived { get; private set; }
 
        public void InitializeCommands(IEnumerable<IGridCommand> commands)
        {
            //TODO: 1. Опять не дизэйблится menuItem в контекстном меню
            //TODO: 2. Не работает редактиравние по дабл клинку.
 
 
            if (_commandInited) return;
            rcbMain.BindingContext = new BindingContext();
 
            foreach (IGridCommand command in commands)
            {
                switch (command.CommandType)
                {
                    case GridCommandType.Add:
                        _commandManager.Bind(command, cbbAdd);
                        _commandManager.Bind(command, rmiAdd);
                        break;
                    case GridCommandType.AddExtra:
                        _commandManager.Bind(command, cbbAddExtra);
                        _commandManager.Bind(command, rmiAddExtra);
                        break;
                    case GridCommandType.AddExtraMulti:
                        var radMenuItem = new RadMenuItem
                        {
                            Text = command.Text
                        };
 
                        cbbAddExtraMulti.Items.Add(radMenuItem);
 
                        _commandManager.Bind(command, radMenuItem);
                        break;
                    case GridCommandType.Edit:
                        _commandManager.Bind(command, cbbEdit);
                        _commandManager.Bind(command, rmiEdit);
                        _commandManager.Bind(command, rgvMain);
                        break;
                    case GridCommandType.Delete:
                        _commandManager.Bind(command, cbbDelete);
                        _commandManager.Bind(command, rmiDelete);
                        break;
                    case GridCommandType.Refresh:
                        _commandManager.Bind(command, cbbRefresh);
                        _commandManager.Bind(command, rmiRefresh);
                        break;
                    case GridCommandType.Restore:
                        _commandManager.Bind(command, cbbRestore);
                        _commandManager.Bind(command, rmiRestore);
                        break;
                    case GridCommandType.CheckAll:
                        _commandManager.Bind(command, cbbCheckAll);
                        _commandManager.Bind(command, rmiCheckAll);
                        break;
                    case GridCommandType.ClearSelection:
                        _commandManager.Bind(command, cbbClearSelection);
                        _commandManager.Bind(command, rmiClearSelection);
                        break;
                    case GridCommandType.ShowHistory:
                        _commandManager.Bind(command, cbbShowHistory);
                        _commandManager.Bind(command, rmiShowHistory);
                        break;
                    case GridCommandType.ShowArchived:
                        _commandManager.Bind(command, cbbShowArchived);
                        _commandManager.Bind(command, rmiShowArchived);
                        break;
                    case GridCommandType.Print:
                        _commandManager.Bind(command, cbbPrint);
                        _commandManager.Bind(command, rmiPrint);
                        break;
                    case GridCommandType.Export:
                        _commandManager.Bind(command, cbbExport);
                        _commandManager.Bind(command, rmiExport);
                        break;
                    case GridCommandType.Import:
                        _commandManager.Bind(command, cbbImport);
                        _commandManager.Bind(command, rmiImport);
                        break;
                    case GridCommandType.ShowClientCard:
                        _commandManager.Bind(command, cbbShowClientCard);
                        _commandManager.Bind(command, rmiShowClientCard);
                        break;
                    case GridCommandType.ExpandAll:
                        _commandManager.Bind(command, cbbExpandAll);
                        _commandManager.Bind(command, rmiExpandAll);
                        break;
                    case GridCommandType.CollapseAll:
                        _commandManager.Bind(command, cbbCollapseAll);
                        _commandManager.Bind(command, rmiCollapseAll);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            _commandInited = true;
        }
 
        public GridCellElement SelectedCell
        {
            get
            {
                return rgvMain.CurrentCell;
            }
        }
 
        public void SetDatasource<T>(BindingList<T> list)
        {
            CheckColumn();
            rgvMain.SmartUpdate(list);
 
            OnSelectionChanged();
        }
 
        object IGridView.SelectedItem
        {
            get { return rgvMain.GetSelectedItem(); }
        }
 
 
 
        public MasterGridViewTemplate MasterTemplate
        {
            get
            {
                return rgvMain.MasterTemplate;
            }
        }
 
        public object DataSource
        {
            get
            {
                return rgvMain.DataSource;
            }
        }
 
 
        private void CheckColumn()
        {
            if (rgvMain.ColumnCount == 0)
            {
                throw new InvalidOperationException("No columns in gridview! Setting canceled!");
            }
        }
 
        private void grdMyGrid_ToolTipTextNeeded(object sender, ToolTipTextNeededEventArgs e)
        {
//            var element = sender as GridHeaderCellElement;
//            if (element != null)
//            {
//                e.ToolTipText = element.Text;
//            }
            GridDataCellElement cell = sender as GridDataCellElement;
 
            if (cell != null && cell.Value != null)
            {
                e.ToolTipText = cell.Value.ToString();
            }
        }
 
        public void DisableContextMemu()
        {
            rcmmMain.SetRadContextMenu(rgvMain, null);
        }
 
        public void SetColumnVisibleStatus(string columnName, bool value)
        {
            if (rgvMain.Columns[columnName] != null)
            {
                rgvMain.Columns[columnName].IsVisible = value;
            }
        }
 
        public int? GetSelectedRowIndex()
        {
            return rgvMain.GetSelectedRowIndex();
        }
 
        public T SelectedItem<T>()
        {
            return rgvMain.GetSelectedItem<T>();
        }
 
        public void AddBindingColumnRange(IEnumerable<BindingColumn> list)
        {
            foreach (BindingColumn column in list)
            {
                AddBindingColumn(column, rgvMain.Columns);
            }
        }
 
        private void RgvMainSelectionChanged(object sender, EventArgs e)
        {
            OnSelectionChanged();
        }
 
        public void SetFilteringStatus(bool status)
        {
            rgvMain.EnableFiltering = status;
            rgvMain.MasterTemplate.EnableFiltering = status;
        }
 
        public void ExecuteAddCommand()
        {
            if (_presenter == null || _presenter.AddCommand == null)
            {
                throw new NullReferenceException("AddCommand");
            }
            _presenter.AddCommand.Execute();
        }
 
        public void ExecuteEditCommand()
        {
            if (_presenter == null || _presenter.EditCommand == null)
            {
                throw new NullReferenceException("EditCommand");
            }
            _presenter.EditCommand.Execute();
        }
 
        public void ExecuteShowHistoryCommand()
        {
            if (_presenter == null || _presenter.ShowHistoryCommand == null)
            {
                throw new NullReferenceException("ShowHistoryCommand");
            }
            _presenter.ShowHistoryCommand.Execute();
        }
 
        public void ExecuteDeleteCommand()
        {
            if (_presenter == null || _presenter.DeleteCommand == null)
            {
                throw new NullReferenceException("DeleteCommand");
            }
            _presenter.DeleteCommand.Execute();
        }
 
 
        public T GetDataInRow<T>(int index)
        {
            return (T) rgvMain.Rows[index].DataBoundItem;
        }
 
        //public void CheckColumn(int rowIndex, int columnIndex)
        //{
        //    rgvMain.Rows[rowIndex].Cells[columnIndex].Value = true;
        //}
 
        public void ExecuteRefreshCommand()
        {
            cbbRefresh.CallDoClick(null);
        }
 
        private void rgvMain_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            if (e.ContextMenuProvider is GridHeaderCellElement)
            {
                e.Cancel = true;
            }
        }
 
        private void rgvMain_ValueChanged(object sender, EventArgs e)
        {
                rgvMain.EndEdit();
        }
 
        public void ExecutePrintCommand()
        {
            if (_presenter != null)
            {
                if (_presenter.PrintCommand != null && _presenter.PrintCommand.Enabled)
                {
                    _presenter.PrintCommand.Execute();
                }
            }
        }
 
 
        private void cbbShowArchived_MouseDown(object sender, MouseEventArgs e)
        {
            IsNeedShowArchived = !IsNeedShowArchived;
        }
 
        private void rmiShowArchived_MouseDown(object sender, MouseEventArgs e)
        {
            IsNeedShowArchived = !IsNeedShowArchived;
        }
 
        private void rmiShowArchived_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            cbbShowArchived.ToggleStateChanged -= cbbShowArchived_ToggleStateChanged;
            cbbShowArchived.InvertToggleState();
            cbbShowArchived.ToggleStateChanged += cbbShowArchived_ToggleStateChanged;
        }
 
        private void cbbShowArchived_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rmiShowArchived.ToggleStateChanged -= rmiShowArchived_ToggleStateChanged;
            rmiShowArchived.InvertToggleState();
            rmiShowArchived.ToggleStateChanged += rmiShowArchived_ToggleStateChanged;
        }
 
 
        private void rgvGroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            if (e.Group.Header == string.Empty)
            {
                e.FormatString = " ";
                e.Group.Expand();
            }
            else
            {
                e.FormatString = e.Group.Header;
            }
        }
 
        public void SetGroupCustomExpanding()
        {
            rgvMain.GroupSummaryEvaluate += rgvGroupSummaryEvaluate;
        }
 
        public void AddGrouping(GridGroupDescriptor groupDescriptor)
        {
            rgvMain.EnableGrouping = true;
            rgvMain.GroupDescriptors.Clear();
 
            var descriptor = new GroupDescriptor();
            GridViewDataColumn gridViewDataColumn =
                rgvMain.MasterTemplate.Columns.GetColumnByFieldName(groupDescriptor.UniqueGroupColumnName)
                    .FirstOrDefault();
            if (gridViewDataColumn != null)
            {
                ListSortDirection listSortDirection = groupDescriptor.SortDirection == GridSortDirection.Ascending
                    ? ListSortDirection.Ascending
                    : ListSortDirection.Descending;
 
                descriptor.GroupNames.Add(gridViewDataColumn.Name, listSortDirection);
                rgvMain.GroupDescriptors.Add(descriptor);
 
                if (groupDescriptor.IsNeedExpand)
                {
                    rgvMain.MasterTemplate.ExpandAllGroups();
                }
            }
        }
 
 
        public void SetHierarchy(string childColumn, string columnFieldName, string columnHeaderText,
            string visibleColumn)
        {
            rgvMain.MasterTemplate.Templates.Clear();
            rgvMain.Templates.Clear();
            rgvMain.Relations.Clear();
            rgvMain.ShowRowHeaderColumn = false;
 
            var template = new GridViewTemplate
            {
                AllowAddNewRow = false,
                ReadOnly = true,
                ShowRowHeaderColumn = false,
                AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill,
                AllowCellContextMenu = false
            };
 
            rgvMain.Templates.Add(template);
 
            template.Columns.Add(new GridViewTextBoxColumn
            {
                FieldName = columnFieldName,
                HeaderText = columnHeaderText
            });
            var relation = new GridViewRelation(rgvMain.MasterTemplate, template);
            relation.ChildColumnNames.Add(childColumn);
 
            rgvMain.Relations.Add(relation);
 
            foreach (GridViewDataColumn column in template.Columns)
            {
                column.IsVisible = column.FieldName == visibleColumn;
            }
        }
 
        private void rgvMain_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
             
            if (e.CellElement.RowIndex <= -1) return;
            var column = e.CellElement.ColumnInfo as GridViewCommandColumn;
            Func<object, bool> func;
            if (column == null || !_commandColumnDictionary.TryGetValue(column, out func)) return;
            var commandCell = (GridCommandCellElement)e.CellElement;
            var buttonElement = (RadButtonElement)commandCell.Children[0];
 
            if (func != null)
            {
                buttonElement.Enabled = func(e.Row.DataBoundItem);
            }
        }
 
        public bool BoldHeaderText { get; set; }
 
        [DebuggerStepThrough]
        private bool IsExpandable(GridViewRowInfo rowInfo)
        {
            return rowInfo.ChildRows != null && rowInfo.ChildRows.Count > 0;
        }
 
        private void rgvMain_ChildViewExpanding(object sender, ChildViewExpandingEventArgs e)
        {
            e.Cancel = !IsExpandable(e.ParentRow);
        }
 
        [Browsable(true)]
        public event CommandCellClickEventHandler CommandCellClick
        {
            add { rgvMain.CommandCellClick += value; }
            remove { rgvMain.CommandCellClick -= value; }
        }
 
        private void rgvMain_ViewCellFormatting_1(object sender, CellFormattingEventArgs e)
        {
            var cell = e.CellElement as GridGroupExpanderCellElement;
            if (cell != null && e.CellElement.RowElement is GridDataRowElement)
            {
                cell.Expander.Visibility = !IsExpandable(cell.RowInfo)
                    ? ElementVisibility.Hidden
                    : ElementVisibility.Visible;
            }
            e.CellElement.DrawFill = false;
            e.CellElement.BorderWidth = 0;
        }
 
        public void ShowCellSelectionBorder()
        {
            rgvMain.ViewCellFormatting += CellSelectionBorder;
        }
 
        private void NaturalSortingByField(object sender, GridViewCustomSortingEventArgs e)
        {
            e.Handled = SortTemplates(rgvMain.MasterTemplate, e);
        }
 
        private bool SortTemplate(GridViewTemplate template, GridViewCustomSortingEventArgs e)
        {
            bool handled = false;
 
            foreach (var name in _customSortingFieldNames)
            {
                int descriptorIndex = -1;
                for (int i = 0; i < template.SortDescriptors.Count; i++)
                {
                    if (template.SortDescriptors[i].PropertyName == name)
                    {
                        descriptorIndex = i;
                    }
                }
 
                if (descriptorIndex != -1)
                {
                    int columnIndex = -1;
                    for (int i = 0; i < template.ColumnCount; i++)
                    {
                        if (template.Columns[i].Name == template.SortDescriptors[descriptorIndex].PropertyName)
                        {
                            columnIndex = i;
                        }
                    }
 
                    var row1Code = (string)e.Row1.Cells[columnIndex].Value;
                    var row2Code = (string)e.Row2.Cells[columnIndex].Value;
 
                    e.SortResult = ServiceFormCodeLogicalComparer.Compare(row1Code, row2Code);
                    if (template.SortDescriptors[descriptorIndex].Direction == ListSortDirection.Descending)
                    {
                        e.SortResult *= -1;
                    }
 
                    handled = true;
                }
            }
 
            return handled;
        }
 
        private bool SortTemplates(GridViewTemplate template, GridViewCustomSortingEventArgs e)
        {
            if (template == null)
            {
                return false;
            }
 
            bool handled = SortTemplate(template, e);
 
            foreach (var child in template.Templates)
            {
                handled |= SortTemplates(child, e);
            }
 
            return handled;
        }
 
        private void CellSelectionBorder(object sender, CellFormattingEventArgs e)
        {
            //TODO: Залепуха для отображения рамки селектора в нижнем уровне журнала ИПР
                if (e.CellElement.IsCurrent && e.CellElement.ViewTemplate.Templates.Count == 0)
                {
                    e.CellElement.BorderWidth = 2;
                    return;
                }
        }
 
 
        public void ExportToExcel(string filepath)
        {
            var excelMl = new ExportToExcelML(rgvMain);
            excelMl.RunExport(filepath);
        }
 
        #region Events
 
        [Browsable(true)]
        public event EventHandler SelectionChanged;
        public event GridViewCellEventHandler CellValueChanged;
 
        private void OnSelectionChanged()
        {
            EventHandler handler = SelectionChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }
 
        private void rgvMain_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            if (CellValueChanged != null)
            {
                CellValueChanged(sender, e);
            }
        }
 
        #endregion
 
        #region Hierarchical grid
 
        private void AddBindingColumnRange(GridViewTemplate gridViewTemplate, IEnumerable<BindingColumn> list)
        {
            foreach (BindingColumn column in list)
            {
                AddBindingColumn(column, gridViewTemplate.Columns);
            }
        }
 
        private void AddBindingColumn(BindingColumn bindingColumn, GridViewColumnCollection columns) //GridViewTemplate gridViewTemplate = null)
        {
            GridViewDataColumn column = bindingColumn.ToGridViewDataColumn();
             
            //TODO: Если содержится колонка с чекбоксом в хедере, то нужно сделать хедер больше по высоте.
            if (bindingColumn.ColumnType == RadGridViewColumnType.CheckBoxColumnCheckableHeader)
            {
                rgvMain.MasterView.TableHeaderRow.MinHeight = 21;
 
            }
            var commandBindingColumn = bindingColumn as CommandBindingColumn;
            var gridViewCommandColumn = column as GridViewCommandColumn;
            if (commandBindingColumn != null && gridViewCommandColumn != null)
            {
                _commandColumnDictionary.Add(gridViewCommandColumn, commandBindingColumn.CanExecute);
            }
 
            if (bindingColumn.IsNaturalSorting)
            {
                if (!rgvMain.EnableCustomSorting)
                {
                    rgvMain.EnableCustomSorting = true;
                    rgvMain.CustomSorting += NaturalSortingByField;
                }
 
                if (!columns.Owner.EnableCustomSorting)
                {
                    columns.Owner.EnableCustomSorting = true;
                }
 
                _customSortingFieldNames.Add(column.Name);
            }
 
            if (bindingColumn is ConditionalEditingBindingColumn)
            {
                rgvMain.CellBeginEdit -= RadGridViewCellBeginEdit; // чтобы обработчик не добавлялся два раза
                rgvMain.CellBeginEdit += RadGridViewCellBeginEdit;
                column.Tag = bindingColumn;
            }
 
            columns.Add(column);
        }
         
        void RadGridViewCellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            var conditionalEditingBindingColumn = e.Column.Tag as ConditionalEditingBindingColumn;
            if (conditionalEditingBindingColumn == null)
            {
                return;
            }
 
            int columnIndex = -1;
            for (int i = 0; i < rgvMain.ColumnCount; i++)
            {
                if (rgvMain.Columns[i].Name.Contains(conditionalEditingBindingColumn.ConditionName))
                {
                    columnIndex = i;
                    break;
                }
            }
 
            if (columnIndex != -1)
            {
                if ((e.Row.Cells[columnIndex].Value as bool?) == true)
                {
                    e.Cancel = true;
                }
            }
        }
 
        public void AddGridTemplate(RadGridViewTemplate radGridViewTemplate)
        {
            rgvMain.EnableGrouping = false;
            rgvMain.GridViewElement.DrawBorder = false;
            rgvMain.GridViewElement.GroupPanelElement.DrawBorder = false;
            rgvMain.MasterTemplate.AllowColumnChooser = false;
            AddBindingColumnRange(rgvMain.MasterTemplate, radGridViewTemplate.Columns);
 
            AddGridTemplateRecursivly(rgvMain, rgvMain.MasterTemplate, radGridViewTemplate.ChildTemplates);
        }
 
        public List<string> DayOfWeeksGroupHeader { get; private set; }
 
        public void AddGroupHeaderIpr(GridViewTemplate childTemplate, List<string> groupHeaderText)
        {
            DayOfWeeksGroupHeader = groupHeaderText;
            rgvMain.ViewCellFormatting += BoldDaysOfWeekHeader;
            var columnGroupsView = new ColumnGroupsViewDefinition();
            int index = 0;
            int i = 0;
            foreach (var header in groupHeaderText)
            {
                columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup(header));
                columnGroupsView.ColumnGroups[index].Rows.Add(new GridViewColumnGroupRow());
                 
                 
                //Три колонки в одном дне недели
                columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(childTemplate.Columns[i++]);
                columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(childTemplate.Columns[i++]);
                columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(childTemplate.Columns[i++]);
                index++;
 
            }
            childTemplate.ViewDefinition = columnGroupsView;
        }
 
        private void BoldDaysOfWeekHeader(object sender, CellFormattingEventArgs e)
        {
            var c = e.CellElement as GridHeaderCellElement;
 
            if (c != null && DayOfWeeksGroupHeader != null)
            {
                if (DayOfWeeksGroupHeader.Contains(e.CellElement.Text))
                {
                    //                    e.CellElement.DrawBorder = true;
                    //                    e.CellElement.DrawFill = true;
                    //                    e.CellElement.BackColor = Color.LightSalmon;
                    c.Font = new Font(Font,FontStyle.Bold);
                }
            }
        }
 
        private void AddGridTemplateRecursivly(RadGridView forRelations, GridViewTemplate parentTemplate,
            List<RadGridViewTemplate> radGridViewTemplates)
        {
            if (parentTemplate == null || radGridViewTemplates.Count == 0)
            {
                return;
            }
 
            foreach (var template in radGridViewTemplates)
            {
                // add child template
                GridViewTemplate childTemplate = template.ToGridViewTemplate();
                AddBindingColumnRange(childTemplate, template.Columns);
                 
                parentTemplate.Templates.Add(childTemplate);
                parentTemplate.AllowCellContextMenu = false;
                childTemplate.AllowCellContextMenu = false;
 
                //add relation
                GridViewRelation firstRelation = template.Relation.ToGridViewRelation();
                firstRelation.ChildTemplate = childTemplate;
                firstRelation.ParentTemplate = parentTemplate;
                forRelations.Relations.Add(firstRelation);
 
                AddGridTemplateRecursivly(forRelations, childTemplate, template.ChildTemplates);
            }
        }
 
        public class RadRelation
        {
            public RadRelation(string relationName, IList<string> parentColumnNames, IList<string> childColumnNames)
            {
                RelationName = relationName;
                ParentColumnNames = parentColumnNames;
                ChildColumnNames = childColumnNames;
            }
 
            public string RelationName { get; set; }
            public IList<string> ParentColumnNames { get; set; }
            public IList<string> ChildColumnNames { get; set; }
 
            public GridViewRelation ToGridViewRelation()
            {
                var r = new GridViewRelation
                {
                        RelationName = RelationName
                };
                 
                if (ChildColumnNames != null)
                {
                    foreach (string columnName in ChildColumnNames)
                    {
                        r.ChildColumnNames.Add(columnName);
                    }
                }
                 
                if (ParentColumnNames != null)
                {
                        foreach (string columnName in ParentColumnNames)
                    {
                        r.ParentColumnNames.Add(columnName);
                    }
                }
                return r;
            }
        }
 
        #endregion
 
        public void CollapseAll()
        {
            rgvMain.MasterTemplate.CollapseAll();
        }
 
        public void ExpandAll()
        {
            rgvMain.MasterTemplate.ExpandAll();
 
        }
    }
     
    public enum GridSortDirection
    {
        Ascending,
        [UsedImplicitly] Descending,
        [UsedImplicitly] None
    }
}



Sorry for my english :)

4 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 07 Apr 2014, 04:37 PM
Hi Nikita,

Thank you for contacting us.

I was not able to reproduce such behavior in a sample application. Both tabs were showing no matter if there were entries or not. You can find my sample application below.

Maybe you are somehow hiding the tabs? Please try to change my sample project and get back to me. Alternatively provide me with another sample project in which the problem is reproducible.

Looking forward to your report.

Regards,
George
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Nikita
Top achievements
Rank 1
answered on 21 May 2014, 06:44 AM
George, hi! Thanks for response! Sorry for the long silence. 
Yes, in your sample application everything is working, but in my project is not all so good. 
We use abstraction in our project, so I removed the superfluous in my test project. Please look again, and note the lines: 
rgvMain.TableElement.PageViewMode = PageViewMode.Stack;
rgvMain.MasterTemplate.Templates [0]. ExpandAll ();

They also do not work as they should. I think the problem in a hierarchy.

http://www34.zippyshare.com/v/87143361/file.html - link to project. If you have trouble downloading, I'll mirror for download
0
Nikita
Top achievements
Rank 1
answered on 22 May 2014, 03:52 AM
Hooray, I found a solution. In the GridView we had a code: 

private bool IsExpandable(GridViewRowInfo rowInfo)
{
    return rowInfo.ChildRows != null && rowInfo.ChildRows.Count > 0;
}
 
private void rgvMain_ChildViewExpanding(object sender, ChildViewExpandingEventArgs e)
{
    e.Cancel = !IsExpandable(e.ParentRow);
}
 
private void rgvMain_ViewCellFormatting_1(object sender, CellFormattingEventArgs e)
{
    var cell = e.CellElement as GridGroupExpanderCellElement;
    if (cell != null && e.CellElement.RowElement is GridDataRowElement)
    {
        cell.Expander.Visibility = !IsExpandable(cell.RowInfo)
            ? ElementVisibility.Hidden
            : ElementVisibility.Visible;
    }
}


The problem is that if the group 2 template, and the first blank and the other not, rowInfo.ChildRows.Count was zero.

But problem with 
rgvMain.TableElement.PageViewMode = PageViewMode.Stack;
rgvMain.MasterTemplate.Templates [0]. ExpandAll ();
is actual.
0
George
Telerik team
answered on 23 May 2014, 02:16 PM
Hello Nikita,

Thank you for your reply.

I am glad that you resolved one of your issues.

As to the matter with expanding the rows, you need to manually expand each row since in this hierarchy they are not being kept in the templates. This means that every template different from the master has no rows and calling expand will not have effect. Instead, here is the approach you can use:
void Form1_Click(object sender, EventArgs e)
{
    foreach (var row in this.grid.MasterTemplate.ChildRows)
    {
        this.ExpandOrCollapseRecursively(row.ChildRows, true);
    }
}
 
private void ExpandOrCollapseRecursively(GridViewChildRowCollection childRows, bool expand)
{
    foreach (GridViewRowInfo row in childRows)
    {
        GridViewHierarchyRowInfo hierarchyRow = row as GridViewHierarchyRowInfo;
 
        if ((row is GridViewGroupRowInfo || (hierarchyRow != null && hierarchyRow.HasChildRows())) && row.IsVisible)
        {
            row.IsExpanded = expand;
            this.ExpandOrCollapseRecursively(row.ChildRows, expand);
        }
    }
}

This should result in the desired behavior.

Let me know if you have further questions.

Regards,
George
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Nikita
Top achievements
Rank 1
Answers by
George
Telerik team
Nikita
Top achievements
Rank 1
Share this question
or