Solved

Binding Gridview to Array - Issues With Headers and Columns

Posted on 2014-04-18
12
684 Views
Last Modified: 2014-04-22
I have a project that when a payment is submitted, I want to display a gridview with column headers, and one row of transaction information coming from an array.

What's happening is that if I comment out the columns and header text of the gridview, I get the data in one one column with the header named ITEM.

But if I leave the columns and headers in I get the following error on the Bind statement:

A field or property with the name 'PolNum' was not found on the selected data source.
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Thanks in advance,

JB

VB.Net Code

        Dim arr1D As String() = {PolNum, MOD, CoInd, AcctBal, PaidToDate, History, Date}
        PRGrid.DataSource = arr1D
        PRGrid.DataBind()

        Me.PRGrid.Visible = True

ASP.Net Code:

                <asp:GridView ID="PRGrid" runat="server" Width="100%" AutoGenerateColumns="True" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CellPadding="2" HorizontalAlign="Justify">
                  <Columns>
                      <asp:BoundField DataField="PolNum" HeaderText="POLICY" />
                      <asp:BoundField DataField="MOD" HeaderText="MOD" >
                            <ItemStyle HorizontalAlign="Left" Wrap="False" /></asp:BoundField>
                      <asp:BoundField DataField="CoInd" HeaderText="CMPY_IND" />
                      <asp:BoundField DataField="AcctBal" HeaderText="ACC_BAL" />
                      <asp:BoundField DataField="PaidToDate" HeaderText="PAID_TO_DATE" />
                      <asp:BoundField DataField="History" HeaderText="TRANSACTION DATA" >
                          <ItemStyle HorizontalAlign="Left" /></asp:BoundField>
                      <asp:BoundField DataField="arrDate" HeaderText="TRAN_DATE_TIME" />
                  </Columns>
                  <PagerStyle ForeColor="Blue" />
                  <HeaderStyle BackColor="#C0C0FF" BorderColor="Black" />
              </asp:GridView>
0
Comment
Question by:JB4375
  • 6
  • 5
12 Comments
 
LVL 12

Expert Comment

by:Jitendra Patil
ID: 40010036
the problem here is you are binding columns using a binding source and you are explicitly passing the columns names to gridview.

try the below link which will be help ful to you
Binding Arrays to GridView in ASP.Net
hope this helps.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 40010189
You'll make life a lot easier for yourself (and your code cleaner) if you create a class to represent your transaction data, and then use that to bind to your grid.
0
 
LVL 1

Author Comment

by:JB4375
ID: 40012661
@Carl - I'm all ears because I can't seem to get a multidimensional array to bind at all.

Can you give me an example of what I should be doing or direct me to a link?

I've seen several that are a simple list but none where multiple values are involved, and again I would like them display on one row.

Thanks,

JB
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40012834
First you'd create a class, something like (i've just guessed at data types, and expanded a couple of property names because MOD and Date are reserved words):
Public Class Payment

    Public Property PolNum As String
    Public Property MODValue As String
    Public Property CoInd As String
    Public Property AcctBal As Decimal
    Public Property PaidToDate As Decimal
    Public Property History As String
    Public Property PaymentDate As DateTime

End Class

Open in new window

The you can create a List<> , populate it, and bind it to the grid:
        Dim list As New List(Of Payment) From
        {
            New Payment With {.PolNum = "PolNum", .MODValue = "MODValue", .CoInd = "CoInd", .AcctBal = 100D, .PaidToDate = 1000D, .History = "History", .PaymentDate = Date.Today}
        }

        GridView1.DataSource = list
        GridView1.DataBind()

Open in new window

0
 
LVL 1

Author Comment

by:JB4375
ID: 40013065
I'm sorry..... this looks good but I'm at a loss. I already have variables assigned that are correct values and type, but I still need to create a public class and more variables?

On the list creation piece:
.Polnum is the variable, and the "Polnum is the column name, correct?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 40013075
.PolNum is the name of a property on the class. You can instantiate the class from vairables (and you might want to change the names of the properties to something more meaningful).

The advantage is that the grid binds more easily to a class that it does to a raw array.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:JB4375
ID: 40013186
OK.  

List(Of Payment) From

Generating the following error: Type 'List' not defined.  Type 'Payment' not defined.

Error Correction Options:
1. Change 'List(Of Payment) to 'Generic.List(Of Payment)
2. Change 'List(Of Payment) to 'ListBox(Of Payment)
3. Generate 'Class List(Of T)'

None of which seem to resolve anything.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 40013205
You'll either need to add the following line to the top of your code file:
Imports System.Collections.Generic

Open in new window

Or, expand the declaration to:
Dim list As New System.Collections.Generic.List(Of Payment) From ...

Open in new window

0
 
LVL 1

Author Comment

by:JB4375
ID: 40013242
That took care of the list error. Still getting Type 'Payment' is not defined.

Should I generate 'Class Payment'?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 40013245
yes, you'll need to add the Payment class from my earlier post into your project somewhere.
0
 
LVL 1

Author Comment

by:JB4375
ID: 40013450
OK.... That works. I had an issue with the trailing zeros being stripped off. Added a ToString fix before sending the values to the GridList.

Final Solution:

ASP.Net
                <asp:GridView ID="GridList" runat="server" Width="100%" AutoGenerateColumns="True" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CellPadding="2" HorizontalAlign="Justify">
                  <PagerStyle ForeColor="Blue" />
                  <HeaderStyle BackColor="#C0C0FF" BorderColor="Black" />
              </asp:GridView>

VB.Net

Public Class Payment
    Public Property POLICY As String
    Public Property MOD_NUM As String
    Public Property COMPY_IND As String
    Public Property ACCT_BAL As Decimal
    Public Property PAID_TO_DATE As Decimal
    Public Property OVERAGE_AMOUNT As Decimal
    Public Property PAYMENT_COMMENTS As String
    Public Property TRANS_DATE As String
End Class

        Dim list As New List(Of Payment) From
            {
                New Payment With {.POLICY = updPolNumTextbox, .MOD_NUM = updMODTextbox, .COMPY_IND = updSelectedCoIndTextbox, .ACCT_BAL = updSelectedAcctBal.ToString("#.00"), .PAID_TO_DATE = updSelectedPaidToDate.ToString("#.00"), .OVERAGE_AMOUNT = updOverage.ToString("#.00"), .PAYMENT_COMMENTS = updHistory, .TRANS_DATE = updDate}
            }
        GridList.DataSource = list
        GridList.DataBind()
0
 
LVL 1

Author Closing Comment

by:JB4375
ID: 40014959
Thanks for pointing me in the right direction and sticking with me, Carl. Very Helpful!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now