Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 901
  • Last Modified:

Binding Gridview to Array - Issues With Headers and Columns

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
JB4375
Asked:
JB4375
  • 6
  • 5
5 Solutions
 
Jitendra PatilSr.Software EngineerCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JB4375Author Commented:
@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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JB4375Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
.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
 
JB4375Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
JB4375Author Commented:
That took care of the list error. Still getting Type 'Payment' is not defined.

Should I generate 'Class Payment'?
0
 
Carl TawnSystems and Integration DeveloperCommented:
yes, you'll need to add the Payment class from my earlier post into your project somewhere.
0
 
JB4375Author Commented:
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
 
JB4375Author Commented:
Thanks for pointing me in the right direction and sticking with me, Carl. Very Helpful!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now