By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

[MC++] DataGrid bound to ArrayList problem

P: n/a
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, then the datagrid is bound to it, and it works
fine. The bottom DataGrid is bound to a different ArrayList that holds
the same type as the first ArrayList. Both DataGrids are set to

The user selects a row from the top DataGrid, then clicks the "Add"
button to copy it to the bottom DataGrid. If I then click on the entry
in the bottom DataGrid, then click off it, I get the following message

Error when committing the row to the original data store.
Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index Do you want to correct the value?
Yes No

Clicking "Yes" does not help, since the DataGrid is Read-Only so none of
the values can be changed. If I click "No", then I get the exception
text given at the bottom of this post (I removed the reference to my
app's loaded assembly, but I left everything else).
Here is my function where I initialize the DataGrid:

void init_selected_xmtrs_datagrid()
using System::Convert;
using System::IO::StreamReader;

selected_xmtrs_list = new ArrayList();

typedef std::vector<Xmtr>::const_iterator XCI;
for (XCI i = selected_xmtrs_->begin(); i != selected_xmtrs_->end(); ++i) {
XmtrWrapper* xw = new XmtrWrapper(*i);
/*if (selected_xmtrs_list->Count == 0) {
selected_xmtrs_list->Add(new XmtrWrapper);
datagrid_selected_xmtrs->DataSource = selected_xmtrs_list;

datagrid_selected_xmtrs->DataSource = selected_xmtrs_list;

DataGridTableStyle* table_style = XmtrDataGridTableStyle();

If I uncomment the commented section (which I tried to do to get it to
show the column names when the ArrayList is empty, which didn't work),
then I do not get the error.
Here is my function that gets called when the user clicks the "Add"

System::Void button_add_Click(System::Object * sender, System::EventArgs * e)
CurrencyManager* cm = __try_cast<CurrencyManager*>(BindingContext->Item[all_xmtrs_list]);
int num_rows = cm->Count;
for (int i = 0; i < num_rows; ++i) {
if (datagrid_all_xmtrs->IsSelected(i)) {
if (!selected_xmtrs_list->Contains(all_xmtrs_list->Item[i])) {
XmtrWrapper* xw = __try_cast<XmtrWrapper*>(all_xmtrs_list->Item[i]);
Xmtr* xmtr = xw->xmtr;
// reset the datasource to refresh the display
datagrid_selected_xmtrs->DataSource = 0;
datagrid_selected_xmtrs->DataSource = selected_xmtrs_list;
Can anybody explain what is going on, or how to fix my problem
************** Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.CancelCurrent Edit()
at System.Windows.Forms.DataGrid.HandleEndCurrentEdit ()
at System.Windows.Forms.DataGrid.OnLeave_Grid()
at System.Windows.Forms.DataGrid.OnLeave(EventArgs e)
at System.Windows.Forms.Control.NotifyLeave()
at System.Windows.Forms.ContainerControl.UpdateFocuse dControl()
************** Loaded Assemblies **************
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll

Marcus Kwok
Mar 14 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.