Solved

DataGridViewComboBoxColumn in ASP.net 2.0

Posted on 2011-09-19
6
251 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

626 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