?
Solved

How to set combo box within a gridview

Posted on 2012-09-18
9
Medium Priority
?
713 Views
Last Modified: 2012-10-04
I have the following template code for a gridview.  In this section when they click edit I want to show a combo box with choice "C" or choice "R", however I want it to default to the bound field.   Any ideas, I have a "R" when not in edit mode but a "C" when I go to edit mode.

thanks


<asp:TemplateField HeaderText="Charge Type" HeaderStyle-Width="25px">
                              <EditItemTemplate>
                                    <asp:ComboBox ID="cboChargeType" runat="server" Width="25px" DataTextField='<%# eval("ResidentCharge") %>'  >
                                          <asp:ListItem Text="C" Value="C" ></asp:ListItem>
                                          <asp:ListItem Text="R" Value="R"></asp:ListItem>
                                    </asp:ComboBox>
                                    <%--<asp:TextBox ID="TextBox6" runat="server" Width="25px"  Text='<%# Bind("ResidentCharge") %>'></asp:TextBox>--%>
                              </EditItemTemplate>
                              <ItemTemplate>
                                    <asp:Label ID="Label8" runat="server" Text='<%# Bind("ResidentCharge") %>'></asp:Label>
                              </ItemTemplate>
                              <HeaderStyle Width="25px" />
                        </asp:TemplateField>
0
Comment
Question by:mgmhicks
  • 4
  • 3
  • 2
9 Comments
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38412113
Try changing:
DataTextField='<%# eval("ResidentCharge") %>'
to:
SelectedValue='<%# bind("ResidentCharge") %>'

Alan
0
 

Author Comment

by:mgmhicks
ID: 38412180
Alan I received this error when trying that.

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: 'cboChargeType' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

thanks
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38412200
You sure you are not getting null in

ResidentCharge column?

And is the capitalization proper? of C and R? I'm not sure though capitalization makes a difference.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mgmhicks
ID: 38412202
No, when I use the text box in edit mode, there is a value there.  Just trying to match that value with the text of value in the listbox.

spelling looks good,
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38412205
I expect you are getting a null or empty string from the underlying data, you may need another listitem, so the null or empty strings can be bound. The other option is to update your table data directly, ensuring there are no nulls.

you may get away with adding another static listitem:
<asp:ListItem Text="" Value="'" ></asp:ListItem>

But possibly need to bind the dropdownlist to a SQLDataSource that ensures we have all the values from the underlying table (name unknown)

<asp:dropdownlist ID="ResidentChargeDropDownList" runat="server" DataSourceID="ResidentChargeSqlDataSource" DataTextField="DataTextValue" DataValueField="ResidentCharge" SelectedValue='<%# Bind("ResidentCharge") %>' />

       <asp:SqlDataSource ID="ResidentChargeSqlDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:yourConnectionString %>" 
        SelectCommand="SELECT Distinct [ResidentCharge], [ResidentCharge] as DataTextValue FROM [dbo].[TheTable] Order By [ResidentCharge]"  SelectCommandType="text" />

Open in new window

Alan
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38412210
Just noticed the code

<asp:ComboBox

Is there a control like this in asp.net?
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38412255
AjaxControlsToolkit prefixed as asp, both the dropdownlist and the combobox are almost identical animals. If it works with asp:dropdownlist, it will work with asp:combobox too.

Think it's an appeasement offering from ms, for all the old-school ms access guys who have graduated to asp .net.

Alan
0
 

Author Comment

by:mgmhicks
ID: 38413467
changed the control to dropdownlist, no difference.
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38415413
Hi mgmhicks,
can you run some SQL on the data, to generate a list of all values that can possibly be returned by the table field?

SELECT Distinct [ResidentCharge] FROM [dbo].[TheTable]

For the static items 'C' and 'R' in your combobox to be sufficient, the previous SQL should only return two rows.

Possibly trailing whitespace in the returned field values, particularly if the field type is nchar(x). For example if the field type was nchar(3) the returned values would be 'C   ' and 'R   ' and would not match the listitem values 'C' and 'R', in which case you would encounter the error you posted.

Alan
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question