Solved

DataGridViewComboBoxColumn in ASP.net 2.0

Posted on 2011-09-19
6
245 Views
Last Modified: 2012-05-12
I am trying to convert a vb2005 windows form based project to ASP.net 2.0.
My main problem has been converting gridviewrows into datagrids.
What's the best way to have a grid with drop down boxes in the columns?  I don't have AJAX at the moment.
 
0
Comment
Question by:AlHal2
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36561914
Instead of doing with dropdowns in each Row, display label with text and place a button beside to the label. if we click on button beside to the label display a popup where user can have dropdown after closing dropdown assign selected value to label.

or else

refer these links

http://webthingsconsidered.blogspot.com/2005/09/dropdownlist-inside-gridview-or.html

http://weblogs.asp.net/vikram/archive/2008/04/17/handling-dropdown-list-inside-gridview-with-autopostback.aspx
0
 

Author Comment

by:AlHal2
ID: 36565760
I converted the code to vb.net.  The result is below.  Only the label is visible when I run the source code.
ASPX

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication2._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <asp:GridView ID="gvStates" AutoGenerateColumns="false" runat="server" OnRowCreated="gvStates_RowCreated">  
    <Columns>    
    <asp:BoundField HeaderText="State" DataField="Name" />    
    <asp:TemplateField HeaderText="Cities">      
    <ItemTemplate> <asp:DropDownList ID="ddlCities" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlCities_SelectedIndexChanged">        
    </asp:DropDownList>
    </ItemTemplate>    
    </asp:TemplateField>  
    </Columns>
    </asp:GridView>
    <asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>

    
    </form>
</body>
</html>

vb 

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            ' Create states array and bind it to Grid       
            Dim states As New ArrayList()
            Dim cities As String() = New String() {"Portland", "Salem", "Eugene"}
            Dim state As New State("OR", cities)
            states.Add(state)
            cities = New String() {"Seattle", "Tacoma", "Olympia"}
            state = New State("WA", cities)
            states.Add(state)
            Me.gvStates.DataSource = states
            Me.gvStates.DataBind()
        End If
    End Sub
    Protected Sub gvStates_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If Not IsPostBack Then
            If e.Row.RowType = DataControlRowType.DataRow Then
                ' Bind drop down to cities           
                Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddlCities"), DropDownList)
                ddl.DataSource = DirectCast(e.Row.DataItem, State).Cities
                ddl.DataBind()
            End If
        End If
    End Sub
    Protected Sub ddlCities_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Me.lblCity.Text = DirectCast(sender, DropDownList).SelectedValue
    End Sub


End Class
Public Class State
    Private _Name As String
    Private _Cities As String()

    Public Sub New(ByVal name As String, ByVal cities As String())
        _Name = name
        _Cities = cities
    End Sub

    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property

    Public ReadOnly Property Cities() As String()
        Get
            Return _Cities
        End Get
    End Property
End Class

Open in new window

0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36568586
Change Page load code as like below

 If Not IsPostBack Then
            ' Create states array and bind it to Grid      
            Dim states As New List(Of State)
            Dim cities As String() = New String() {"Portland", "Salem", "Eugene"}
            Dim state As New State()
      state.Name = "OR"
      state.Cities = cities
            states.Add(state)
            cities = New String() {"Seattle", "Tacoma", "Olympia"}
            state = New State("WA", cities)
      state.Name = "WA"
      state.Cities = cities
            states.Add(state)
            Me.gvStates.DataSource = states
            Me.gvStates.DataBind()
        End If

Add below namespace
imorts System.Collections.Generic
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:AlHal2
ID: 36572170
These lines come up with errors as name and city properties are read only.
state.Name = "OR"
state.Cities = cities
0
 
LVL 12

Accepted Solution

by:
jagssidurala earned 250 total points
ID: 36575778
Change ur properties to SET and GET methods, it should work
0
 

Author Closing Comment

by:AlHal2
ID: 36579089
thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now