?
Solved

Datagrid a,d collections error

Posted on 2004-10-31
1
Medium Priority
?
156 Views
Last Modified: 2010-04-15
Hey,

Struggling for a few days now (question I already ask before, but now I wrote some code where I can generate my error again)

I have a piece of code (see below). It is an array bound to an asp-datagrid.
There are 3 objects in the array, so 3 items in the datagrid.
I select the second item (Object2) and delete it out of the array an then
rebind the array to the datagrid.
Then automatically the next item in datagrid is selected (Onject2), I delete
out of the array and rebind the array.
Then nothing is selected in the array, but Object1 is still there. One I
push delete (without selecting the item with a mouse-click) I receive an
error on the test if there is an item selected.

I don't understand what is wrong here. Ok, there is no selecteditem but
therfor I do a test. And that test fails?
Can someone explains me what is wrong in that case.
Thanks a lot.

(this is working code)

using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestDg
{
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DataGrid DGusers;
            protected System.Web.UI.WebControls.Button Button2;
            ArrayList myarr = new ArrayList();

            private void Page_Load(object sender, System.EventArgs e)
            {
                  if (!Page.IsPostBack)
                  {
                        myarr.Add(new Obj("Object1","nhz"));
                        myarr.Add(new Obj("Object2","pvp"));
                        myarr.Add(new Obj("Object3","jnj"));

                        Session.Add("Arr",null);
                        Session["Arr"] = myarr;

                        this.DGusers.DataSource = myarr;
                        this.DGusers.DataBind();
                  }
                  myarr = (ArrayList) Session["Arr"];            
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button2.Click += new System.EventHandler(this.Button2_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void Button2_Click(object sender, System.EventArgs e)
            {
                  if (this.DGusers.SelectedItem != null)
                  {
                        myarr.RemoveAt(this.DGusers.SelectedIndex);

                        this.DGusers.DataSource = myarr;
                        this.DGusers.DataBind();

                        Session["Arr"] = myarr;
                  }
            }

            public class Obj
            {      

                  public Obj(string nm, string ini)
                  {
                        this.mnaam = nm;
                        this.minit = ini;
                  }
                  private string mnaam;
                        
                        public string Naam
                        {
                              get
                              {
                                    return mnaam;
                              }
                              set
                              {
                                    mnaam = value;
                              }                  
                        }

                  private string minit;
                  public string Initialen
                  {
                        get
                        {
                              return minit;
                        }
                        set
                        {
                              minit = value;
                        }
                        
                  }
            }            
      }
}


 
 
 
0
Comment
Question by:haezeban
1 Comment
 
LVL 11

Accepted Solution

by:
sachiek earned 750 total points
ID: 12461024
First logic change you can do is


 private void Button2_Click(object sender, System.EventArgs e)
          {
               if (this.DGusers.SelectedItem != null)
               {
                    myarr.RemoveAt(this.DGusers.SelectedIndex);

                    this.DGusers.DataSource = myarr;
                    this.DGusers.DataBind();
               }
                    Session["Arr"] = myarr;
          }


Sachi
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question