2013-07-29 102 views
0

我有一個網格視圖,如果數據庫爲特定列返回null我想插入從數據庫填充的下拉列表。動態添加GridView單元格內的下拉列表

這裏是代碼我必須確定列是空的,它工作正常。

protected void viewThemeTypeAssociationsGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.Cells[1].Text == " ") 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
     //fill current rows cell with a dropdown list 
    } 
} 

這裏是在GridView:

<asp:GridView ID="viewThemeTypeAssociationsGridView" runat="server" 
    AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" 
    BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
    DataSourceID="SqlDataSource6" OnRowDataBound="viewThemeTypeAssociationsGridView_OnRowDataBound"> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
     <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> 
    </Columns> 
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 
    <SortedAscendingCellStyle BackColor="#FFF1D4" /> 
    <SortedAscendingHeaderStyle BackColor="#B95C30" /> 
    <SortedDescendingCellStyle BackColor="#F1E5CE" /> 
    <SortedDescendingHeaderStyle BackColor="#93451F" /> 
</asp:GridView> 

<asp:SqlDataSource ID="SqlDataSource6" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" > 
    </asp:SqlDataSource> 

此外,一旦我填充行,我怎麼知道該下拉列表時,有它的許多版本,特別是正在使用?

回答

2
Design 
<asp:GridView ID="viewThemeTypeAssociationsGridView" runat="server" AutoGenerateColumns="False" 
      BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 
      CellPadding="3" CellSpacing="2" 
      OnRowDataBound="viewThemeTypeAssociationsGridView_RowDataBound" 
      DataSourceID="Sql_New" > 
      <Columns> 
       <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" 
        InsertVisible="False" ReadOnly="True" /> 
       <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
       <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> 
       <asp:TemplateField HeaderText ="New Column" > 
       <ItemTemplate > 
       <asp:DropDownList ID="ddlnew" runat ="server" Visible ="false" ></asp:DropDownList> 
       </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
      <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
      <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
      <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 
      <SortedAscendingCellStyle BackColor="#FFF1D4" /> 
      <SortedAscendingHeaderStyle BackColor="#B95C30" /> 
      <SortedDescendingCellStyle BackColor="#F1E5CE" /> 
      <SortedDescendingHeaderStyle BackColor="#93451F" /> 
     </asp:GridView> 
     <asp:SqlDataSource ID="Sql_New" runat="server" 
      ConnectionString="<%$ ConnectionStrings:EmployeeConnectionString %>" 
      SelectCommand="SELECT * FROM [tblnew]"></asp:SqlDataSource> 


Code 
protected void viewThemeTypeAssociationsGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.Cells[2].Text == "&nbsp;") 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       DropDownList ddlnew = (DropDownList)e.Row.FindControl("ddlnew"); 
       ddlnew.Visible = true; 
      } 
     } 

    } 
相關問題