簡體   English   中英

如何在GridView單元格內動態添加下拉列表

[英]How to dynamically add drop down lists inside gridview cells

我有一個網格視圖,如果數據庫為特定列返回空值,我想插入一個從數據庫填充的下拉列表。

這是我必須識別該列為空的代碼,並且它可以正常工作。

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

另外,一旦我填充了該行,當它有很多版本時,我如何知道正在使用哪個下拉列表?

使可能填充的下拉列表成為行模板的一部分。 使下拉列表默認情況下不可見,然后僅當您使用數據庫中的數據填充下拉列表時,該列表才可見。

沒有看到您的代碼,我想您正在使用TemplateField在網格視圖中定義列,如下所示:

<asp:GridView id="viewThemeTypeAssociationsGridView" ruant="server" OnRowDataBound="viewThemeTypeAssociationsGridView_OnRowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
            <ItemTemplate>
                <asp:DropDownList id="DropDownList1" runat="server" Visible="False">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

現在,對於網格視圖中的每一行,當您處於RowDataBound事件中時,都可以找到下拉列表,如下所示:

if (e.Row.RowType == DataControlRowType.DataRow)
{
     // Find the drop down list by name
     DropDownList theDropDownList = (DropDownList)e.Row.FindControl("DropDownList1");

     // Go get data from database and populate the drop down list

     // Change visibility of drop down list here
     theDropDownList.Visible = true;
}

注意:在網格視圖的每一行中將有一個名為DropDownList1 ,並且FindControl()方法為您正在使用的行獲取“正確的控件”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM