C# 使用下拉菜单创建动态栅格

C# 使用下拉菜单创建动态栅格,c#,asp.net,C#,Asp.net,我想创建一个动态gridview,在单击“编辑”按钮时,将第一行作为下拉列表。我不知道如何开始。你能帮忙吗。我阅读了一些文章,发现使用实例化方法我们可以实现 public class CreateItemTemplate : ITemplate { //Field to store the ListItemType value private ListItemType myListItemType; public CreateItemTe

我想创建一个动态gridview,在单击“编辑”按钮时,将第一行作为下拉列表。我不知道如何开始。你能帮忙吗。我阅读了一些文章,发现使用实例化方法我们可以实现

public class CreateItemTemplate : ITemplate
    {
        //Field to store the ListItemType value
        private ListItemType myListItemType;

        public CreateItemTemplate(ListItemType item)
        {
            myListItemType = item;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            //Code to create the ItemTemplate and its field.
            if (myListItemType == ListItemType.Item)
            {
                TextBox txtCashCheque = new TextBox();
                container.Controls.Add(txtCashCheque);
            }
        }
    }

如果要在单个页面上显示此内容,则不应创建服务器控件

使用网格的TemplateField

注意:如果您使用的是AutoGenerateColumns=true,只需将该列添加到网格标记中即可。它将首先添加

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:DropDownList id="someId" runat="server">
        <asp:ListItem Text="One" />
                    <asp:ListItem Text="twO" />
         </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
您可能需要提供有关要执行的操作的更多信息。此下拉列表是否需要默认值?。 根据您的需要,您可以在标记中执行此操作,或者,您可能需要使用网格事件

布瑞恩 更新:添加事件处理程序 如果设置onrowcreated=GridView1\u在网格中创建的行

    <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="true" 
        onrowcreated="GridView1_RowCreated">
并在代码隐藏中执行此操作:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var dropdown = e.Row.FindControl("someId") as DropDownList;
            //dropdown.DataSource= <>; bind it
            //dropdown.SelectedValue =<>"; / set value how you would 
        }
    }
您可以操纵它创建的下拉广告。
如果无法为控件添加fin,请在每个单元格中查找:e.Row.Cells[[index]]。FindControlsomeId

这是针对页面上网格的单个实例,还是您正在创建一个可重用的控件?在这种情况下,整个列将变为右下拉列表?我使用的是AutoGenerateColumns=true。只有该网格才会填充datatable数据。在这种情况下,您将添加一个新列,该列只是一个下拉列表。添加事件处理程序示例