Solved

Datagrid a,d collections error

Posted on 2004-10-31
151 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
Question by:haezeban
    1 Comment
     
    LVL 11

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    845 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now