Search code examples
c#visual-studiogridviewuser-interfaceitemtemplate

C# Dynamic Input list


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.


Solution

  • 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.