Solved

Drop List in Gridview

Posted on 2008-10-12
7
380 Views
Last Modified: 2013-11-07
Hi Experts

I am currently creating a web app for a sports club, I have a Gridview containing a list of club honours. One of the fields is the season the honour was achieved, the Gridview has buttons to edit the individual rows.

The problem I am having is that I need, on edit to apply a drop list to the season field so this value can be changed, which will contain a list of years ranging from 1960 to current year. I have applied this drop list correctly to the form and am fine with generating the values.

I am just not to sure how to achieve this in the Gridview, which event of the GV do I use...?
How do I get the correct row index that is being edited?

The code I am using to generate the Drop List on the form is

Dim currentSeason As Date = Now()
        Dim currentSeasonYear As Integer
        currentSeasonYear = currentSeason.Year

        For i As Integer = currentSeasonYear To 1970 Step -1
            drpHonoursYear.Items.Add(i)
        Next

In summary I need to be able to apply this code to  a drop list in a Grid View when a row is edited

Regards


0
Comment
Question by:mattpotts1974
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22698160
you can apply the grid view change for the combo box on the grid view row editing event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(VS.80).aspx

you must be able to identify the record - cell and then apply the modifications you need to apply i.e. change the cell to drop down list
0
 

Author Comment

by:mattpotts1974
ID: 22698439
Thanks for the prompt reply

I am still a bit confused though, do I add an editItemTemplate field in the asp.net page on the gridview and set it to a dropdownlist with an ID and the on the rowediting event handler dynamically create the list values..?

Or do I leave the GV field without a editItemTemplate and create the drop down list in the vb code..?

If so do I need to cast from a table cell to  a drop down list, can this be done..?

Any code sample would be helpful

Regards
0
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 22698518
What I suggest to do it is the following:
1. Use an objectdatasource to get the data of the dropdownlist
2. On the EditItemTemplate, add a dropdownlist and bind it to the objectdatasource
3. add the tag: SelectedValue='<%# Bind("") %>' to your dropdownlist

Here is a sample:

<asp:ObjectDataSource ID="odsRole" runat="server" SelectMethod="GetRoles" TypeName="RolesController">
</asp:ObjectDataSource>
 <asp:GridView ID="grvUser" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" DataSourceID="odsUser" DataKeyNames="UserName" meta:resourcekey="grvUserResource1">
                <Columns>
                    <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" ReadOnly="True" meta:resourcekey="BoundFieldResource1"/>
                    <asp:TemplateField HeaderText="Role" SortExpression="RoleName" meta:resourcekey="TemplateFieldResource1">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlRole" runat="server" AppendDataBoundItems="True" DataSourceID="odsRole" DataTextField="RoleName" SelectedValue='<%# Bind("RoleName") %>' meta:resourcekey="ddlRoleResource2">
                                <asp:ListItem Text="-- Select --" meta:resourcekey="ListItemResource2" Selected="True" />
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="rfvRole" runat="server" ControlToValidate="ddlRole"
                                Display="Dynamic" ErrorMessage="Required Role" ValidationGroup="UpdateUser" meta:resourcekey="rfvRoleResource2">*</asp:RequiredFieldValidator>                            
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblRoleName" runat="server" Text='<%# Eval("RoleName") %>' meta:resourcekey="lblRoleNameResource1" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ModifiedDate" ReadOnly="true" meta:resourcekey="ModifiedDateField" SortExpression="ModifiedDate" />
                    <asp:CommandField ShowEditButton="True" ValidationGroup="UpdateUser" meta:resourcekey="CommandFieldResource1"/>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" CausesValidation="false" meta:resourcekey="btnDelete"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Open in new window

0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

Author Comment

by:mattpotts1974
ID: 22701688
Thanks for the response

Using the objectdatasource seems a good way to go with it....
I have never used this before, is it just a case of creating a class with a function in it..?

If so, in the function do I declare a dropdownlist object, and then pass values to the object i.e. dropdownlist1.items.add(i)

Or do I just create a function that generates the values to an array then pass them out of the function..?

Any code samples would be appreciated

Regards
0
 
LVL 11

Expert Comment

by:Salim Fayad
ID: 22702082
From where are you getting your values? From the database, code, ...?
It doesn't have to be ObjectDataSource, you can use SqlDataSource if you are using SQL Server where you can pass the stored procedure to the SqlDataSource.
As for the DropDownList, all you have to set its DataSourceId to the ID of the datasource (objectdatasource or sqldatasrouce)
0
 

Author Comment

by:mattpotts1974
ID: 22702510
The values are being generated using a for loop, this is the code I have used in another area of the form to generate the same thing

Dim currentSeason As Date = Now()
        Dim currentSeasonYear As Integer
        currentSeasonYear = currentSeason.Year

        For i As Integer = currentSeasonYear To 1970 Step -1
            drpHonoursYear.Items.Add(i)
        Next

I am just wondering in the objectdatasource do I need to be declaring the dropdownlist..?
0
 
LVL 11

Accepted Solution

by:
Salim Fayad earned 500 total points
ID: 22709475
Create a class and put this code in a function inside this class. Select this class in the objectdatasource as the business object, and then set the "SelectMethod" as the function name.
As for declaring the dropdownlist inside the objectdatasource, you cannot. Both controls are seperate controls
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
make control visible based off other control value 7 44
Import a excel sheet in a grid 2 44
asp.net mvc5 6 33
Convert a string to date 4 40
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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