开发者

how to make a cell in a gridview as dropdown

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="serv开发者_JAVA技巧er" Text='<%#Eval("ABC")%>'
                                            MaxLength="3" Columns="3">
                                        </asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>    


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();
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