datagird "Index out of array bound" exception

G

Guest

When the datagrid has multi rows and the vertical scrollbar hasn't appeared,
if I click the column near the bound or drag horizontal scrollbar, this
exception will be throwed. The trace stack as follows:

at System.Windows.Forms.DataGrid.CreateScrollableRegion(Rectangle scroll)
at System.Windows.Forms.DataGrid.set_HorizontalOffset(Int32 value)
at System.Windows.Forms.DataGrid.ScrollRight(Int32 columns)\
at System.Windows.Forms.DataGrid.ScrollToColumn(Int32 targetCol)
at System.Windows.Forms.DataGrid.EnsureVisible(Int32 row, Int32 col)
at System.Windows.Forms.DataGrid.set_CurrentCell(DataGridCell value)
at System.Windows.Forms.DataGrid.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button,
Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
..
..
..

My datasource is a datatable.
IDE: .net 2003 + framework 1.1 + sp1
Does anyone know whether this is the DataGrid own bug?
What can I do to workaground it or resolve it?
Thanks.
 
P

Peter Vitt

This exception can be thrown when you are altering the data in the bound
DataSource but you dont update the DataGrid. So it trys to show rows that arent
available anymore.
Check if the RaiseListChangedEvents-Property of the underlying bindingSource is set.

I hope that helps.
Greets
 
G

Guest

Hi Peter,

Thank you for your reply.

My case is: Anytime I udpate the bound DataSource, I rebind the DataGrid to
new DataSource. This exception only occurs when the DataGrid has more than
one rows and the vertical scrollbar hasn't appeared. But if the DataGrid has
only one row or the vertical scrollbar has appeared, anything is ok.

Follows is the code:
private void UpdateGrid()
{
this.m_grid.ReadOnly = false;
DataTable dt = (DataTable)this.m_grid.DataSource;
dt.Clear();
int numColumns = PageVersionsEditor.FixedColumns.Length;
if ( this.m_showDefaultVersionColumn )
numColumns += 1;
numColumns += this.m_allZones.Count;
for( int iVersion = 0; iVersion < this.m_versions.Count;
iVersion++ )
{
Version ver = (Version)this.m_versions[iVersion];
int column = 0;

object[] row = new object[numColumns];

row[column++] = iVersion+1;

// Default version
if ( this.m_showDefaultVersionColumn )
{
row[column++] = ver.IsDefault();
}

foreach ( Zone z in this.m_allZones )
{
row[column++] = ver.HasZone(z.name);
}

dt.Rows.Add(row);
}
this.m_grid.DataSource = dt;
this.m_grid.ReadOnly = true;
}

What happened?

Thanks & Regard,
Cymer
 
G

Guest

And I'm very sorry that my framework is 1.1, RaiseListChangedEvents only be
supported from 2.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top