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;
}
}