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

Making a programmatically hierarchy with GridView

3 Answers 196 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 17 Jul 2009, 10:32 AM

Hi,

I am trying out the GridView in a programmatically manner, to build a hierarchy of an exception tree.  I don’t know if my approach is the best way, please tell if otherwise. The hierarchy is shown, but there are two issues I can’t figure out.

First, Due to the relationship all rows are shown as expandable! Only the root exception row and the InnerException row should be expandable. I have tried setting the ParentID to zero for those rows that are not expandable, but then they are not shown at all?

Second, In the expanded (nested) grid two tabs appear (named; table1 and table2), how do I remove/hide those?

Cheers, Frank

---------------------------- Sample Code -------------------------

  public class ExceptionExplorer
  {
    private static readonly string FIELD_EXCEPTIONID = "ExceptionID";
    private static readonly string FIELD_PARENTID = "ParentID";
    private static readonly string FIELD_NAME = "Name";
    private static readonly string FIELD_VALUE = "Value";

    public void ShowData(RadGridView gridView)
    {
      gridView.AutoGenerateHierarchy = false;
      gridView.AllowDrop = false;
      gridView.ShowGroupPanel = false;
      gridView.ShowNoDataText = false;
      
      try
      {
        gridView.Enabled = false;
        gridView.GridElement.BeginUpdate();
        gridView.Rows.Clear();

        List<Exception> exceptions = new List<Exception>();
        exceptions.Add(new ArithmeticException("ArithmeticException", new DataMisalignedException("DataMisalignedException", new global::System.NotSupportedException("NotSupportedException", new NotImplementedException("NotImplementedException")))));
        exceptions.Add(new ArgumentOutOfRangeException("ArgumentOutOfRangeException", new NullReferenceException("NullReferenceException", new global::System.NotSupportedException("NotSupportedException", new NotImplementedException("NotImplementedException")))));

        PrepareGridViewTemplate(gridView.MasterGridViewTemplate);
        int exceptionID = 0;
        foreach (Exception exception in exceptions)
        {
          ShowException(gridView.MasterGridViewTemplate, null, ref exceptionID, 0, exception.GetType().Name, exception);
        }
      }
      finally
      {
        gridView.GridElement.EndUpdate();
        gridView.Enabled = true;
      }
    }

    private void ShowException(GridViewTemplate gridviewTemplate, GridViewDataRowInfo parentRow, ref int exceptionID, int parentID, string exceptionTitle, Exception exception)
    {
      GridViewDataRowInfo exceptionRow = CreateGridRow(gridviewTemplate, parentRow, ++exceptionID, parentID, exceptionTitle, exception.GetType().FullName);

      parentID = exceptionID;
      GridViewTemplate childGridViewTemplate = CreateChildGridViewTemplate(gridviewTemplate);

      CreateGridRow(childGridViewTemplate, exceptionRow, ++exceptionID, parentID, "Message", exception.Message);
      CreateGridRow(childGridViewTemplate, exceptionRow, ++exceptionID, parentID, "Source", exception.Source);
      CreateGridRow(childGridViewTemplate, exceptionRow, ++exceptionID, parentID, "HelpLink", exception.HelpLink ?? string.Empty);
      CreateGridRow(childGridViewTemplate, exceptionRow, ++exceptionID, parentID, "TargetSite", exception.TargetSite == null ? string.Empty : exception.TargetSite.ToString());
      CreateGridRow(childGridViewTemplate, exceptionRow, ++exceptionID, parentID, "Stacktrace", exception.StackTrace);

      if (exception.InnerException != null)
      {
        ShowException(childGridViewTemplate, exceptionRow, ref exceptionID, parentID, "InnerException", exception.InnerException);
      }
    }

    private GridViewDataRowInfo CreateGridRow(GridViewTemplate gridviewTemplate, GridViewDataRowInfo parentRow, int exceptionID, int parentID, string nameColumn, string valueColumn)
    {
      //      GridViewDataRowInfo dataRow = gridviewTemplate.Rows.AddNew(/*parentRow*/); // Gives NullReferenceException...
      //      GridViewDataRowInfo dataRow = gridviewTemplate.Rows.AddNew();
      //      dataRow.Cells[FIELD_EXCEPTIONID].Value = exceptionID.ToString();
      //      dataRow.Cells[FIELD_PARENTID].Value = parentID.ToString();
      //      dataRow.Cells[FIELD_NAME].Value = nameColumn;
      //      dataRow.Cells[FIELD_VALUE].Value = valueColumn;
      // Gives NullReferenceException if cells are addressed through index/name ???

      GridViewDataRowInfo dataRow = gridviewTemplate.Rows[gridviewTemplate.Rows.Add(exceptionID, parentID, nameColumn, valueColumn)];
      dataRow.EnsureVisible();

      return dataRow;
    }

    private void PrepareGridViewTemplate(GridViewTemplate gridviewTemplate)
    {
      gridviewTemplate.AutoGenerateColumns = false;

      GridViewDataColumn gridColumn;
      gridviewTemplate.Columns.Add(gridColumn = new GridViewDataColumn(FIELD_EXCEPTIONID));
      gridColumn.IsVisible = false;
      SetColumnDefaults(gridColumn);

      gridviewTemplate.Columns.Add(gridColumn = new GridViewDataColumn(FIELD_PARENTID));
      gridColumn.IsVisible = false;
      SetColumnDefaults(gridColumn);

      gridviewTemplate.Columns.Add(gridColumn = new GridViewDataColumn(FIELD_NAME));
      gridColumn.Width = 200;
      SetColumnDefaults(gridColumn);

      gridviewTemplate.Columns.Add(gridColumn = new GridViewDataColumn(FIELD_VALUE));
      gridColumn.MinWidth = 200;
      SetColumnDefaults(gridColumn);

      gridviewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

      gridviewTemplate.AllowAddNewRow = false;
      gridviewTemplate.AllowDeleteRow = false;
      gridviewTemplate.AllowEditRow = false;
      gridviewTemplate.AllowColumnChooser = false;
      gridviewTemplate.AllowColumnReorder = false;
      gridviewTemplate.AllowDragToGroup = false;

      gridviewTemplate.ShowColumnHeaders = gridviewTemplate.Owner.MasterGridViewTemplate == gridviewTemplate;
      gridviewTemplate.ShowFilteringRow = false;
      gridviewTemplate.ShowGroupedColumns = false;
    }

    private GridViewTemplate CreateChildGridViewTemplate(GridViewTemplate parentTemplate)
    {
      GridViewTemplate childTemplate = new GridViewTemplate(parentTemplate.Owner);
      PrepareGridViewTemplate(childTemplate);

      GridViewRelation relation = new GridViewRelation(parentTemplate);
      relation.ChildTemplate = childTemplate;
      relation.RelationName = "ExceptionRelation";
      relation.ParentColumnNames.Add(FIELD_EXCEPTIONID);
      relation.ChildColumnNames.Add(FIELD_PARENTID);
      parentTemplate.Owner.Relations.Add(relation);

      parentTemplate.ChildGridViewTemplates.Add(childTemplate);

      return childTemplate;
    }

    private void SetColumnDefaults(GridViewDataColumn gridColumn)
    {
      gridColumn.HeaderText = gridColumn.FieldName;
      gridColumn.AllowFiltering = false;
      gridColumn.AllowGroup = false;
      gridColumn.AllowSort = false;
    }
  }

