Link to home
Start Free TrialLog in
Avatar of leachj
leachj

asked on

Is it possible to use an unbound gridview to collect data entry?

I have a form that will require the entry of about 20 zipcodes with a number total for participants within those zipcodes.  

Zip        Attendees
65212        2
65284        5
65202        10
etc.

I'm thinking an unbound gridview or maybe a table.  I have not been able to figure out how to use the gridview unbound for data entry.  Also not sure if growing a table dynamicly  is the right solution.  I would like the control to scroll so it does not take up page space.  I would like to enter all the zips and numbers before making a trip to the server.  The number of entries will vary widely from as few as 3 zip codes to as many as 30.  I am unsure as to how I can approach this.  Could someone point me in the right direction?

Thanks,
John
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America 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 leachj
leachj

ASKER

I reviewed classes over the weekend, but I am still not clear on your suggestion.  Could you elaborate some?
Thanks
John,

I could give you an example, but you didn't talk about language.

Bob
Avatar of leachj

ASKER

I am using VS2005 and Visual Basic.
Default page:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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>GridView Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="overflow: auto; width: 600px; height: 400px;">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Tahoma"
                Font-Size="Smaller" Width="100%" PageSize="20">
                <HeaderStyle BackColor="SteelBlue" ForeColor="White" />
                <Columns>
                    <asp:BoundField DataField="ZipCode" HeaderText="Zip" />
                    <asp:BoundField DataField="Attendees" HeaderText="Attendees"></asp:BoundField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>
Default code-behind:

Imports System.Collections.Generic

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Me.LoadGridData()
        End If
    End Sub

    Private Sub LoadGridData()

        Dim list As New List(Of Participants)
        list.Add(New Participants(65212, 2))
        list.Add(New Participants(65284, 5))
        list.Add(New Participants(65202, 10))

        Me.GridView1.DataSource = list
        Me.GridView1.DataBind()

    End Sub
End Class
Class:

Public Class Participants

    Public Sub New(ByVal zipCode As Integer, ByVal attendees As Integer)
        m_zipCode = zipCode
        m_attendees = attendees
    End Sub

    Private m_zipCode As Integer
    Public Property ZipCode() As Integer
        Get
            Return m_zipCode
        End Get
        Set(ByVal value As Integer)
            m_zipCode = value
        End Set
    End Property

    Private m_attendees As Integer
    Public Property Attendees() As Integer
        Get
            Return m_attendees
        End Get
        Set(ByVal value As Integer)
            m_attendees = value
        End Set
    End Property

End Class

Bob
Avatar of leachj

ASKER

I get an error here...

Dim list As New List(Of Participants)

Type 'List' is not defined.

Do I need to add List property to the class?
Avatar of leachj

ASKER

Never mind, I left out
Imports System.Collections.Generic
Avatar of leachj

ASKER

I have gotten this to work, thanks.  I will award points.  I have a question though regarding the sort property of the gridview.  It does not seem to be available to me.  I have set the allowsorting property of the gridview but it is not enabled.  Is it related to the way in which the control was created?

John
The AllowSorting only works if you are bound to an SqlDataSource.  If you aren't then you need to handle the sorting event yourself.

Bob