Displaying 2D array on my aspx page

Posted on 2006-05-16
Last Modified: 2012-06-21
Good evening Experts

Writing an application in C#. Working with a 2D array, that I'm populating with data,
doing calculations and runnings totals on.

How do I display my 2D array on the html page?
Tried binding it to a datagrid, but it doesn't like 2D.
Tried adding to a viewstate and a session without much

Any ideas?

Kind regards
Question by:Firewater
    LVL 37

    Expert Comment

    create a datatable with columns to match your array
    loop through the array and add the items to the datatable

    ...also...i've done this same thing in the past...because i'm used to working with arrays i did the same thing you're doing...but...depending on the can work with a datatable directly and do your math and calcs to the datatable columns/rows just as you would an array...then bind the datatable directly to a data control

    Author Comment

    Cool, thanks for that.
    Did the following:

    DataTable dtData = new DataTable("DataTable");

    for(i = loop till end)
         DataRow row = dtData.NewRow();
         row["Col1"] = summaryArr[i,1];
         row["Col2"] = summaryArr[i,2];


    DataSet ds = new DataSet();
    dataGrid.DataSource = ds;

    Unfortunately, no data is posting back to the <asp:DataGrid> yet.
    Any ideas? Did I miss out a step?

    Kind regards
    LVL 37

    Expert Comment

    ...on pda at moment...try bind to dt instead of ds...don't think you even need the ds
    dataGrid.DataSource = dt.defaultview;

    Author Comment

    Hi samtran

    Did try skipping the ds and straight to the dt.
    The page completes, without any errors, but no
    data is being displayed. I did query my datatable,
    and the data is there.

    This is what my grid's code looks like on the aspx page:
    <form runat="server">
    <asp:DataGrid ID="dataGrid" runat="server" ShowHeader="true"></asp:DataGrid>

    Kind regards
    LVL 37

    Expert Comment

    ...i got a chunk of code where i'm basically doing the exact same thing...but it's at home and I won't be able to get to it till evening...but we're missing something really simple...(if I had my code chunk, I could tell you exactly what it is!)

    so if you can wait ...we can knock  this out later...I want to say that I needed an actual dataview object....
    dataview dv = new dataview;
    dv = dt.defaultview
    then bind to dv...

    can't remember why i did that though....i think i had it in a function and the dv was getting returned to the page...


    Author Comment

    Hi samtram

    That would be appreciated.

    I agree with you, its something simple.
    Viewstate possibly, or along those lines.

    I'll work on it again tomorrow evening.

    Thanks for the pointers so far.
    LVL 37

    Accepted Solution

    no prob Firewater...
    I've had a few financial clients over the years and used arrays heavily for calcs...but since .Net...I've found datatables really easy to work with and capable of handling the same things that I used md arrays for...if you use arrays a lot just for calcs between rows and columns...spreadsheet-like stuff....look further into datatables...

    doing this in a loop
    ar[i,2] = ar[i-1] + 5

    isn't different than
    dt.rows(i)(2) = dt.rows(i-1) + 5

    ...the above is a horrible example and syntax is off...just making a point that it is possible to skip the array step all together using a dt...

    Author Comment

    Hi samtran

    Got it working. Thanks for the DataTable info.
    It does work well.

    The problem was NOT with the C# stuff, but
    a table, that was pushing my datagrid out of
    the page, and it didn't produce a horizontal scroller.

    Appreciate it.
    LVL 37

    Expert Comment

    interesting...were you using a <div overflow .../> or something around the dg?
    glad you got it working though!

    Expert Comment


    Is there any example you could post in VB?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 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

    10 Experts available now in Live!

    Get 1:1 Help Now