• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

GridView dot net question

suityou01

Potty-trained
Joined
Jan 20, 2009
Messages
21,122
Likes received
773
Thanks received
222
Location
Clown World
Is is possible to bind a gridview to a string array, stored in session?

Obviously it can be done as people are but I cannot find any examples of this, and when the update event fires the eventargs has no values for old and new values.
 
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.
 
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.

I agree. Permie lass posed the question. After 45 minutes of "Why would you want to" type questions from me, I just glazed and accepted she had good reason.

I think the data source option was a no go as it needs to be wired to the database and she did not want this.

Others have tried but do not post code.

I suggested doing a manual bind on page load, and defining an item template.

This is working for her.
 
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!!
 
Last edited: 08:14
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!!

Yeah that's one part of it. When the update event fires though the bind is lost. :-( I guess this is because the thing you are binding to is not persisted anywhere. You can persist the array in session state (:suicide:)
and this is what they have done. (Edit : I reread your post, and see you are already doing this)

Also by building an item template you can bind the cells in the grid to the array by using Container.DataItem.

I'll try and post some code later. It is not very easily come across on th'internet and may be useful some time. I've not quite decided.
 
Last edited: 08:28
OK, I put some code together to demonstrate.

This is the page

<%@ 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>

And this is the cs

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?
 
You could hand craft a dataset, or you could create a structure and then populate a list based on that structure.

So, something like


public struct MyObject
{
public String MyField1{get;set;}
public String MyField2{get;set;}
public String MyField3{get;set;}
}

public class DataSources
{
public List<MyObject> MyObjectDataSource()
{
List<MyObject> myList = new List<MyObject>();

//Populate the List however you want here

return myList;

}
}


Now, at this point, you could write the code into the Page Load event, however, you could also use an <asp:ObjectDataSource /> and set the DataSourceID of the GridView to the ObjectDataSource, using something like


<asp:GridView ID="MyGridView" RunAt="Server" DataSourceID="myODS" />
<asp:ObjectDataSource RunAt="Server" ID="myODS" typeName="DataSources" selectMethod="MyObjectDataSource" />

Or something like that.
 
Last edited: 11:58
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") %>'
 
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.
 
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.

You are just propogating a bad design.

Create a class to represent the data. Create a constructor that will consume your mad array of strings. Bind the grid to a List<T> of these objects.

Don't pass the array around your application. Get it into nice objects ASAP.
 

Advertisers

Back
Top