【问题标题】:Dynamically adding drop down lists inside gridview cells在gridview单元格内动态添加下拉列表
【发布时间】:2013-07-29 13:40:32
【问题描述】:

我有一个网格视图,如果数据库为特定列返回 null,我想插入一个从数据库填充的下拉列表。

这是我必须识别该列是否为空并且工作正常的代码。

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
     }
}

这是网格视图:

    <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>

此外,一旦我填充了该行,当有多个版本时,我如何知道具体使用的是哪个下拉列表?

【问题讨论】:

    标签: c# asp.net sql-server gridview drop-down-menu


    【解决方案1】:
    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;
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 2017-06-30
      • 2023-03-15
      相关资源
      最近更新 更多