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
Read-Only.

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
box:

---------------------------
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);
selected_xmtrs_list->Add(xw);
}
/*if (selected_xmtrs_list->Count == 0) {
selected_xmtrs_list->Add(new XmtrWrapper);
datagrid_selected_xmtrs->DataSource = selected_xmtrs_list;
selected_xmtrs_list->Clear();
}*/

datagrid_selected_xmtrs->DataSource = selected_xmtrs_list;

DataGridTableStyle* table_style = XmtrDataGridTableStyle();
datagrid_selected_xmtrs->TableStyles->Clear();
datagrid_selected_xmtrs->TableStyles->Add(table_style);
}

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"
button:

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;
selected_xmtrs_->push_back(*xmtr);
selected_xmtrs_list->Add(all_xmtrs_list->Item[i]);
selected_xmtrs_list->Sort();
}
}
}
// 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
correctly?
************** 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 **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
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
----------------------------------------
System.Drawing
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
----------------------------------------
System.Xml
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.