Coder Coder -4 years ago 85
C# Question

how to write a recursive function for my multilevel list and apply my logic of rowspan and colspan within it

This is my code which has a list of table. consisting
headers,subheaders and subsubheaders. i have two fields rowspan and
colspan for each of my headers,subheaders and subsubheaders which i
need to set using a recursive function. In the below code i have
written the logic for setting rowspan and colspan in two different
functions rowspan() and colspan(). And have used them in my main
function.But for each of my headers and subheaders i have to set them
separately. How can i perform it by using a recursive function? kindly help.
this is the table for which iam setting rowspan and colspan

namespace ConsoleApplication3 {
public class Table
{
public List<tableheader> headers {get; set;}

public class tableheader
{
public string title;
public List<tableheader> subheaders = new List<tableheader>();
public int rowspan;
public int colspan;
public int order;

}
}
}
class Program
{
static int level;
static int max;
static int count;

public static int rowspan(int level)
{
int x;
if (count!=0)
{
x=1;
}
else
{
x =( max - level) + 1;
}
return x;
}

public static int colspan()
{
int y = 1;
if ( count != 0)
{

y=count;
}
return y;

}

public static void Main(string[] args)
{
level = 0;
Table table=new Table();
table.headers=new List<Table.tableheader>();

level++;

Table.tableheader A = new Table.tableheader();
A.title = "A";
A.order=level;


Table.tableheader B = new Table.tableheader();
B.title = "B";
B.order=level;


Table.tableheader C = new Table.tableheader();
C.title = "C";
C.order=level;

level++;

B.subheaders = new List<Table.tableheader>();

Table.tableheader B1 = new Table.tableheader() {title="B1" ,order=level};
Table.tableheader B2 = new Table.tableheader() {title ="B2",order=level };
B.subheaders.Add(B1);
B.subheaders.Add(B2);

C.subheaders = new List<Table.tableheader>();
Table.tableheader C1 = new Table.tableheader() { title = "C1", order = level };
Table.tableheader C2 = new Table.tableheader() { title = "C2", order = level };
C.subheaders.Add(C1);
C.subheaders.Add(C2);

level++;

Table.tableheader B21 = new Table.tableheader() { title = "B21", order = level };
Table.tableheader B22 = new Table.tableheader() { title = "B22", order = level };
B2.subheaders.Add(B21);
B2.subheaders.Add(B22);

table.headers.Add(A);
table.headers.Add(B);
table.headers.Add(C);

max = level;

count=A.subheaders.Count();
A.rowspan=rowspan(A.order);
A.colspan = colspan();

count=B.subheaders.Count();
B.rowspan=rowspan(B.order);

count = B1.subheaders.Count();
B1.rowspan = rowspan(B1.order);
B1.colspan = colspan();

count = B2.subheaders.Count();
B2.rowspan = rowspan(B2.order);
B2.colspan = colspan();

count = B1.rowspan + B2.rowspan;
B.colspan = colspan();

count = C.subheaders.Count();
C.rowspan = rowspan(C.order);
C.colspan = colspan();

count = C1.subheaders.Count();
C1.rowspan = rowspan(C1.order);
C1.colspan = colspan();

count = C2.subheaders.Count();
C2.rowspan = rowspan(C2.order);
C2.colspan = colspan();

count = B21.subheaders.Count();
B21.rowspan = rowspan(B21.order);
B21.colspan = colspan();

count = B22.subheaders.Count();
B22.rowspan = rowspan(B22.order);
B22.colspan = colspan();

} }

Answer Source

See if code below helps. Made lots of changes to your original. I didn't write recursive function but you should be able to figure this out yourself with my updated code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication3
{
    public class Table
    {
        public static Tableheader headers { get; set; }
        public static int max;
        public class Tableheader
        {
            public string title;
            public List<Tableheader> subheaders;
            public int rowspan;
            public int colspan;
            public int order;

        }
        public void Add(DataTable data)
        {
            headers = new Tableheader();
            int level = 0;
            AddRecursive(data, headers, "", 0);
        }

        public void AddRecursive(DataTable data, Tableheader headers, string headerName, int level)
        {
            if (level > max) max = level;
            foreach (DataRow row in data.AsEnumerable().Where(x => x.Field<string>("parent") == headerName))
            {
                if (headers.subheaders == null) headers.subheaders = new List<Tableheader>();

                Tableheader newHeader = new Tableheader();
                headers.subheaders.Add(newHeader);

                string title = row.Field<string>("title");
                newHeader.title = title;
                AddRecursive(data, newHeader, title, level++);

            }
        }
        public int AddRecursiveSpan(int level, Tableheader header)
        {
            int cols = 0;
            foreach (Tableheader sHeader in header.subheaders)
            {
                if (sHeader.subheaders == null)
                {
                    sHeader.rowspan = max - level + 1;
                    sHeader.colspan = 1;
                    cols += 1;
                }
                else
                {
                    sHeader.colspan += AddRecursiveSpan(level + 1, sHeader);
                    sHeader.rowspan = 1;
                    cols += sHeader.colspan;
                }
            }
            return cols;
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("title", typeof(string));
            dt.Columns.Add("parent", typeof(string));


            dt = new List<DataRow>() {
                 dt.Rows.Add(new object[] {"A",""}),
                 dt.Rows.Add(new object[] {"B",""}),
                 dt.Rows.Add(new object[] {"C",""}),
                 dt.Rows.Add(new object[] {"B1","B"}),
                 dt.Rows.Add(new object[] {"B2","B"}),
                 dt.Rows.Add(new object[] {"C1","C"}),
                 dt.Rows.Add(new object[] {"C2","C"}),
                 dt.Rows.Add(new object[] {"B21","B2"}),
                 dt.Rows.Add(new object[] {"B22","B2"})
             }.CopyToDataTable();

            Table table = new Table();
            table.Add(dt);
            int level = 1;
            table.AddRecursiveSpan(level,Table.headers);
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download