C# 动态输入列表

发布于 2025-01-04 13:14:37 字数 700 浏览 1 评论 0原文

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

Number of Parents: (Textbox) // int only

Number of Children: (should be a table) // int only

当用户输入父母的数量,例如 2 该表应显示 2 行供用户输入,如下所示

-------------------------------
|No.Of Parents | No.Of Children|
|--------------|---------------|
|       1      |    (input)    |
|--------------|---------------|
|       2      |    (input)    |
|--------------|---------------|

。当用户修改父母编号时,输入的父母编号为不可编辑字段。父级为 3,则表中应为 3 行。

该表是“GridView”,我添加了 2 个“templateField”。对于No.Of Children,我将“Textbox”添加到“ItemTemple”,但我不知道

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

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

在 Visual Studio C# 中可以做到这一点吗?非常感谢。

I have a question about C# and interface design. I want to design a interface like the following:

Number of Parents: (Textbox) // int only

Number of Children: (should be a table) // int only

When user enters the number of parents, e.g. 2
The table should display 2 rows for user to input like following

-------------------------------
|No.Of Parents | No.Of Children|
|--------------|---------------|
|       1      |    (input)    |
|--------------|---------------|
|       2      |    (input)    |
|--------------|---------------|

The input of no.of parents is un-edit field, when the user modify the no. of parents to 3, it should be 3 rows in the table.

The table is 'GridView', I add 2 'templateField'. For the No.Of Children, I add the 'Textbox' to the 'ItemTemple', but I don't know

1) how to display row number of table depends on the input of textbox

2) how to display the text from 1 to n rows in table.

Is it possible to do this in visual studio C#? Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2025-01-11 13:14:37

我假设因为您使用的是 GridView 是 ASP.NET 而不是 WinForms。我认为您真正想要的可以直接在页面上完成或使用自定义用户控件,而不是界面。 C# 中的术语“接口”具有特定的含义,并且略有不同:

http://msdn.microsoft.com/en-us/library/87d83y5b(v=vs.80).aspx

假设您继续并在页面上执行此操作,您需要为 NumberOfParents 文本框 TextChanged 事件添加一个事件处理程序,并在代码隐藏中添加一些简单的代码来添加行并绑定 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,这意味着你可以使用 List(t)、Dictionary 、数组等。

希望有帮助。

I assume since you're using a GridView is ASP.NET and not a WinForms. I think what you are really looking for can be done directly on your page or using a custom UserControl, not an interface. The term "Interface" in C# has a specific meaning and its a little different:

http://msdn.microsoft.com/en-us/library/87d83y5b(v=vs.80).aspx

Assuming you just go ahead and do it on the page, you need to add an eventhandler for your NumberOfParents textbox TextChanged event and some some simple code in your codebehind to add the rows and bind your gridview. In your ASPX page, something like this:

    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>

And in your codebehind, something like this:

    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();
    }

A gridview (or any other databound control) can be bound to pretty much any array or IEnumerable, which means you can use a List(t), Dictionary, array, etc.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文