3 Answers, 1 is accepted

Sort by
0
Accepted
Julian Benkov
Telerik team
answered on 20 Jul 2009, 06:56 AM
Hi Frank,

The problem is related to how ChildTemplates are created. I suggest you to consider the following implementation:

private GridViewTemplate CreateChildGridViewTemplate(GridViewTemplate parentTemplate) 
    if (parentTemplate.ChildGridViewTemplates.Count > 0) 
    { 
        return parentTemplate.ChildGridViewTemplates[0]; 
    } 
 
    GridViewTemplate childTemplate = new GridViewTemplate(parentTemplate.Owner); 
    PrepareGridViewTemplate(childTemplate); 
 
    GridViewRelation relation = new GridViewRelation(parentTemplate); 
    relation.ChildTemplate = childTemplate; 
    relation.RelationName = "ExceptionRelation"
    relation.ParentColumnNames.Add(FIELD_EXCEPTIONID); 
    relation.ChildColumnNames.Add(FIELD_PARENTID); 
    parentTemplate.Owner.Relations.Add(relation); 
 
    parentTemplate.ChildGridViewTemplates.Add(childTemplate); 
 
    return childTemplate; 


Greetings,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Frank
Top achievements
Rank 1
answered on 21 Jul 2009, 11:46 AM
Hi Julian Benkov,

That fixed my nested-problem, and the Table tabs :) So as i read your suggestion, the same viewtemplate can be used throughout all nested levels (in my case).

I still have an outstanding about the expandable rows. All my rows is shown as expandable, even though the Exception and InnerException should be the only expandable. How do i "switch off" the expandable mechanism for particular row ?

Thx, Frank
0
Jack
Telerik team
answered on 23 Jul 2009, 09:29 AM
Hello Frank,

Please take a look at this KB article. It shows how to hide the expand/collapse image when it is not needed. I hope it helps. If you have further questions, please write us back.

All the best,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Frank
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Frank
Top achievements
Rank 1
Jack
Telerik team
Share this question
or