C#动态输入列表

C#动态输入列表,c#,visual-studio,gridview,user-interface,itemtemplate,C#,Visual Studio,Gridview,User Interface,Itemtemplate,我有一个关于C#和界面设计的问题。我想设计一个如下所示的界面: 家长人数:(文本框)//仅限int 子项数:(应为表)//仅限int 当用户输入家长人数时,例如2 该表应显示2行供用户输入,如下所示 ------------------------------- |No.Of Parents | No.Of Children| |--------------|---------------| | 1 | (input) | |--------------|---

我有一个关于C#和界面设计的问题。我想设计一个如下所示的界面:

家长人数:(文本框)//仅限int

子项数:(应为表)//仅限int

当用户输入家长人数时,例如2 该表应显示2行供用户输入,如下所示

-------------------------------
|No.Of Parents | No.Of Children|
|--------------|---------------|
|       1      |    (input)    |
|--------------|---------------|
|       2      |    (input)    |
|--------------|---------------|
父项编号的输入为非编辑字段,当用户将父项编号修改为3时,表中应为3行

该表为“GridView”,我添加了2个“templateField”。对于孩子的数量,我将“文本框”添加到“ItemTemple”中,但我不知道

1) 如何显示表格的行号取决于文本框的输入

2) 如何在表中显示从1到n行的文本


可以在visual studio C#中执行此操作吗?非常感谢。

由于您使用的是ASP.NET而不是WinForms,所以我假定您使用的是GridView。我认为您真正需要的是可以直接在页面上完成,或者使用自定义用户控件,而不是界面。C#中的“接口”一词有特定的含义,但有点不同:

假设您只是在页面上继续操作,您需要为NumberOfParents textbox TextChanged事件添加一个eventhandler,并在codebehind中添加一些简单代码来添加行并绑定gridview。在您的ASPX页面中,类似以下内容:

    Number Of Parents: <asp:TextBox runat="server" ID="txtNumberOfParents" AutoPostBack="true" OnTextChanged="txtNumberOfParents_TextChanged" /><br />
    <br />
    <asp:GridView runat="server" ID="gvNumberOfChildren" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="No. of Parents">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="No. of Children">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="txtNumberOfChildren" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    protected void txtNumberOfParents_TextChanged(object sender, EventArgs e)
    {
        int numParents = 0;
        int[] bindingSource = null;

        Int32.TryParse(txtNumberOfParents.Text, out numParents);

        if (numParents > 0)
        {
            bindingSource = new int[numParents];
        }

        gvNumberOfChildren.DataSource = bindingSource;
        gvNumberOfChildren.DataBind();
    }
gridview(或任何其他数据绑定控件)可以绑定到几乎任何数组或IEnumerable,这意味着您可以使用列表(t)、字典、数组等

希望有帮助