Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# 将变量嵌套列表绑定到GridView_C#_Asp.net_Gridview_Data Binding_Nested Lists - Fatal编程技术网

C# 将变量嵌套列表绑定到GridView

C# 将变量嵌套列表绑定到GridView,c#,asp.net,gridview,data-binding,nested-lists,C#,Asp.net,Gridview,Data Binding,Nested Lists,我试图根据用户的输入在页面上显示乘法表。这是我的代码: <asp:GridView runat="server" ID="TableData"></asp:GridView> List<List<int>> nestedList = new List<List<int>>(); protected void LoadTable(int val) { for (int y = 0; y <= val; y++

我试图根据用户的输入在页面上显示乘法表。这是我的代码:

<asp:GridView runat="server" ID="TableData"></asp:GridView>

List<List<int>> nestedList = new List<List<int>>();

protected void LoadTable(int val)
{
    for (int y = 0; y <= val; y++)
    {
        List<int> list = new List<int>();
        for (int x = 0; x <= val; x++)
            list.Add(x * y);
        nestedList.Add(list);
    }

    TableData.DataSource = nestedList;
    TableData.DataBind();
}
我做错了什么

为了澄清,如果用户输入5,输出应为:

 0  0  0  0  0  0
 0  1  2  3  4  5
 0  2  4  6  8 10
 0  3  6  9 12 15
 0  4  8 12 16 20
 0  5 10 15 20 25

现在我不担心列标题或行标题。

问题在于您的项目来源

列表>(我认为)这不是一个好的选择。 对于线性视图,可以使用此方法 代码片段

            var objList = new List<object>();
            for (int i = 0; i < 5; i++)
            {
                var temp = new { operation = string.Format("{0} * {1}", i, i + 1), result = i * (i + 1) };
                objList.Add(temp);
            }
var objList=new List();
对于(int i=0;i<5;i++)
{
var temp=new{operation=string.Format(“{0}*{1}”,i,i+1),result=i*(i+1)};
对象列表添加(临时);
}

问题在于您的项目来源

列表>(我认为)这不是一个好的选择。 对于线性视图,可以使用此方法 代码片段

            var objList = new List<object>();
            for (int i = 0; i < 5; i++)
            {
                var temp = new { operation = string.Format("{0} * {1}", i, i + 1), result = i * (i + 1) };
                objList.Add(temp);
            }
var objList=new List();
对于(int i=0;i<5;i++)
{
var temp=new{operation=string.Format(“{0}*{1}”,i,i+1),result=i*(i+1)};
对象列表添加(临时);
}

< /代码>

GRIDVIEW不支持2D列表绑定,请考虑使用另一个方法。

例如,使用一个简单的列表,每个字符串将代表一行,您可以使用如下循环填充每个字符串:

(first loop)
{
    string s;
    for(int x = 0; x < val; x ++)
    {
        s += (x * y).Tostring() + " ");

    }
    nestedList.Add(s);
}
(第一个循环)
{
字符串s;
对于(int x=0;x< /代码> 

GRIDVIEW不支持2D列表绑定,请考虑使用另一个方法。

例如,使用一个简单的列表,每个字符串将代表一行,您可以使用如下循环填充每个字符串:

(first loop)
{
    string s;
    for(int x = 0; x < val; x ++)
    {
        s += (x * y).Tostring() + " ");

    }
    nestedList.Add(s);
}
(第一个循环)
{
字符串s;
对于(int x=0;x
我不确定您想要的是什么样的输出,但是如果您不指定gridview应该如何呈现输出,它将为网格中显示的类型的每个公共属性显示一列。在这种情况下,
List
有两个公共属性:
Capacity
Count
。我不明白为什么要使用列表作为项目源。你能告诉我为什么吗?post
.aspx
page gridview代码。将其更改为列表对输出没有影响。我想要的输出是一个乘法表,就像你在小学时做的那样。第x行第y列的单元格是x和y的乘积。我不确定您要输出什么,但如果您不指定gridview应如何呈现输出,它将为网格中显示的类型的每个公共属性显示一列。在这种情况下,
List
有两个公共属性:
Capacity
Count
。我不明白为什么要使用列表作为项目源。你能告诉我为什么吗?post
.aspx
page gridview代码。将其更改为列表对输出没有影响。我想要的输出是一个乘法表,就像你在小学时做的那样。第x行第y列的单元格是x和y的乘积。此代码将具有固定的列数。乘法表在运行时会有一个用户定义的列数和行数。因此,这种方法对我没有帮助。@j正如我所说的,这是一个线性结果的代码段。这段代码的列数是固定的。乘法表在运行时会有一个用户定义的列数和行数。因此,这种方法对我没有帮助。@JHasell,正如我所说,它是线性结果的代码片段