2016-10-24 152 views
0

我目前有一個gridview沒有行,只有標題。我有一個ASP控件textboxOnTextChange事件。所以每次輸入textbox時,我的gridview都會根據它生成行數。和行內,將有dropdownlist基於TextBox輸入動態生成GridView行

舉例來說,在我的textbox,I型數2,2列,將在GridView的產生。

我目前使用ASP.NET

文本框:

[ 2 ] 

GridView控件:

---------------------------------------------------- 
| S/N |      |      | 
---------------------------------------------------- 
| 1 | [dropdownlist]  |  [dropdownlist] | 
|--------------------------------------------------| 
| 2 | [dropdownlist]  |  [dropdownlist] | 
-------------------------------------------------- 

回答

0

這裏是一個片段,讓你開始。在GridView中,您可以使用<TemplateField>來創建所需的佈局。之後,您可能需要查看OnRowDataBound事件以填充DropDownLists。

protected void Button1_Click(object sender, EventArgs e) 
{ 
    int rowCount = 0; 

    //get the number from the textbox and try to convert to int 
    try 
    { 
     rowCount = Convert.ToInt32(TextBox1.Text); 
    } 
    catch 
    { 
    } 

    //set the new rowcount as a viewstate so it can be used after a postback 
    ViewState["rowCount"] = rowCount; 

    //start the function to fill the grid 
    fillGrid(); 
} 

private void fillGrid() 
{ 
    int rowCount = 0; 

    //get the current row count from the viewstate 
    if (ViewState["rowCount"] != null) 
    { 
     rowCount = Convert.ToInt32(ViewState["rowCount"]); 
    } 

    //create a new DataTable with three columns. 
    DataTable table = new DataTable(); 
    table.Columns.Add("ID", typeof(int)); 
    table.Columns.Add("Name", typeof(string)); 
    table.Columns.Add("Created", typeof(DateTime)); 

    //loop to add the row to the table 
    for (int i = 0; i < rowCount; i++) 
    { 
     table.Rows.Add(0, "Name_" + i.ToString(), DateTime.Now.AddMinutes(i)); 
    } 

    //bind the table to the grid 
    GridView1.DataSource = table; 
    GridView1.DataBind(); 
}