我在C#中有一个2d(nxn)字符串数组,如何动态地将其输出到网页(尝试DataTables / Binding等…)

我有一个需要输出到网页的n字符串数组,我发现一些解决方案需要许多(读取:很多)代码行(通常将其转换为DataTable然后绑定到GridView)。 几乎所有这些解决方案都不适用于我的数组的动态特性(我不知道会提前生成多少列和行,也不知道列的名称:它是用户控制的)。

我发现所有这些解决方案都有点荒谬,在某些情况下比我已经设计的整个模块更大,只是输出一小块我的数据……

这是我试图做的一个例子:

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(); 

这不起作用(根据我得到的错误需要1d数组,我不知道为什么DataSource / GridView会受此限制),我已经阅读了一些非常难看的解决方案,但实际上,我只需要编写一个嵌套的for循环来输出n行和n行到ASP.NET页面。 有人可以帮助我吗(为什么这么琐碎的任务必须如此困难?)

感谢您的任何反馈=)

戴夫

您是否考虑过只创建自定义Web控件。 我已经创建了一个接受数组[,]的快速方法,只需在div中输出数组的内容,每个数组值都有p。 它简单,轻便,您将完全控制输出。

以下是您需要实施的步骤:

将新项目添加到Web应用程序,并确保您指向System.web。 也许调用项目WebControls。

将以下C#代码添加到可添加到项目中的新类文件中。

定制控制代码:

 using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebControls; namespace WebControls { [ToolboxData("<{0}:ArrayDisplayControl runat=server>")] 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"); } } } 

现在,您只需将新添加的项目添加到Web应用程序中即可。 properties - > add reference - 选择项目,然后选择新项目的名称。

好的,剩下的就是在你的asp.net页面顶部添加一个decleration,这样你就可以引用自定义控件,如下所示:

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

现在引用你html中的控件,如下所示:

  

好的,最后一步是将控件绑定到后面的代码中的某些数据 - 如下所示:

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

以下是运行后的输出:

 

Mike

Amy

Mary

Albert

希望这可以帮助你摆脱困境。

请享用!

不漂亮,但应该给你一个开始

  //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看起来是更好的解决方案

虽然丑陋,但.aspx中的ASP Code Nuggets会起作用:

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

……并在代码隐藏中:

  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); } }