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
Solved

Binding Gridview to Array - Issues With Headers and Columns

Posted on 2014-04-18
12
722 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

790 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