Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Datagrid a,d collections error

Posted on 2004-10-31
1
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

609 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