如何将网格视图中的单元格设置为下拉菜单

发布于 2024-11-27 22:16:33 字数 1958 浏览 1 评论 0原文

如何将网格中的单个单元格设置为下拉菜单。我有一个可编辑的网格,其中所有行都是可编辑的,除了单独的两行应该可以从下拉列表中选择。我们怎样才能实现它呢?我什至无法开始如何进行。有什么想法吗?

<asp:GridView AutoGenerateColumns="false" PageSize="300" ID="gvService"
                            runat="server" GridLines="None" BorderWidth="1" BorderColor="Brown" AlternatingRowStyle-BackColor="Cyan"
                            HeaderStyle-BackColor="ActiveCaption" ShowFooter="true">
                           <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Classic</HeaderTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblClassic" runat="server" Text='<%# Eval("Classic") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        ABC</HeaderTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblA" Visible='<%# ! IsInEditMode %>' runat="server" Text='<%# Eval("ABC") %>' />
                                        <asp:TextBox ID="txtA" Visible='<%# IsInEditMode %>' runat="server" Text='<%#Eval("ABC")%>'
                                            MaxLength="3" Columns="3">
                                        </asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>    


How to make a single cell in a grid to a dropdown. I have a editable grid in which all the rows are editable except two rows alone should be selectable from a dropdown. How can we achieve it? I could not start even how to proceed. Any ideas?

<asp:GridView AutoGenerateColumns="false" PageSize="300" ID="gvService"
                            runat="server" GridLines="None" BorderWidth="1" BorderColor="Brown" AlternatingRowStyle-BackColor="Cyan"
                            HeaderStyle-BackColor="ActiveCaption" ShowFooter="true">
                           <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Classic</HeaderTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblClassic" runat="server" Text='<%# Eval("Classic") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        ABC</HeaderTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblA" Visible='<%# ! IsInEditMode %>' runat="server" Text='<%# Eval("ABC") %>' />
                                        <asp:TextBox ID="txtA" Visible='<%# IsInEditMode %>' runat="server" Text='<%#Eval("ABC")%>'
                                            MaxLength="3" Columns="3">
                                        </asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2024-12-04 22:16:33

准确理解您的要求有点困难,但您可以添加另一个带有下拉列表的字段,例如:

<asp:TemplateField>
    <HeaderTemplate>
        Classic</HeaderTemplate>
    <ItemStyle HorizontalAlign="Center" />
    <ItemTemplate>
        <asp:Label ID="lblClassic" runat="server" Text='<%# Eval("Classic") %>' />
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <HeaderTemplate>
        ABC</HeaderTemplate>
    <ItemStyle HorizontalAlign="Center" />
    <ItemTemplate>
        <asp:Label ID="lblA" Visible='<%# ! IsInEditMode %>' runat="server" Text='<%# Eval("ABC") %>' />
        <asp:TextBox ID="txtA" Visible='<%# IsInEditMode %>' runat="server" Text='<%#Eval("ABC")%>'
            MaxLength="3" Columns="3">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Header="header">
    <EditItemTemplate>
        <asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="ddllbl" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

如果您想显式地将数据绑定到它,您可以这样做:

// In your aspx page
<asp:DropDownList ID="yourDDL" runat="server" DataTextField="yourTextFieldName" DataValueField="yourValueFieldName" OnDataBinding="yourddl_DataBinding">
</asp:DropDownList>

// In your codebehind .cs file
protected void yourddl_DataBinding(object sender, System.EventArgs e)
{
    DropDownList ddl = (DropDownList)(sender);
    // This could be a List of objects, DataTable, DataSet, whatever
    ddl.DataSource = GetCachedData();  
    ddl.DataBind();
}

It's a little difficult to understand exactly what you're asking for, but you can just add another field with a dropdown in it like:

<asp:TemplateField>
    <HeaderTemplate>
        Classic</HeaderTemplate>
    <ItemStyle HorizontalAlign="Center" />
    <ItemTemplate>
        <asp:Label ID="lblClassic" runat="server" Text='<%# Eval("Classic") %>' />
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <HeaderTemplate>
        ABC</HeaderTemplate>
    <ItemStyle HorizontalAlign="Center" />
    <ItemTemplate>
        <asp:Label ID="lblA" Visible='<%# ! IsInEditMode %>' runat="server" Text='<%# Eval("ABC") %>' />
        <asp:TextBox ID="txtA" Visible='<%# IsInEditMode %>' runat="server" Text='<%#Eval("ABC")%>'
            MaxLength="3" Columns="3">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Header="header">
    <EditItemTemplate>
        <asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="ddllbl" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

And if you want to bind data to it explicitly you can do:

// In your aspx page
<asp:DropDownList ID="yourDDL" runat="server" DataTextField="yourTextFieldName" DataValueField="yourValueFieldName" OnDataBinding="yourddl_DataBinding">
</asp:DropDownList>

// In your codebehind .cs file
protected void yourddl_DataBinding(object sender, System.EventArgs e)
{
    DropDownList ddl = (DropDownList)(sender);
    // This could be a List of objects, DataTable, DataSet, whatever
    ddl.DataSource = GetCachedData();  
    ddl.DataBind();
}
软糯酥胸 2024-12-04 22:16:33

快速答案是...在gridview上编辑您的模板..它应该有一个下拉菜单..在该下拉菜单中您会找到“编辑项目模板”,您可以在那里粘贴您想要的任何控件..并且在视图中它应该只是文本或文本框,无论您使用什么。

有关模板字段的更多信息.. http://msdn.microsoft.com/en -us/library/aa479353.aspx

quick answer is... on the gridview edit your templates.. it should have a drop down.. in that drop down you will find "edit item template" you would stick whatever control you want there.. and in the view it should be just text or a textbox whatever you with.

little mor einfo on template fields.. http://msdn.microsoft.com/en-us/library/aa479353.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文