Profile Picture

TreeList - GroupBy several columns at the same level

Posted By Ignacio Mendiguren 13 Years Ago
Author
Message
Ignacio Mendiguren
Posted 13 Years Ago
View Quick Profile
Junior Member

Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)

Group: Forum Members
Last Active: 11 Years Ago
Posts: 20, Visits: 1

Hi,

I wonder if there is a way to group a TreeList by more than one column, but at the same level. If I have something like:

NameSurnameModel of car
JohnBraunAudi A6
JohnBraunMercedes SLK
ThomasSilvesterWolkswagen Passat 2.0
MonicaDufreauFord Fiesta

I want to get to something like this (group by name and surname at the same level):

JohnBraun
Audi A6
Mercedes SLK
ThomasSilvester
Wolkswagen Passat 2.0
MonicaDufreau
For Fiesta

Two more questions:

  • is there a way to get the columns used in groupby and their order?
  • is there a way to modify the list of columns that appear in the context menu of the grid (right click on a column header -> Choose columns)?

Thanks,

IM



Nevron Support
Posted 13 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009

Hi Ignacio,

You cannot do this with default state of the control.

However, if you are binding your data to NTreeList control you can subclass NTreeListTableData in order to build the nodes and subitems of NTreeList to look like you described.

I made an example which is doing that, but the source is strongly tided with the data structure and if the data structure changes it may not work correctly.

First lets create a DataTable with your data:

private DataTable LoadTable()
{
    
DataTable table = new DataTable();
   
DataColumn column1 = new DataColumn("Name");
   
DataColumn column2 = new DataColumn("SurName");
   
DataColumn column3 = new DataColumn("Car");
    table.Columns.AddRange(
new DataColumn[] { column1, column2, column3 });

   
DataRow row0 = table.NewRow();
    row0[0] =
"John";
    row0[1] =
"Braun";
    row0[2] =
"Audi A6";
    table.Rows.Add(row0);

   
DataRow row1 = table.NewRow();
    row1[0] =
"John";
   
row1[1] = "Braun";
    row1[2] =
"Mercedes SLK";
    table.Rows.Add(row1);

   
DataRow row2 = table.NewRow();
    row2[0] =
"Thomas";
    row2[1] =
"Silvester";
    row2[2] =
"Volkswagen Passat 2.0";
    table.Rows.Add(row2);

   
DataRow row3 = table.NewRow();
    row3[0] =
"Monica";
    row3[1] =
"Dufreau";
   
row3[2] = "Ford Fiesta";
    table.Rows.Add(row3);

    table.DefaultView.Sort =
"Name, SurName DESC";

   
return table;
}

Then create new class that derives from NTreeListTableData and override PopulateRows method:

public class MyNTreeListData : NTreeListTableData
{
    protected override void PopulateRows()
    {
       
DataView view = Table.DefaultView;
       
int count = view.Count;
       
DataRowView row;
       
DataRowView temp = null;
       
NTreeListNode tempNode = null;

       
for (int i = 0; i < count; i++)
        {
            row = view[i];
           
string row2 = string.Empty;
           
if (i == 0)
            {
                tempNode = GetRow(row.Row);
                row2 = row.Row[2].ToString();
                AddSubNode(row2, tempNode);
                temp = row;
                continue;
            }

            if (row.Row[0] != temp.Row[0] || row.Row[1] != temp.Row[1])
            {
                Owner.Nodes.Add(tempNode);
                tempNode = GetRow(row.Row);
            }
           
else
            {
                row2 = row.Row[2].ToString();
                AddSubNode(row2, tempNode);
                temp = row;
                continue;
            }

            row2 = row.Row[2].ToString();
            AddSubNode(row2, tempNode);
            temp = row;
        }

        Owner.Nodes.Add(tempNode);
    }

    protected void AddSubNode(string nodeText, NTreeListNode node)
    {
        NTreeListNode subNode = new NTreeListNode();
        NTreeListNodeStringSubItem subitem = new NTreeListNodeStringSubItem(nodeText);
        subitem.Column = Owner.Columns[2];
        subNode.SubItems.Add(subitem);
        node.Nodes.Add(subNode);
    }

    protected NTreeListNode GetRow(DataRow row)
    {
       
NTreeListNode node = new NTreeListNode();
        object[] values = row.ItemArray;
        object value;
        int length = values.Length;
        NTreeListNodeSubItem item;

        for (int i = 0; i < length - 1; i++)
        {
            value = values[i];
            item = CreateSubItem(value);
            item.Column = Owner.Columns[i];
            node.SubItems.Add(item);
        }

        return node;
    }
}

Finally, all you need is to bind NTreeList to the data using the new class:

MyNTreeListData data = new MyNTreeListData();
data.Table = LoadTable();
data.Bind(nTreeList1);

Ones again I want to mension that this example implementation applies only for specific structure of your data.

I hope this helps.

 



Best Regards,
Nevron Support Team



Ignacio Mendiguren
Posted 13 Years Ago
View Quick Profile
Junior Member

Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)

Group: Forum Members
Last Active: 11 Years Ago
Posts: 20, Visits: 1

Hi,

And regarding the list of columns displayed in the Choose columns option, and the list of columns (and order) used when grouping, is there a way to get them?

Thanks and regards,

 

IM



Nevron Support
Posted 13 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009
Hi Ignacio,

In both cases you can get the columns from Columns property. The order of the displayed columns is the same as it is in Columns collection.
If there is a grouping usually group by column is hidden. However this column still exist in Columns collection, but its IsVisible property is false.

Best Regards,
Nevron Support Team





Similar Topics


Reading This Topic