Solved

Binding Gridview to Array - Issues With Headers and Columns

Posted on 2014-04-18
12
717 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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