Link to home
Start Free TrialLog in
Avatar of rowternet
rowternet

asked on

Dropdownlists in gridview

Hi all,
 
 
 
I am tryign to show a gridview with multiple dropdownists in it.
 
For example here, client data (firstnm, lastnm, address) are coming from one table in database and  city, state are coming from different tables.
 
city and state are dropdownlists.
 
I can show the grid but when the user enters in to edit mode, i want to show the city dropdown and stat drpdown.
 How can i do this?
 
This is my code so far.
 
Thanks.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DropdownGrid.aspx.vb" Inherits="Site.DropdownGrid" MasterPageFile="~/Site.Master" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
<tr>
<td>
</td>
<td>
    <asp:Button ID="btnView" runat="server" Text="View Grid" />
</td>
</tr>

<tr>
<td>
</td>
<td>

    <asp:GridView ID="gdvSales" runat="server" AutoGenerateColumns="false" >
   
    <Columns>
    <asp:CommandField ButtonType="Button" CancelText="Cancel" EditText="Edit" UpdateText="Update" ShowEditButton="true" ShowCancelButton="true" />
    <asp:BoundField DataField="FirstNm" HeaderText="First Name" />
    <asp:BoundField DataField="LastNm" HeaderText="Last Name" />
    <asp:BoundField DataField="Addr1" HeaderText="Address1" />
    <asp:BoundField DataField="Addr2" HeaderText="Address2" />
    <asp:TemplateField HeaderText="city">
    <ItemTemplate>
        <asp:Label ID="lblCity" runat="server" Text='<%# Eval("CityNm") %>'></asp:Label>    
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList ID="ddlCity" runat="server">
        </asp:DropDownList>
   
    </EditItemTemplate>
     </asp:TemplateField>
         <asp:TemplateField HeaderText="State">
    <ItemTemplate>
        <asp:Label ID="lblstate" runat="server" Text='<%# Eval("StateNm") %>'></asp:Label>
      
    </ItemTemplate>
    <EditItemTemplate>
    
        <asp:DropDownList ID="ddlState" runat="server">
        </asp:DropDownList>
    
    </EditItemTemplate>
    </asp:TemplateField>
  </Columns>
 </asp:GridView>
</td>
</tr>
</table>
</asp:Content>







Public Class DropdownGrid
    Inherits System.Web.UI.Page
    Dim myglobal As New clsGlobal
    Dim mygrid As New myDropdownGrid

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not Page.IsPostBack) Then
        End If
    End Sub

    Protected Sub btnView_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnView.Click
        BindData()
    End Sub

    Private Sub BindData()
        gdvSales.DataSource = mygrid.GetClientData()
        gdvSales.DataBind()
    End Sub

    Protected Sub gdvSales_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvSales.RowDataBound

        If (e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Edit) Then

            Dim ddlcity As DropDownList = DirectCast(e.Row.FindControl("ddlCity"), DropDownList)
            If (Not ddlcity Is Nothing) Then
                ddlcity.DataSource = mygrid.getdropdownValues("tblcity", "CityNm", "CityID")
                ddlcity.DataBind()
                ddlcity.SelectedValue = gdvSales.DataKeys(e.Row.RowIndex).Values(0).ToString
            End If
        End If
    End Sub

    Protected Sub gdvSales_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gdvSales.RowEditing
        gdvSales.EditIndex = e.NewEditIndex
        BindData()
    End Sub
    Protected Sub gdvSales_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gdvSales.RowUpdating

    End Sub

    Protected Sub gdvSales_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gdvSales.RowUpdated

    End Sub

    Protected Sub gdvSales_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gdvSales.RowCancelingEdit
        gdvSales.EditIndex = -1
    End Sub


End Class

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I believe that you are referring to this EditTemplate:

 <EditItemTemplate>
        <asp:DropDownList ID="ddlCity" runat="server">
        </asp:DropDownList>
   
    </EditItemTemplate>

You would need to bind the DropDownList to a different data source, and the bind the SelectedValue to a data element in the main data source.
Avatar of rowternet
rowternet

ASKER

TheLearnedOne:
I can bind the dropdownlist to  a different datasource.  how do i bind he selected value?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America 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