Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Exception deleting a row from DataGrid bound to an ArrayList

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
km - 07 Mar 2006 19:54 GMT
I have a DataGrid bound to an ArrayList. When I delete a 'row' from the
ArrayList the DataGrid throws an IndexOutOfRangeException exception. I
constructed a very simple sample app that reproduces the problem (code
below). This sample simply populates the grid from the array (see Form1
constructor). When you click the button it deletes the current row (see
button1_click). At that point the exception is thrown.

If anyone could suggest what I'm doing wrong I'd appreciate it.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
 public class Form1 : System.Windows.Forms.Form
 {
   private ArrayList m_oArrayList=new ArrayList();
   private System.Windows.Forms.DataGrid m_oDataGrid;
   private System.Windows.Forms.Button button1;

   private System.ComponentModel.Container components = null;

   public Form1()
   {
     InitializeComponent();

     m_oArrayList.Add(new NameValuePair("NameOne","ValueOne"));
     m_oArrayList.Add(new NameValuePair("NameTwo","ValueTwo"));
     m_oArrayList.Add(new NameValuePair("NameThree","ValueThree"));
     m_oArrayList.Add(new NameValuePair("NameFour","ValueFour"));
     m_oArrayList.Add(new NameValuePair("NameFive","ValueFive"));
     m_oArrayList.Add(new NameValuePair("NameSix","ValueSix"));
     m_oArrayList.Add(new NameValuePair("NameSeven","ValueSeven"));

     m_oDataGrid.SetDataBinding(m_oArrayList,"");
   }

   private void button1_Click(object sender, System.EventArgs e)
   {
     m_oArrayList.RemoveAt(m_oDataGrid.CurrentRowIndex);
     m_oDataGrid.Refresh();
   }

   protected override void Dispose( bool disposing )
   {
     if( disposing )
     {
       if (components != null)
       {
         components.Dispose();
       }
     }
     base.Dispose( disposing );
   }

   private void InitializeComponent()
   {
     this.m_oDataGrid = new System.Windows.Forms.DataGrid();
     this.button1 = new System.Windows.Forms.Button();
     
((System.ComponentModel.ISupportInitialize)(this.m_oDataGrid)).BeginInit();
     this.SuspendLayout();
     this.m_oDataGrid.DataMember = "";
     this.m_oDataGrid.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
     this.m_oDataGrid.Location = new System.Drawing.Point(16, 40);
     this.m_oDataGrid.Name = "m_oDataGrid";
     this.m_oDataGrid.Size = new System.Drawing.Size(264, 176);
     this.m_oDataGrid.TabIndex = 0;
     this.button1.Location = new System.Drawing.Point(80, 8);
     this.button1.Name = "button1";
     this.button1.Size = new System.Drawing.Size(64, 24);
     this.button1.TabIndex = 1;
     this.button1.Text = "delete";
     this.button1.Click += new System.EventHandler(this.button1_Click);
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 229);
     this.Controls.Add(this.button1);
     this.Controls.Add(this.m_oDataGrid);
     this.Name = "Form1";
     this.Text = "Form1";
     
((System.ComponentModel.ISupportInitialize)(this.m_oDataGrid)).EndInit();
     this.ResumeLayout(false);
   }

   [STAThread]    static void Main()
   {
     Application.Run(new Form1());
   }
 }   
   
 public class NameValuePair
 {
   private string m_strName=string.Empty;
   public string Name{get{return m_strName;} set{m_strName=value;}}

   private string m_strValue=string.Empty;
   public string Value{get{return m_strValue;} set{m_strValue=value;}}

   public NameValuePair(string strName,string strValue)
   {
     m_strName = strName;
     m_strValue =strValue;
   }
 }   
}
km - 10 Mar 2006 15:20 GMT
I've found the following to be a reliable solution:

private void button1_Click(object sender, System.EventArgs e)
{
 m_oArrayList.RemoveAt(m_oDataGrid.CurrentRowIndex);
 
 CurrencyManager cm;
 cm = (CurrencyManager)m_oDataGrid.BindingContex[m_oDataGrid.DataSource];
 cm.Refresh();
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.