Home » Visual Studio

DataGridView Default Error Dialog exception.


When I add a new row, and then do bindingsource.rejectchanges(), for some reason I get this strange exception. The exception is not in a regular dialoge box, but a different one.  It says the following.

The following exception occured in teh DataGridView:

System.IndexOutOfRangeException: Index 28278 does not have a value.

at System.Windows.Forms.CurrencyManager.get_Item(Int32 Index)


System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 RowIndex)

Any idea what could be causing it?


1 Answer Found


Answer 1

The following fixed teh  problem.

clientsDataGridView1.DataSource =



this.clientsDataGridView1.DataSource = this.clientsBindingSource;



In SharePoint, I opened a modal dialog named a by SP.UI.ModalDialog.ShowModalDialog, then I opened a sub dialog b from dialog a. When I close the dialog b by clicking the "X" button, it will trigger the onbeforeunload event in top page. But I close the dialog by SP.UI.ModalDialog.commonModalDialogClose, it will not. It is odd.


I use this code to export my datagridview to excel. bu this does not give the user the posibility to choose the location or the name of the file. How can I do this? Do you have any ideas ??? 

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   



Dim xlApp As Excel.Application

Dim xlWorkBook As Excel.Workbook

Dim xlWorkSheet As Excel.Worksheet 

Dim misValue AsObject = System.Reflection.Missing.Value  Dim i AsInteger

Dim j AsInteger

xlApp =New Excel.Application

xlWorkBook = xlApp.Workbooks.Add(misValue)

xlWorkSheet = xlWorkBook.Sheets(



For i = 0 To DataGridView1.RowCount - 2  For j = 0 To DataGridView1.ColumnCount - 1



xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i).Value.ToString()











PrivateSub releaseObject(ByVal obj AsObject)  Try





obj =



Catch ex AsException

obj =








thanks a lot


"TextBox1.text"".xlsx") "You can find the file C:\vbexcel.xlsx")  




Hello i want to create a Phone book

i made on my parent window a datagridview that connect to a database

i have also a menu i have added a button


<pre lang="x-vbnet">'my code for opening the dialog'

in the dialog i have 7 label's and 7 textbox's

-Name + textbox

-Sur name + textbox

-phone name + Textbox


Now i have a button on the bottom of the dialog


i want when i press the button it will save it to the database and also refresh the datagridview


but how do i do that




Arno (proudtobemexp)


I am trying to force the user to select the input files before processing and creating a new worksheet(s).  Is there any dialog (builtin) for the user to select the input file?  Is there a list for the builtin dialogs in Excel 2007?  At our work we are running Office 2007 on Vista.  We do not know when we will move to W7.

Is it easy to build a data input wizard for Excel 2007?  Any pointers for this?

Thank you in advance.



Hi, I've implemented an ActiveX control for IE, that displays PDF's in the browser (embedded or full page). I also have an option that enables/disables the full page mode.

I handle the logic of opening the pdf in the browser, or not, in the OnBeforeNavigate callback.

My question is, I want to trigger the default IE open/save dialog, but I am unable to.

I've tried the DoFileDownload function from shdocvw.dll, but this goes straight to the SaveAs dialog, with no Open option. I've read that DoFileDownloadEx provides the Open/Save option, and I see shdocvw.dll also has a DoFileDownloadEx function, but it, and DoFileDownload, are actually located in ieframe.dll. Checking ieframe.dll only DoFileDownload shows up. Using GetProcAddress for DoFileDownload works, but trying for DoFileDownloadEx always fails (for both shdocvw.dll and ieframe.dll)

How does one trigger the default IE open/save dialog?



Can I change the default sort order for SSMS 2008 File Open dialog?

I found the registry key HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\100\Tools\Shell\Open Find\Microsoft SQL Server Management Studio\Settings\Open File\View but I'm not sure how to edit the binary data 

Hello, I'm working with VS 2010 and I'd like my default Open Project option to be "Add to Solution" like it was in VS 2008. It appears that it was changed to "Close Solution" in VS 2010. Can I configure this default anywhere in the system? The reason I would like it to behave this way is explained in my TFS forum post here: http://social.msdn.microsoft.com/Forums/en/tfsgeneral/thread/446934c1-2928-4089-ba77-50222ef3776f A short description of why I need to set this default that I want projects to be added to my current solution when I double click them in the TFS Source Control Explorer (as they were in VS 2008). Could you provide me with some direction of how I may configure this in Visual Studio? Thanks, -Nick

I have an msi I've created. It installs a windows service I wrote with has CanStop=false so it requires a reboot. I'd like still let the user have that option at the end whether to reboot or not for the change to take affect...however...I don't want them to have to hit the "Next" button on the first two standard prompts that come up which the installer presents (i.e., install for everyone or me etc...).

Can I use Orca and the ControlEven table to provide this default behavior? If so...how do I go about doing it...DoAction or something like that?


Hi all,

I have a problem which I am not sure of how to do this. Maybe you can help me out.

Currently I have a Silverlight application which contains a form on which a user can export data to an excel or pdf file. What happens is when a user presses a button an excel or pdf file is created. The page on which the silverlight application is hosted contains some code which returns the export file to the user:

Response.Charset = "";
Response.ContentType = "application/" + boDocument.DocumentType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + boDocument.DocumentFilename);
byte[] aByte = boDocument.DocumentContent.ToArray();

All going perfect which results in a dialog screen asking the user if he wants to open the file or save it to disk (the default dialog screen when downloading something).

The problem I now have is that I have enabled my application for Out Of Browser. Now I would like to have that same dialog box again in which the user can choose to save or open the created export file.

But how??


Dear all,

I would like the "Enter" key to behave like a "Tab" key in DataGridView cells. After searching for some time, I couldn't find much to help me. Any ideas?


I want to export a datagridview to a new Excel file and I want to use the default normal template or any specified template, how can I specify template for a new Excel file?

Hello All,

I am manually adding a rows to a datagrid view.

everytime the first row gets selected and turns dark blue in the background color.

my problem is sometimes I need to show red color on error. So I need to disable the default cell selection .

how do i make a setting so tat no row gets selected in the grid by default.




I have the following code to add a new column into the datagridview but how to default value to this column?

I do not want to use for loop to loop through each row and get the column updated, is there any other way?

I want to default them to 'N' and i will have the code to change it to 'Y' subsequently based on conditions.


        dgDataConfiguration.Columns.Add("Update", "Update")


Many thanks.



I put a folderBrowserDialog on my form and used the following code to show it:

Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();

But  when I run my program I get the following exception in both debug and release modes:

An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll

Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process. 


I put the following code before calling the dialog but it didn't work:

System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;

But what is the problem and how can I solve it? Note that I don't have any Main function as it is a CLR application. Thanks,


I have an MFC-driven dialog-based application created with MSVS2005. Here is my problem step by step. I have button on my dialog and corresponding click-handler with code like this:

int* i = 0;
*i = 3;
I'm running debug version of program and when I click on the button, Visual Studio catches focus and alerts "Access violation writing location" exception, program cannot recover from the error and all I can do is to stop debugging. And this is the right behavior.

Now I add some OpenGL initialization code in the OnInitDialog() method:

HDC DC = GetDC(GetSafeHwnd());
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored

int pixelformat = ChoosePixelFormat(DC, &pfd);
SetPixelFormat(DC, pixelformat, &pfd);

HGLRC hrc = wglCreateContext(DC);
ASSERT(hrc != NULL);
wglMakeCurrent(DC, hrc);

Of course this is not exactly what I do, it is the simplified version of my code. Well now the strange things begin to happen: all initialization is fine, there are no errors in OnInitDialog(), but when I click the button... no exception is thrown. Nothing happens. At all. If I set a break-point at the *i = 3; and press F11 on it, the handler-function halts immediately and focus is returned to the application, which continue to work well. I can click button again and the same thing will happen.

