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
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 string title;
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();

level++;

A.title = "A";
A.order=level;

B.title = "B";
B.order=level;

C.title = "C";
C.order=level;

level++;

Table.tableheader C1 = new Table.tableheader() { title = "C1", order = level };
Table.tableheader C2 = new Table.tableheader() { title = "C2", order = level };

level++;

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

max = level;

A.rowspan=rowspan(A.order);
A.colspan = colspan();

B.rowspan=rowspan(B.order);

B1.rowspan = rowspan(B1.order);
B1.colspan = colspan();

B2.rowspan = rowspan(B2.order);
B2.colspan = colspan();

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

C.rowspan = rowspan(C.order);
C.colspan = colspan();

C1.rowspan = rowspan(C1.order);
C1.colspan = colspan();

C2.rowspan = rowspan(C2.order);
C2.colspan = colspan();

B21.rowspan = rowspan(B21.order);
B21.colspan = colspan();

B22.rowspan = rowspan(B22.order);
B22.colspan = colspan();

}             }
``````

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 int max;
{
public string title;
public int rowspan;
public int colspan;
public int order;

}
{
int level = 0;
}

{
if (level > max) max = level;
foreach (DataRow row in data.AsEnumerable().Where(x => x.Field<string>("parent") == headerName))
{

string title = row.Field<string>("title");

}
}
{
int cols = 0;
{
{
sHeader.rowspan = max - level + 1;
cols += 1;
}
else
{
}
}
return cols;
}
}
class Program
{
public static void Main(string[] args)
{
DataTable dt = new DataTable();

dt = new List<DataRow>() {