2012-08-25 46 views
0

我有一個GridView,並且GridView的標題字段是我的程序中ListBox的項目。因此,每次運行時生成的列數都是動態的。基於此,如果我單擊我的GridView中關聯的編輯按鈕,如何爲該行的每個單元格生成TextBox如果標題不是數據綁定,單擊「編輯」按鈕時,如何在GridView行中獲取TextBox?

我寫的代碼的.cs是:

protected void DONE4_Click(object sender, EventArgs e) 
{ 
    Panel7.Visible = true; 

    SqlDataAdapter mydat = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con); 
    DataSet dst = new DataSet(); 
    mydat.Fill(dst, "Table"); 
    ListBox3.Items.Clear(); 
    ListBox3.DataSource = dst.Tables[0]; 
    ListBox3.DataTextField = dst.Tables[0].Columns["Profile_Instance"].ColumnName; 
    ListBox3.DataBind(); 

    int count = ListBox3.Items.Count; 

    DataTable dt = new DataTable(); 
    DataRow rw = default(DataRow); 
    for (int i = 0; i < ListBox1.Items.Count; i++) 
    { 
     dt.Columns.Add(ListBox1.Items[i].ToString(), 
             System.Type.GetType("System.String")); 
    } 

    for (int j = 0; j < count; j++) 
    { 
     rw = dt.NewRow(); 
     for (int i = 0; i < ListBox1.Items.Count; i++) 
     { 
      rw[ListBox1.Items[i].ToString()] = " "; 
     } 
     dt.Rows.Add(rw); 
    } 

    GridView2.DataSource = dt; 
    GridView2.DataBind(); 

    foreach (GridViewRow grdRow in GridView2.Rows) 
    { 
     DropDownList bind_dropdownlist = new DropDownList();             // defining the property of the DropDownList as bind_dropdownlist 
     bind_dropdownlist = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[0].FindControl("Pro_List")); // finding the DropDownList from the gridiew for binding 
     SqlDataAdapter mydata = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con); 
     DataSet dset = new DataSet();                   // binding the DropDownList with the dataset ds 
     mydata.Fill(dset, "Table"); 
     bind_dropdownlist.DataSource = dset; 
     bind_dropdownlist.DataTextField = "Profile_Instance";             // set the DropDownList's DataTextField as designation which display the designation in the dropdownlist after fetching the data from database 
     bind_dropdownlist.DataBind(); 
     bind_dropdownlist.Items.Insert(0, new ListItem("---Choose Profile---", "-1")); 
    } 
} 

的設計代碼爲GridView是:

<asp:Panel ID="Panel7" runat="server"> 
    <asp:GridView ID="GridView2" runat="server" CellPadding="4" 
     style="text-align: center; font-size: small" BackColor="White" 
     BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"> 

     <Columns>     
     <asp:TemplateField HeaderText=""> 
      <ItemTemplate> 
       <asp:DropDownList ID="Pro_List" runat="server"> 
        <asp:ListItem>--Select--</asp:ListItem>       
       </asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField> 

     <asp:CommandField ShowEditButton="True" /> 

     </Columns> 
    </asp:GridView> 
</asp:Panel> 

是否有人可以幫助我在此?我希望問題清楚。

回答

1

如果您不是綁定GridView並且仍然想訪問其中的控件,則可以使用GridViewRow屬性。

protected void grd_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GridViewRow selectRow = grd.Rows[e.NewEditIndex]; 
    TextBox txtKj=(TextBox)selectRow.Cells[3].FindControl("txtKjId"); 
} 
+0

這給了我一個錯誤:非可調用成員'System.Web.UI.WebControls.GridView.Rows'不能像方法一樣使用。 –

+0

將grd.Rows(e.NewEditIndex)更改爲grd.Rows [e.NewEditIndex]並檢查.. –

+0

謝謝你的工作。 –

相關問題