?
Solved

Dropdownlists in gridview

Posted on 2011-10-29
3
Medium Priority
?
300 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:rowternet
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37053430
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.
0
 

Author Comment

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

Thanks
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 37054386
Here is an example that you can use to figure the syntax for the HTML that you can add to the DropDownList declaration:

SelectedValue='<%# Eval("CurrentState") %>'
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

864 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