Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGridViewComboBoxColumn in ASP.net 2.0

Posted on 2011-09-19
6
Medium Priority
?
256 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

824 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