Solved

Binding Gridview to Array - Issues With Headers and Columns

Posted on 2014-04-18
12
759 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
[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
  • 6
  • 5
12 Comments
 
LVL 13

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
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…

726 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