How do you copy dropdownlist to GridView

T

tshad

I am trying to copy the Value and Text column of a DropDownList to a
GridView.

With my GriView:

<asp:GridView ID="GridView2" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblJobs" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlBuckets" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblJobIDs" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlBuckets" runat="server">
<asp:ListItem Value="1"
Selected="True">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

If I do:

GridView2.DataSource = ddlJob.Items.Cast<ListItem>().Skip(1).ToArray();
GridView2.DataBind();

It copies the whole list (Value, Text, Enabled, Selected)

What I want to do is copy the Value into lblJobs and the Text into lblJobIDs
and then have the ddlBuckets to show in each row (which it does).

Can I do this automatically or do I need to create the rows manually?

Thanks,

Tom
 
T

tshad

Figured out how to do this.

I changed the 2 labels to have EVALs like so:

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblJobs" Text='<%# Eval("Value") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblJobIDs" Text='<%# Eval("Text") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>

Now it takes the values correctly in the label columns and to get rid of the
extra columns I added AutoGenerateColumns="false".

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top