- Joined
- Jan 20, 2009
- Messages
- 21,122
- Likes received
- 773
- Thanks received
- 222
- Location
- Clown World
Perhaps the real question is why you've ended up with an array of data rather than a List<T> of objects?
A good design ends up with populated objects, rather than N-dimensional arrays of strings.
Hrm,
Off the top of my head, try
String[] myArray = (String[])Session["myArrayName"];
List<String> myList = new List<String>(myArray);
myGridView.DataSource = myList;
myGridView.DataBind();
You could do this in Page Load, or you could return the list within a separate class and refer to it within an object datasource. Or you could do this in Linq!
PS - I have a funny feeling I've missed the entire point of the question. Its early in the morning though!!
)<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvLeagueTable" runat="server" AutoGenerateColumns="True">
<columns>
<asp:TemplateField HeaderText="Name" >
<EditItemTemplate>
<asp:TextBox ID="txtTeam" runat="server" Text='Container.DataItem(0)'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTeam" runat="server" Text='Container.DataItem(0)'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="P" >
<EditItemTemplate>
<asp:TextBox ID="txtPlayed" runat="server" Text='Container.DataItem(1)'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPlayed" runat="server" Text='Container.DataItem(1)'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="W" >
<EditItemTemplate>
<asp:TextBox ID="txtWon" runat="server" Text='Container.DataItem(2)'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblWon" runat="server" Text='Container.DataItem(2)'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="L" >
<EditItemTemplate>
<asp:TextBox ID="txtLost" runat="server" Text='Container.DataItem(3)'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLost" runat="server" Text='Container.DataItem(3)'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="D" >
<EditItemTemplate>
<asp:TextBox ID="txtDrawn" runat="server" Text='Container.DataItem(4)'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDrawn" runat="server" Text='Container.DataItem(4)'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pts" >
<EditItemTemplate>
<asp:TextBox ID="txtPoints" runat="server" Text='Container.DataItem(5)'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPoints" runat="server" Text='Container.DataItem(5)'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
String[,] leagueTable = new String[6,6];
Session["leagueTable"] = leagueTable;
leagueTable[0, 0] = "Tottenham";
leagueTable[0, 1] = "38";
leagueTable[0, 2] = "38";
leagueTable[0, 3] = "0";
leagueTable[0, 4] = "0";
leagueTable[0, 5] = "114";
leagueTable[1, 0] = "Arsenal";
leagueTable[1, 1] = "38";
leagueTable[1, 2] = "0";
leagueTable[1, 3] = "38";
leagueTable[1, 4] = "0";
leagueTable[1, 5] = "0";
gvLeagueTable.DataSource = leagueTable;
gvLeagueTable.DataBind();
}
}
}
The problem is the last line of the cs
gvLeagueTable.DataBind();
Argument exception "Array was not a one-dimensional array."
We got it working with a one dimensional array. Grids with more than one column are multidimensional, Shirley?
In the Page HTML, you would probably need to change the code which renders the values to something like
<asp:TextBox Text='<%# DataBinder.Eval(Container.DataItem,"FieldNameHere") %>'
Or
<asp:TextBox Text='<%# Bind("FieldsNameHere") %>'
Interesting thought. The underlying object is a string array, so it is numerically indexed. If the grid view needs a kvp this could be the problem.
I guess it is whatever Container.DataItem actually is. Can't step through as it's markup though.
Back to google.