• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • 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

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    GridView dot net question

    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.
    Knock first as I might be balancing my chakras.

    #2
    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.

    Comment


      #3
      Originally posted by DimPrawn View Post
      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.
      Knock first as I might be balancing my chakras.

      Comment


        #4
        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 by Weltchy; 28 July 2009, 07:18.

        Comment


          #5
          Originally posted by Weltchy View Post
          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 ()
          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 by suityou01; 28 July 2009, 07:35. Reason: Reread post.
          Knock first as I might be balancing my chakras.

          Comment


            #6
            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

            [Quote]
            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?
            Knock first as I might be balancing my chakras.

            Comment


              #7
              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 by Weltchy; 28 July 2009, 11:05.

              Comment


                #8
                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") %>'

                Comment


                  #9
                  Originally posted by Weltchy View Post
                  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.
                  Knock first as I might be balancing my chakras.

                  Comment


                    #10
                    Originally posted by suityou01 View Post
                    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.

                    Comment

                    Working...
                    X