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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

Displaying 2D array on my aspx page

Good evening Experts

Writing an asp.net 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
  • 5
  • 4
1 Solution
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 situation...you 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
FirewaterAuthor Commented:
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
...on pda at moment...try bind to dt instead of ds...don't think you even need the ds
dataGrid.DataSource = dt.defaultview;
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!

FirewaterAuthor Commented:
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
...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...

FirewaterAuthor Commented:
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.
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...
FirewaterAuthor Commented:
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.
interesting...were you using a <div overflow .../> or something around the dg?
glad you got it working though!

Is there any example you could post in VB?


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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