Hi.
I've followed the steps in this how-to to set up a hierarchical grid:
http://docs.telerik.com/devtools/winforms/gridview/hierarchical-grid/object-relational-hierarchy-mode
Now I want to generate the columns in the child template myself to avoid displaying internal fields like database IDs etc..
Even in your example in the 'Manually generating hierarchy mode' section you add columns in the CreateChildTemplate() method, but never turn off the autogenerated columns. So, as in the figure shown, you just add the columns additionally to the autogenerated columns.
If I set the template.AutoGenerateColumns = False, then the database relation somehow gets broken. However, the subdatasets won't be shown after clicking the expand button.
Regards,
Bernhard
15 Answers, 1 is accepted
Thank you for writing.
As it is described in the beginning of the article, it is necessary to set the AutoGenerateHierarchy property to true. I have prepared a sample project for your reference.
I hope this information helps. Should you have further questions I would be glad to help.
Regards,
Dess
Telerik by Progress
Hi Dess.
I think you misunderstood my problem.
For example, in the sample you sent, let's say I'd like to display just the ProductName and the Picture in the child grid.
So the child template would look like something like this:
private GridViewTemplate CreateChildTemplate()
{
GridViewTemplate childTemplate = new GridViewTemplate();
childTemplate.AutoGenerateColumns = false;
GridViewDataColumn column = new GridViewDecimalColumn("ProductName");
childTemplate.Columns.Add(column);
column = new GridViewDecimalColumn("Picture");
childTemplate.Columns.Add(column);
//column = new GridViewDecimalColumn("ProductName");
//childTemplate.Columns.Add(column);
//column = new GridViewDecimalColumn("UnitPrice");
//childTemplate.Columns.Add(column);
//column = new GridViewDecimalColumn("SupplierID");
//childTemplate.Columns.Add(column);
//column = new GridViewDecimalColumn("CategoryID");
//childTemplate.Columns.Add(column);
//column = new GridViewDecimalColumn("UnitsInStock");
//childTemplate.Columns.Add(column);
childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this.radGridView1.Templates.Add(childTemplate);
return childTemplate;
}
I set the AutoGenerateColumns property to false and add just the two columns to the template. But this code change doesn't change a thing in the output. The grid would always display all columns. Setting the AutoGenerateHierarchy property to false leads to an empty product list though...
Regards,
Bernhard
Thank you for writing back.
The provided clarification is greatly appreciated. When the RadGridView.AutoGenerateHierarchy property is set to true, the grid automatically will create all columns in order to set up the hierarchy. If you need to display only specific columns in the child template, you have two options:
1. Set the AutoGenerateHierarchy property to true and hide all redundant columns.
2. Set the AutoGenerateHierarchy property to false, the child template's AutoGenerateColumns property to false, add the desired columns and set up the relation programmatically. Additional information about setting up the relation is available here: http://docs.telerik.com/devtools/winforms/gridview/hierarchical-grid/binding-to-hierarchical-data-programmatically
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik by Progress
Hi Dess.
[quote]1. Set the AutoGenerateHierarchy property to true and hide all redundant columns.[/quote]
How do I hide auto generated columns? When setting up the template, the columns collection is empty. So I have to use an event after databinding to set the column's isVisible property to false?
[quote]2. Set the AutoGenerateHierarchy property to false, the child template's AutoGenerateColumns property to false, add the desired columns and set up the relation programmatically.[/quote]
Can you please provide working code - in the sample you attached - for this approach?
The following code leads to the attached output. I don't get it why the child rows are not displayed.
private void RadForm1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'nwindDataSet.Products' table. You can move, or remove it, as needed.
this.productsTableAdapter.Fill(this.nwindDataSet.Products);
// TODO: This line of code loads data into the 'nwindDataSet.Suppliers' table. You can move, or remove it, as needed.
this.suppliersTableAdapter.Fill(this.nwindDataSet.Suppliers);
GridViewTemplate childTemplate = CreateChildTemplate();
GridViewRelation relation = new GridViewRelation(this.radGridView1.MasterTemplate, childTemplate);
relation.ChildColumnNames.Add("Products");
this.radGridView1.AutoGenerateHierarchy = false;
this.radGridView1.DataSource = this.suppliersBindingSource;
this.radGridView1.Relations.Add(relation);
}
private GridViewTemplate CreateChildTemplate()
{
GridViewTemplate childTemplate = new GridViewTemplate();
GridViewDataColumn column = new GridViewDecimalColumn("ProductName");
childTemplate.AutoGenerateColumns = false;
childTemplate.Columns.Add(column);
column = new GridViewDecimalColumn("Picture");
childTemplate.Columns.Add(column);
childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this.radGridView1.Templates.Add(childTemplate);
return childTemplate;
}
Thank you for writing back.
According to the referred help article, I have modified the sample code in order to set up the relation and bind the child template. Note that it is important that the column which participates in the relation between the parent and child tables is added to the child template. Otherwise, it is not possible to extract the relevant data for each parent row. Here is the code snippet which result is illustrated in the attached screenshot:
private
void
RadForm1_Load(
object
sender, EventArgs e)
{
this
.productsTableAdapter.Fill(
this
.nwindDataSet.Products);
this
.suppliersTableAdapter.Fill(
this
.nwindDataSet.Suppliers);
this
.radGridView1.AutoGenerateHierarchy =
false
;
this
.radGridView1.DataSource =
this
.suppliersBindingSource;
GridViewTemplate childTemplate = CreateChildTemplate();
childTemplate.DataSource =
this
.productsBindingSource;
this
.radGridView1.MasterTemplate.Templates.Add(childTemplate);
GridViewRelation relation =
new
GridViewRelation(
this
.radGridView1.MasterTemplate, childTemplate);
relation.ParentColumnNames.Add(
"SupplierID"
);
relation.ChildColumnNames.Add(
"SupplierID"
);
this
.radGridView1.Relations.Add(relation);
}
private
GridViewTemplate CreateChildTemplate()
{
GridViewTemplate childTemplate =
new
GridViewTemplate();
childTemplate.AutoGenerateColumns =
false
;
GridViewDecimalColumn column1 =
new
GridViewDecimalColumn(
"ProductID"
);
childTemplate.Columns.Add(column1);
column1.FieldName =
"ProductID"
;
GridViewTextBoxColumn column2 =
new
GridViewTextBoxColumn(
"ProductName"
);
childTemplate.Columns.Add(column2);
column2.FieldName =
"ProductName"
;
GridViewDecimalColumn column3 =
new
GridViewDecimalColumn(
"SupplierID"
);
column3.FieldName =
"SupplierID"
;
column3.IsVisible =
false
;
childTemplate.Columns.Add(column3);
childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
return
childTemplate;
}
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik by Progress
Hi Dess,
I have two issue in my Radgrid
- There is borders of columns are not showing when export to Excel, or getting dataonly when export. I need to show both case that with border or without border.
- If Hierarchical data Grid : The expanded chilled templates are not showing when take print preview of grid.
Regards
Jamshi
I am not sure which export exactly you are using but I would recommend you to use the GridViewSpreadExport. It utilizes our RadSpreadProcessing library to export the content of RadGridView to xlsx, csv, pdf and txt formats. The ExportHierarchy property defines whether the exporter will export hierarchical data or not and the ChildViewExportMode property defines which child view of a hierarchy row to be exported.
The SpreadExporter.CellFormatting is used to format the cells to be exported. Thus you can achieve the custom design that you need. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/exporting-data/spread-export
I hope this information helps. If you have any additional questions, please let me know.
Dess
Progress Telerik
Hi Dess,
Thanks for your answer, But
Where I asked about hierarchy data not in case of export, I am asking in case of print preview of hierarchical grid.
I take Print Preview of hierarchical grid, Where not showing chilled template.
Is there any property to show template in print preview.
Thanks
Jamshi
Please excuse me for the misunderstanding. If you need to print the child rows when calling the PrintPreview method, it is necessary to set the GridPrintStyle.PrintHierarchy property to true:
GridPrintStyle style =
new
GridPrintStyle();
style.PrintHierarchy =
true
;
this
.radGridView1.PrintStyle = style;
this
.radGridView1.PrintPreview();
If You need any further assistance please don't hesitate to contact me.
Regards,
Dess
Progress Telerik
hello Dess,
Thanks, I got chilled rows in PrintPreview, But
Still I didn't get the border when export to Excel.
I tried SpreadExporter.cellformating event, but I am not getting correct output.
here s my code to export
GridViewSpreadExport spreadExporter = new GridViewSpreadExport(dgv);
SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
spreadExporter.ExportChildRowsGrouped = false;
spreadExporter.ExportFormat = SpreadExportFormat.Xlsx;
spreadExporter.ExportHierarchy = true;
spreadExporter.ExportGroupedColumns = false;
spreadExporter.ExportViewDefinition = false;
spreadExporter.FileExportMode = FileExportMode.CreateOrOverrideFile;
spreadExporter.HiddenColumnOption = HiddenOption.DoNotExport;
spreadExporter.HiddenRowOption = HiddenOption.DoNotExport;
spreadExporter.PagingExportOption = PagingExportOption.AllPages;
spreadExporter.SheetName = "Sheet";
spreadExporter.SummariesExportOption = SummariesOption.DoNotExport;
//spreadExporter.CellFormatting += exporter_CellFormatting;
string filename = commandButton("xlsx");
if (filename != "")
{
spreadExporter.RunExport(filename, exportRenderer);
}
Thanks
Jamshi
According to the provided code snippet, it seems that the CellFormatting event handler is missing for the GridViewSpreadExport. I have prepared a sample code snippet demonstrating how to assign borders for the exported cells:
ThemableColor purple =
new
ThemableColor(System.Windows.Media.Color.FromArgb(255, 155, 89, 182));
ThemableColor darkBlue =
new
ThemableColor(System.Windows.Media.Color.FromArgb(255, 44, 62, 80));
CellBorders border;
private
void
exporter_CellFormatting(
object
sender, Telerik.WinControls.Export.CellFormattingEventArgs e)
{
if
(border ==
null
)
{
border =
new
Telerik.Windows.Documents.Spreadsheet.Model.CellBorders(
new
CellBorder(CellBorderStyle.Medium, darkBlue),
new
CellBorder(CellBorderStyle.Medium, darkBlue),
new
CellBorder(CellBorderStyle.Medium, darkBlue),
new
CellBorder(CellBorderStyle.Medium, darkBlue),
new
CellBorder(CellBorderStyle.Thin, purple),
new
CellBorder(CellBorderStyle.Thin, purple),
new
CellBorder(CellBorderStyle.None, darkBlue),
new
CellBorder(CellBorderStyle.None, darkBlue));
}
e.CellStyleInfo.Borders = border;
}
Since the GridViewSpreadExport uses the RadSpreadProcessing library, I would recommend you to have a look at the following help article demonstrating how to customize the cell's style: https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/working-with-cells/get-set-clear-properties#borders-property
If you need any further assistance please don't hesitate to contact me.
Regards,
Dess
Progress Telerik
Hello, Jamsheer,
The purpose of the printing behavior in RadGridView is to print all rows together with their child rows when there is a hierarchical template no matter of the current expanded state of the row.
If you need to skip printing the child records for a certain parent row, e.g. the second row in the grid, the possible solution that I can suggest is to hide the child rows for this particular parent row before invoking the print logic and restore the rows visibility after it. You can find below a sample code snippet demonstrating how to skip printing the child rows for the parent row at index 1:
private void radButton1_Click(object sender, EventArgs e)
{
foreach (GridViewRowInfo row in this.radGridView1.Rows[1].ChildRows)
{
row.IsVisible = false;
}
GridPrintStyle style = new GridPrintStyle();
style.PrintHierarchy = true;
this.radGridView1.PrintStyle = style;
this.radGridView1.PrintPreview();
foreach (GridViewRowInfo row in this.radGridView1.Rows[1].ChildRows)
{
row.IsVisible = true;
}
}
Should you have further questions please let me know.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Hi Dess,
Its working fine but, If the row is not expanded then I don't want to see the template column header also not only the data rows
I need to show the template grid header column only when parent is expanded.
How could I hide.
Regards
Jamshi
Hello, Jamsheer,
In order to achieve your goal, I would suggest you an alternative solution. However, it would require creating a custom PrintGridTraverser. Thus, in its CanStepInHierarchy method you can control whether the child rows will be traversed when printing considering the expanded state of the row. Please refer to the following code snippet:
private void radButton1_Click(object sender, EventArgs e)
{
CustomGridPrintStyle style = new CustomGridPrintStyle();
style.PrintHierarchy = true;
this.radGridView1.PrintStyle = style;
this.radGridView1.PrintPreview();
}
public class CustomGridPrintStyle : GridPrintStyle
{
public override void Initialize()
{
base.Initialize();
FieldInfo fi = typeof(GridPrintStyle).GetField("traverser", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(this, new CustomPrintGridTraverser(this.GridView.MasterView));
}
}
public class CustomPrintGridTraverser : PrintGridTraverser
{
public CustomPrintGridTraverser(GridViewInfo viewInfo) : base(viewInfo)
{
}
protected override bool CanStepInHierarchy()
{
GridViewHierarchyRowInfo hierarchyRow = this.Traverser.Current as GridViewHierarchyRowInfo;
if (hierarchyRow != null)
{
return hierarchyRow.IsExpanded;
}
return base.CanStepInHierarchy();
}
}
Should you have further questions please let me know.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik