Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#动态表_C#_Html_Asp.net_Dynamic - Fatal编程技术网

C#动态表

C#动态表,c#,html,asp.net,dynamic,C#,Html,Asp.net,Dynamic,我想动态创建一个表。我的空间有限,无法容纳此表,因此我希望将最大行数设置为10 前两列将被填充,当它达到10时,再创建另外两列,用另外10行填充它,然后再创建另外两列,依此类推 我该怎么做,或者做循环的最佳方式是什么?我想您应该翻译他的表单中的数据: A 1 B 2 C 3 D 4 E 5 F 6 G 7 进入 如果是这样的话,您可以通过以下方式实现: 首先,我使用一个类来包含每对数据项(如a,1),如下所示: 然后,我构建了一个helper函数,该函数接受列表并将其转换为您格式的表:

我想动态创建一个表。我的空间有限,无法容纳此表,因此我希望将最大行数设置为10

前两列将被填充,当它达到10时,再创建另外两列,用另外10行填充它,然后再创建另外两列,依此类推


我该怎么做,或者做循环的最佳方式是什么?

我想您应该翻译他的表单中的数据:

A 1
B 2
C 3
D 4
E 5
F 6
G 7
进入

如果是这样的话,您可以通过以下方式实现:

首先,我使用一个类来包含每对数据项(如a,1),如下所示:

然后,我构建了一个helper函数,该函数接受列表并将其转换为您格式的表:

    private const int MaxRows = 3; //Configure how many Rows in the table
    private const int NumColsPerElement = 2; //Configure how many columns per element

    static System.Data.DataTable BuildTabularTableElements(List<DataElement> elements)
    {
        System.Data.DataTable dt = new System.Data.DataTable();

        int columnCount = (((elements.Count + MaxRows - 1) / MaxRows)) * NumColsPerElement;          

        for (int x = 0; x < columnCount; x++) //Add enough columns
            dt.Columns.Add();

        for (int x = 0; x < Math.Min(MaxRows, elements.Count); x++) //Add enough rows
            dt.Rows.Add();
        for (int x = 0; x < elements.Count; x++)
        {
            int curCol = (x / MaxRows) * NumColsPerElement; //Determine the current col/row
            int curRow = x % MaxRows;

            dt.Rows[curRow][curCol] = elements[x]First you need a.Value1; //Place the data in the correct row/column
            dt.Rows[curRow][curCol+1] = elements[x].Value2;
        }
        return dt;
    }
private const int MaxRows=3//配置表中的行数
私有常量int numcolsperelation=2//配置每个元素的列数
静态System.Data.DataTable BuildTableElements(列表元素)
{
System.Data.DataTable dt=新的System.Data.DataTable();
int columnCount=((elements.Count+MaxRows-1)/MaxRows))*numcolsperelation;
对于(int x=0;x
最后,要进行测试,请使用以下命令:

       List<DataElement> e = new List<DataElement>();
       e.Add(new DataElement() { Value1 = "1", Value2 = "2" });
       e.Add(new DataElement() { Value1 = "3", Value2 = "4" });
       e.Add(new DataElement() { Value1 = "5", Value2 = "6" });
       e.Add(new DataElement() { Value1 = "7", Value2 = "8" });

       System.Data.DataTable dt = BuildTabularTableElements(e);
List e=新列表();
e、 添加(newdataelement(){Value1=“1”,Value2=“2”});
e、 添加(新数据元素(){Value1=“3”,Value2=“4”});
e、 添加(newdataelement(){Value1=“5”,Value2=“6”});
e、 添加(新数据元素(){Value1=“7”,Value2=“8”});
System.Data.DataTable dt=BuildTableTableElements(e);

请注意,我使用常量来控制要使用的行数和列数。在我的示例中,每个数据元素最多使用3行和2列。这当然可以清理一下,但我想这是你想要的。您可以将常量拉入方法参数,并更改列表以使用标准数组,以便提供高度灵活性,从而可以使用任意大小的表和列计数,但如果需要,我将留给您决定

你在说什么?数据库表?html表格?或者winforms,silverlight,wpf。。。你的问题一点也不清楚。请详细解释,否则没人能帮你。这是WinForms吗?WPF?银灯?ASP.NETHTML?新华社?wxWidgets。。。这在很大程度上影响了你所需要的答案。在将来,试着发布一个你想做的翻译的例子,这样读者就更容易想象你在寻找什么。我在下面发布了我认为你的翻译是什么。
    private const int MaxRows = 3; //Configure how many Rows in the table
    private const int NumColsPerElement = 2; //Configure how many columns per element

    static System.Data.DataTable BuildTabularTableElements(List<DataElement> elements)
    {
        System.Data.DataTable dt = new System.Data.DataTable();

        int columnCount = (((elements.Count + MaxRows - 1) / MaxRows)) * NumColsPerElement;          

        for (int x = 0; x < columnCount; x++) //Add enough columns
            dt.Columns.Add();

        for (int x = 0; x < Math.Min(MaxRows, elements.Count); x++) //Add enough rows
            dt.Rows.Add();
        for (int x = 0; x < elements.Count; x++)
        {
            int curCol = (x / MaxRows) * NumColsPerElement; //Determine the current col/row
            int curRow = x % MaxRows;

            dt.Rows[curRow][curCol] = elements[x]First you need a.Value1; //Place the data in the correct row/column
            dt.Rows[curRow][curCol+1] = elements[x].Value2;
        }
        return dt;
    }
       List<DataElement> e = new List<DataElement>();
       e.Add(new DataElement() { Value1 = "1", Value2 = "2" });
       e.Add(new DataElement() { Value1 = "3", Value2 = "4" });
       e.Add(new DataElement() { Value1 = "5", Value2 = "6" });
       e.Add(new DataElement() { Value1 = "7", Value2 = "8" });

       System.Data.DataTable dt = BuildTabularTableElements(e);