It seems like someone had handled occurred exception of access violation and silently returned execution into main application message-receiving cycle.

If I comment the line wglMakeCurrent(DC, hrc);, all works fine as before, exception is thrown and Visual Studio catches it and shows window with error message and program must be terminated afterwards.

I experience this problem under Windows 7 64-bit, NVIDIA GeForce 8800 with latest drivers (of 11.01.2010) available at website installed. My colleague has Windows Vista 32-bit and has no such problem - exception is thrown and application crashes in both cases.


PS Already contacted with nVidia about this issue, they say it isn't their bug.

I have Visual Studio 2008 Team Edition.
On the Exceptions dialog (Debug->Exceptions on the menu) I'm given a list of exception types for the debugger to break into. There are 2 columns, Name and Thrown(with check boxes in it). With 2005, I also had a column to specify that the exception wasn't handled by user code. Anyone have any idea where that second checkbox column went?
Mike Swaim

I get the infamous "Dialogs must be user-initiated" Security Exception when I try to print some stuff in Silverlight. As you can see, the dialog is as user-initiated as can be:
![Silverlight printing "Dialogs must be user-initiated" Security Exception][1]
[John Papa couldn't help me much out neither][2], because I don't have any breakpoint set. [Mr MSDN thinks it could also be that I'm just taking too long][3], but this is a demo application just as simple as can be.
Any ideas? I guess it's a Visual Studio quirk, maybe some extensions interfering, as things seems to work when I launch the application outside. I first thought maybe the Code Contracts are interfering with their IL weaving, but they are deactivated for this project.
  [1]: http://i.imgur.com/SOmG3.png
  [2]: http://johnpapa.net/silverlight/printing-tip-ndash-handling-user-initiated-dialogs-exceptions/ "John Papa: Printing Tip – Handling User Initiated Dialogs Exceptions"
  [3]: http://msdn.microsoft.com/en-us/library/ee671023(VS.95).aspx "MSDN: Silverlight Printing"

I get the infamous "Dialogs must be user-initiated" Security Exception when I try to print some stuff in Silverlight. As you can see, the dialog is as user-initiated as can be:

Silverlight printing "Dialogs must be user-initiated" Security Exception

John Papa couldn't help me much out neither, because I don't have any breakpoint set. Mr MSDN thinks it could also be that I'm just taking too long, but this is a demo application just as simple as can be.

Any ideas? I guess it's a Visual Studio quirk, maybe some extensions interfering, as things seems to work when I launch the application outside. I first thought maybe the Code Contracts are interfering with their IL weaving, but they are deactivated for this project.

Update: This is just a simple Silverlight application that runs locally from the file system. When I do "Start debugging", Visual Studio creates a hosting HTML file containing the Silverlight app in the Debug resp. Release folder of the project, launches the Internet Explorer with that HTML file and attaches the debugger to it.

Update 2: I also get the same error when I create a web project to host the Silverlight app and create a virtual directory for it on IIS.

I might also want to add that I don't have problems with printing in other Silverlight projects regardless of their hosting scenarios.

Update 3: I downloaded FireFox and it works, I don't get the error when I debug with it. So it seems to have to do with my IE8. I uploaded the solution:


I wonder if anyone can reproduce?


Hello all,

I'm having a problem with DataGirdView. 

I am using a BindingSource object to bind that DataTable object to the DataGridView and I get the following exception thrown on the line I set the DataGirdView.DataSource = BindingSource object:-

Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"} System.Exception {System.ArgumentOutOfRangeException}

Heres the code:-

Code Snippet

source = newBindingSource();

source.DataSource = dset.Tables[0];

resultsGrid.DataSource = source;

I am at a loss as to why this is happening as the the DataSet contains 1 table and the Table contains 1 row.

After it throws the exception the app binds the table to DataGridView as expected 

Does anyone know why this would be happening ??



I am getting an argument out of rance exception error when trying to set the DGV column names.  I set the Datasource of the DGV in another function then  immediately call this code to set up column name information for the DGV



void SetUpGrid()



//dgvJobData.AutoGenerateColumns = false;

dgvJobData.AllowUserToAddRows =


dgvJobData.AllowUserToDeleteRows =


dgvJobData.AllowUserToResizeColumns =


dgvJobData.AllowUserToOrderColumns =


dgvJobData.Columns[0].AutoSizeMode =


dgvJobData.Columns[0].HeaderText =

"Job ID";


dgvJobData.Columns[1].AutoSizeMode =


dgvJobData.Columns[1].HeaderText =


dgvJobData.Columns[2].AutoSizeMode =


dgvJobData.Columns[2].HeaderText =


dgvJobData.Columns[3].AutoSizeMode =


dgvJobData.Columns[3].HeaderText =

"Job Fixed";

dgvJobData.Columns[4].AutoSizeMode =


dgvJobData.Columns[4].HeaderText =

"Job Start";

dgvJobData.Columns[5].AutoSizeMode =


dgvJobData.Columns[5].HeaderText =

"Job Close";

dgvJobData.Columns[6].AutoSizeMode =


dgvJobData.Columns[6].HeaderText =

"Latest End Time";

dgvJobData.Columns[7].Visible =

false; // Hide the status column

dgvJobData.Columns[8].Visible =

false; // Hide the alert status column


When the code hits the first Column[0] line it throws the exception with the following message:-

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Any help would greatly be appreciated





A form in my application contains a DataGridView.  In this application, the form also includes TextBox(s), ComboBox(s) buttons.  The user clicks a button to add a line, fills in the text boxes and ComboBox(s).  Another button allows the user to edit and save lines.  When the edit or add button is clicked, the DataGridView is disabled, when clicks Save button the items either edit an existing line or add a new line as per the add or add button clicked.  When not in edit or add mode, the user can click on a DataGridView row to select it, or click on a column header to sort.  The buttons Enable or disable the DataGridView and do other things.  The DataGridView is bound to a BindingSource which is bound to a DataSet. 

The problem is as follows:  When editing a row in the DataGridView for the second time one of the things that clicking the Edit button does is to Disable the DataGridView.  This causes an Invalid Operation Exception exception with the following stack.  I would appreciate any help with this:


Operation is not valid due to the current state of the object.

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)

   at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)

   at System.Windows.Forms.BindingSource.OnListChanged(ListChangedEventArgs e)

   at System.Windows.Forms.BindingSource.Add(Object value)

   at System.Windows.Forms.BindingSource.AddNew()

   at System.Windows.Forms.CurrencyManager.AddNew()

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew()

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded()

   at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)

   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)

   at System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick, Boolean clearSelection, Boolean forceCurrentCellSelection)

   at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean includeNewRow)

   at System.Windows.Forms.DataGridView.OnEnter(EventArgs e)

   at System.Windows.Forms.ContainerControl.UpdateFocusedControl()

   at System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control value)

   at System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control, Boolean originator)

   at System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control value)

   at System.Windows.Forms.ContainerControl.SetActiveControl(Control ctl)

   at System.Windows.Forms.Control.Select(Boolean directed, Boolean forward)

   at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)

   at System.Windows.Forms.Control.SelectNextControlInternal(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)

   at System.Windows.Forms.Control.SelectNextIfFocused()

   at System.Windows.Forms.Control.set_Enabled(Boolean value)

   at atlasquote.AlternateQuote.SetIsEditingLi() in G:\Atlas\AtlasQuote\atlasquote\AlternateQuote.cs:line 2572


The error event is caused by the following line of code:

            LineItemsDataGridView.Enabled = false;



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure