2012-02-12 61 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