selected value databinding for DropDownBox in gridview is Disabled

G

Guest

Hi,

I am trying to bind the selected value of a databound dropdown box in a
databound gridview control to the value being displayed in the template
column the dropdown box has been added to. Both the grid and the dropdown
box are retrieving and displaying data fine, I just cant bind the two
together.

I followed the instructions in the help document called 'Walkthrough:
Displaying a Drop-Down List While Editing in the GridView Web Server Control'
and the behavior I am getting is not as described in the help document.

After setting the ODS for both the gridview and the dropdown list (which was
added in an editItemTemplate), I went in to the DropDownList Tasks menu,
chose Edit DataBindings, but when I expected to modify the 'Binding for
Selected Value' options, they were greyed out (disabled). The ONLY available
method was the custom binding expression.

How can I get the 'Binding for Selected Value' options enabled? What am I
missing?

OR, what would be the Custom binding expression I should use to make it work.

Below is the code I am using. The control is the ddCountryRegion.

<asp:GridView ID="gvData" runat="server" CssClass="main-result-grid"
AutoGenerateColumns="False" DataSourceID="odsData">
<Columns>
<asp:BoundField DataField="CountryCode"
HeaderText="CountryCode" />
<asp:BoundField DataField="CountryName"
HeaderText="CountryName" />
<asp:BoundField DataField="BaseCurrency"
HeaderText="BaseCurrency" />
<asp:TemplateField HeaderText="CountryRegion">
<EditItemTemplate>
<asp:DropDownList ID="ddCountryRegion"
runat="server" DataSourceID="odsRegionCol" DataTextField="key"
DataValueField="value">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCountryRegion"
runat="server" Text='<%# Bind("CountryRegion") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="WonCountry"
HeaderText="WonCountry" />
<asp:BoundField DataField="PrimaryIndex"
HeaderText="PrimaryIndex" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate>
No Country Information has been found.
</EmptyDataTemplate>


</asp:GridView>


Thanks in advance for your help,
Dean
 
S

Steven Cheng[MSFT]

Hi Dean,

Thank you for posting.

From your description, I understand you're buiding an data display page
through ASP.NET 2.0. The page contains one Gridview control which has a
template column with a dropdownlist in the editTemplate. However, you're
encountering some problem on set the dropdownlist's databinding property ,
correct?

Based on the aspx control template you provided, I think it is a quite
typical case of inner control databinding and in the VS IDE's design-time
interface, you should be able to edit the databinding field(SelectedValue
property) of the dropdownlist interactively... Anyway, here is a simple
Gridview's template I used in my local test project(which uses the
NorthWind database's Products/Categories tables):

=====================================
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
................................
<asp:TemplateField HeaderText="CategoryID"
SortExpression="CategoryID">
<EditItemTemplate>
&nbsp;<asp:DropDownList ID="DropDownList1"
runat="server" DataSourceID="SqlDataSource2"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
&nbsp;<asp:DropDownList ID="DropDownList2"
runat="server" DataSourceID="SqlDataSource2"
DataTextField="CategoryName"
DataValueField="CategoryID" Enabled="False" SelectedValue='<%#
Bind("CategoryID") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
......................................
</Columns>
</asp:GridView>
================================

As you can see, the generated databinding expression for the dropdownlist
in the edittemplate is simply a

SelectedValue='<%# Bind("CategoryID") %>'

statement.

In addition, for customization on databinding of the nested controls in
GridView, you can also consider using the GridView's "RowDataBound" event.
We can find the certain control(dropdownlist) in the EditTemplate and
adjust its properties.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Joined
Jan 22, 2009
Messages
2
Reaction score
0
I know this is an old post, by 1 up to Steven.

Been looking for an answer to this.

I was attempting to do it like

Code:
<asp:DropDownList ID="PRIORITY" runat="server" CausesValidation="true" >
                            <asp:ListItem SelectedValue='<%# Bind("PRIORITY") %>'></asp:ListItem>
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                        </asp:DropDownList>
When It should have been

Code:
<asp:DropDownList ID="PRIORITY" runat="server" CausesValidation="true" SelectedValue='<%# Bind("PRIORITY") %>'>
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                        </asp:DropDownList>
 
Joined
Mar 15, 2010
Messages
1
Reaction score
0
Hi Steven,

I followed the example code you gave in response to Dean's problem (which is exactly what I'm experiencing and added SelectedValue='<%# Bind("current_status") %>'> to my code.

Unfortunately I get the error 'code blocks not supported in this context'.

Please help.

Many thanks in advance
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top