Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

DataGridViewComboBoxColumn in ASP.net 2.0

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.
 
Avatar of jagssidurala
jagssidurala
Flag of India image

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
Avatar of AlHal2

ASKER

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

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
Avatar of AlHal2

ASKER

These lines come up with errors as name and city properties are read only.
state.Name = "OR"
state.Cities = cities
ASKER CERTIFIED SOLUTION
Avatar of jagssidurala
jagssidurala
Flag of India 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
Avatar of AlHal2

ASKER

thanks.