Populating a GridView from a 2-dimensional array and editing the rows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ASPnewb1
    New Member
    • Mar 2008
    • 14

    Populating a GridView from a 2-dimensional array and editing the rows

    Hello,

    I have a 2-dimensional array created from the following text file:

    [test1]
    data1=asdfawe
    data2=2
    data3=223d
    data4=22

    [test2]
    data1=23fs
    data2=234rs
    data3=2d@
    data4=wef
    data5=23f2

    [test3]
    data1=2ef
    data2=weffws
    data3=wewfe
    data4=dw


    What I am currently doing to fill the gridview is this:
    [code=C#]
    Table table = new Table();

    GridViewRow headerRow = new GridViewRow(-1, -1, DataControlRowT ype.Header, DataControlRowS tate.Normal);

    TableCell h1cell = new TableCell();
    h1cell.Controls .Add(new LiteralControl( "test #"));
    headerRow.Cells .Add(h1cell);

    for (int n = 0; n < maxElements; n++)
    {
    TableCell hcell = new TableCell();

    hcell.Controls. Add(new LiteralControl( n.ToString()));
    headerRow.Cells .Add(hcell);
    }

    table.Rows.Add( headerRow);

    for (int a = 0; a < numSearches; a++)
    {
    GridViewRow row = new GridViewRow(a, a, DataControlRowT ype.DataRow, DataControlRowS tate.Edit);

    for (int b = 0; b < maxElements; b++)
    {
    TableCell cell = new TableCell();
    cell.Controls.A dd(new LiteralControl( array[a, b]));

    row.Cells.Add(c ell);
    }


    table.Rows.Add( row);
    }

    GridView1.Contr ols.Add(table);
    [/code]


    This gives me:




    If all I wanted to do was show the text file in a grid setting I would be finished, but I cannot figure out how to add a column that allows editing of the rows.

    When I manually add a column to the Gridview in the GUI editor, it shows up with edit or delete buttons down the column (depending on what I chose, edit or delete).

    But when I run the code it removes the edit/delete column from the gridview.

    All I really need to be able to do is edit the contents of the row and then have that change the values inside the table to be later posted to the same or a new text file.

    thank you.
  • markrawlingson
    Recognized Expert Contributor
    • Aug 2007
    • 346

    #2
    Hi ASPNewb. Please post your question in the correct forum - ASP is not ASP.Net. Also, please use code tags around your code.


    I'll move this thread to the .net forum.

    Sincerely,
    Mark

    Comment

    • Shashi Sadasivan
      Recognized Expert Top Contributor
      • Aug 2007
      • 1435

      #3
      I havent tried to bind a grid to an array,
      however you can create a dataset and bind it to that.

      Comment

      • ASPnewb1
        New Member
        • Mar 2008
        • 14

        #4
        Thanks for the tip.

        I tried a dataset and I have the same results. It will post to the gridview, but I'm not sure how to have a column that allows editing of each row by the user.

        Comment

        • ASPnewb1
          New Member
          • Mar 2008
          • 14

          #5
          update:

          I have the array correctly posting to the grid.

          What I'm now running into is the edit feature is not loading the grid back again on the screen.

          What I currently have (minus the function that creates the 2-dim array)

          protected void Page_Load(objec t sender, EventArgs e)
          {
          if (!Page.IsPostBa ck)
          BindData();
          }

          protected void Edit_DataGrid(o bject sender, GridViewEditEve ntArgs e)
          {
          GridView2.EditI ndex = e.NewEditIndex;

          // GridView2.DataB ind();

          BindData();
          }

          protected void BindData()
          {
          DataSet ds = new DataSet();

          DataTable dt = new DataTable();

          ds.Tables.Add(d t);


          DataColumn dc = new DataColumn("sea rch #");
          dt.Columns.Add( dc);

          dc = new DataColumn("1") ;
          dt.Columns.Add( dc);

          dc = new DataColumn("2") ;
          dt.Columns.Add( dc);

          dc = new DataColumn("3") ;
          dt.Columns.Add( dc);

          dc = new DataColumn("4") ;
          dt.Columns.Add( dc);

          dc = new DataColumn("5") ;
          dt.Columns.Add( dc);


          DataRow dr;

          object[] myArray = new object[maxElements];

          for (int a = 0; a < numSearches; a++)
          {
          for (int b = 0; b < maxElements; b++)
          {
          myArray[b] = array[a, b];

          }

          dr = dt.NewRow();
          dr.ItemArray = myArray;
          dt.Rows.Add(dr) ;
          }

          GridView2.DataS ource = ds;
          GridView2.DataB ind();
          }





          When the edit button is clicked the page is reloaded without the gridview in site.

          I did put the above edit handler into the Events RowEditing slot in the properties window.

          any suggestions?

          Comment

          Working...