I have an array of 2d (nxn) strings in C #, how can I get it dynamically on a web page (Tried DataTables / Binding etc.)

I have an array of n by n String that needs to be displayed on a web page, I found several solutions that require many (read: many) lines of code (usually converting it to a DataTable and then binding to a GridView). And almost all of these solutions will not even work for the dynamic nature of my arrays (I don’t know in advance how many columns and rows will be created, as well as column names: it is controlled by the user).

I find this for all these solutions somewhat ridiculous, and in some cases larger than my entire module that I have already developed, just to output a small part of my data ...

This is an example of what I was trying to do:

object1 = (string[,]) r.GetSymbol("stringArray"); //I retrieve nxn array and cast as a string contained in an object (have to do this because I am using COM interfaces). Output_GridView.DataSource = object1; //(If I try to convert this to a string, it returns the dataType "string" not the 2d array Output_GridView.DataBind(); 

This does not work (this will require the 1st array according to the error received, I don’t know why the DataSource / GridView will be limited this way), I read some very ugly solutions, but in fact all I need is just write a nested for loop to output n columns and n rows to an ASP.NET page. Can someone help me here (why such a trivial task should be so complicated?)

Thanks for any feedback =)

-Dave

+1
source share
3 answers

Have you considered just creating a custom web control. I created a quick one that takes an array of [,] and just prints the contents of the array into a div with p around each value of the array. Its simple, easy and you will have full control over the exit.

Below is the step you need to complete:

Add a new project to your web application and make sure you link to System.web. Perhaps call the WebControls project.

Add the following C # code to a new class file that you can add to the project.

CUSTOM CONTROL CODE:

 using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebControls; namespace WebControls { [ToolboxData("<{0}:ArrayDisplayControl runat=server></{0}:ArrayDisplayControl>")] public class ArrayDisplayControl : WebControl { protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } public string[,] DataSource { get { return (string[,])ViewState["DataSource"]; } set { ViewState["DataSource"] = value; } } protected override void RenderContents(HtmlTextWriter output) { output.WriteBeginTag("div"); for (int i = 0; i < DataSource.GetLength(0); i++) { for (int j = 0; j < DataSource.GetLength(1); j++) { output.WriteFullBeginTag("p"); output.Write(DataSource[i, j]); output.WriteEndTag("p"); } } output.WriteEndTag("div"); } } } 

Now you only need to update your recently added project in your web application. properties β†’ add link - select projects, and then the name of the new project.

Well, all that’s left is to add an ad at the top of the asp.net page so that you can link to the custom control as follows:

 <%@ Register Assembly="WebControls" Namespace="WebControls" TagPrefix="custom" %> 

Now reference the control in html, as shown below:

 <custom:ArrayDisplayControl ID="ctlArrayDisplay" runat="server" /> 

So the last step is to bind the control to some data in the code behind - something like the following:

 protected void Page_Load(object sender, EventArgs e) { string[,] data = new string[2, 2] { { "Mike", "Amy" }, { "Mary", "Albert" } }; ctlArrayDisplay.DataSource = data; ctlArrayDisplay.DataBind(); } 

And here is the result after launch:

 <div id="ctlArrayDisplay"> <div><p>Mike</p><p>Amy</p><p>Mary</p><p>Albert</p></div> </div> 

Hope this helps you get rid of your jam.

Enjoy it!

+3
source

not really, but should give you a start

  //setup the data var random = new Random(); int x = random.Next(14) + 1; int y = random.Next(29) + 1; var data = new int[x, y]; for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) data[i, j] = random.Next(100); //create the data table var table = new Table(); for (int i = 0; i < x; i++) { var newRow = new TableRow(); for (int j = 0; j < y; j++) { var newCell = new TableCell(); newCell.Text = data[i,j].ToString(); newRow.Cells.Add(newCell); } table.Rows.Add(newRow); } ux_Panel.Controls.Add(table); 

Shlomo looks like the best solution if you do not want to massage the data a little

+2
source

Despite the ugliness, ASP Code Nuggets in .aspx will work:

 <table> <% for(int i = 0; i < ArrayDimensionLength; i++) { %> <tr> <td>RowHeader</td> <% for(int j = 0; j < ArrayDimensionLength; j++) { %> <td><%= Array[i,j] %> </td> <% } %> </tr> <% } %> </table> 

... and in code:

  protected string[,] MyArray { get { //Insert your array here return new string[,]{{"1", "1"}, {"2", "2"}}; } } protected int ArrayDimensionLength { get { return (int)Math.Sqrt(MyArray.Length); } } 
+1
source

All Articles