2012-02-12 62 views
0

我有一个关于C#和界面设计的问题。我想设计如下所示的界面: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#中做到这一点?非常感谢你。

回答

0

我假设你使用的GridView是ASP.NET而不是WinForms。我认为你真正想要的东西可以直接在你的页面上完成,或者使用一个自定义的UserControl而不是一个接口。在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,数组等。

+0

非常感谢。一个很好的解决方案。 :) – 2012-02-12 05:50:44