473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

datagrid and collections - Error

Nic
Hey,

Struggling for a few days now.

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;
}

}
}
}
}
Nov 16 '05 #1
0 916

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
0
by: Reb | last post by:
I am using a datagrid within another datagrid. My second datagrid is expand/collapse one. I am facing some error. This is my code. ExpandGrid.aspx <form id="Form1" method="post"...
4
by: kscdavefl | last post by:
I ahve a datagrid on a web form. I need to change the value in column 3 as follows. If the value in column 3 reads 0, I want to change it to read YES. How can I accomplish this task. ...
0
by: Mauro | last post by:
Hi, I need a big help to resolve this problem. I need to put a usercontrol in a datagrid: this control check if the code inserted is present in a archive and if not return a error message. (In...
1
by: Michael Gorbach | last post by:
Iv got a StatisticsContainer object that contains an arraylist of objects of different types, all inherited from class Statistic. The statistics class has 2 string public properties, name and...
3
by: BobforeApples | last post by:
Hi All, I was trying to apply databinding to a custom collection as in the MSDN Magazine article found in the August, 2005 issue. After converting the code in the article to C# and applying it to...
1
by: Clark Choi | last post by:
I ran the sample application called Petstore from msdn. Everything went fine until I tested Update button on the web form to update the database through Datagrid. I have been searching the web to...
3
by: CVerma | last post by:
Hi, I have an embedded datagrid within a datalist. I am not able to perfrom paging in the datagrid. Any ideas? Here is my code: Here is my Simplegrid.cs file: using System; using...
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.