Link to home
Start Free TrialLog in
Avatar of JamesAStewart
JamesAStewart

asked on

ASP.Net 2.0 Datagridview bound to arraylist - how do I make one of the columns into a dropdownlist

I am using an arraylist to populate a datagridview which is working well however, I would like to edit two of the columns.

I would like to be able to place dropdownlists into columns 2 and 3 (customerRef,ThemePreference) and populate them and make the current selection visible. I can then handle any update and update the underlying profile properties information.

Any advice would be greatlly appreciated.

James
Using ASP.Net 2.0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.DataSource = getProfiles()
        GridView1.DataBind()
    End Sub
 
 
    Public Function getProfiles()
 
        Dim profiles As New ArrayList
 
        Dim userProfiles As ProfileInfoCollection
        userProfiles = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All)
        Dim userInfo As ProfileInfo
        For Each userInfo In userProfiles
            profiles.Add(New userProfileDetails(Profile.GetProfile(userInfo.UserName).UserName, _
            Profile.GetProfile(userInfo.UserName).customerRef, _
            Profile.GetProfile(userInfo.UserName).ThemePreference, _
            Profile.GetProfile(userInfo.UserName).LastActivityDate))
 
        Next
 
        Return profiles
    End Function
 
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" Style="position: relative" AutoGenerateColumns="false" >
            <Columns>
                <asp:BoundField HeaderText="User Name" DataField="UserName"/>
                <asp:BoundField HeaderText="customerRef" DataField="customerRef"/>
                <asp:BoundField HeaderText="ThemePreference" DataField="ThemePreference" />
                <asp:BoundField HeaderText="Last Activity" DataField="LastActivityDate" />
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of avnish_tanna
avnish_tanna
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JamesAStewart
JamesAStewart

ASKER

Thanks for the links.

I now have a DropDownList in the DataGridView. I now need to bind the combo box to the value supplied by the DataGridView. I can get that value with:

<%# eval("customerRef") %>

Do you know how I set the combo box so that it displays the appropriate value?
                <asp:TemplateField HeaderText="The DDL Column">
                    <ItemTemplate>
                        <asp:DropDownList ID="listCustomersDropDownList" runat="server" AutoPostBack="True"
                            DataSourceID="listCustomersObjectDataSource" DataTextField="companyName" DataValueField="companyRef"
                            Style="position: relative">
                        
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window

You have already set DataTextField and DataValueField attributes for DropDownList. These are the attributes to bind it to database.
Use DataTextField="customerRef" which will show customerRef values as visible elements in the dropdownlist. Use DataValueField="customerRef" for underlying values for each of the dropdownlist elements.
I maybe have not explained myself clearly enough.

The combo box is bound correctly, I need it to display (or have selected) the appropriate value (customerRef) for each row in the datagridview.
I've solved the problem with the following code:        

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            DirectCast(e.Row.FindControl("listCustomersDropDownList"), DropDownList).SelectedValue = DataBinder.Eval(e.Row.DataItem, "customerRef")
        End If
    End Sub