Solved

DataGridViewComboBoxColumn in ASP.net 2.0

Posted on 2011-09-19
6
250 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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