M
Marcus Kwok
This is a repost of a question I posted to the codeproject.com forums,
but got no responses:
http://www.codeproject.com/script/comments/forums.asp?msg=1872655&forumid=1650#xx1872655xx
I forgot to mention in the above post that I am using Managed C++
(Visual Studio .NET 2003 SP1) / .NET Framework 1.1.
In my application, I have a DataGrid that is used to display search
results from a query into a Firebird SQL database. The DataGrid is not
directly connected to the DB, but instead I perform my query and store
the results in a std::vector, perform some more work on it, then copy
the final results into an ArrayList that the DataGrid is connected to.
So, when I do a search, it goes like this:
/* Obtain final std::vector with query results */
/* search_results_list is an ArrayList */
search_results_list->Clear();
// copy data into ArrayList
tr = db_conn->BeginTransaction();
typedef vector::const_iterator CIter;
for (CIter it = run_ids.begin(); it != run_ids.end(); ++it) {
/* look up Run ID in DB and store the run in disp */
search_results_list->Add(disp);
}
tr->Commit();
/* reset DataSource to refresh the display */
datagrid_search_results->DataSource = 0;
datagrid_search_results->DataSource = search_results_list;
This works fine the first time, however I am having the following
problem. If I click on a row, then subsequently perform another search
that makes the row disappear, I get an IndexOutOfRange exception:
************** Exception Text **************
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
However, my DataGrid has set the ReadOnly property to true. In my other
DataGrid that is used for a different purpose, I was able to solve it by
setting the CurrentSelectedRow property to 0 before removing the
elements, but this does not work in this situation.
I am not sure if it is related to this, but the only event I am handling
for this DataGrid is the MouseUp event, in order to get it to select the
entire row when a cell is clicked:
// Select the entire row when the user clicks on a cell in the row
System::Void datagrid_MouseUp(System::Object* sender, System::Windows::Forms::MouseEventArgs* e)
{
using System:rawing:oint;
using System::Windows::Forms:ataGrid;
using System::Windows::Forms:ataGridCell;
Point pt = Point(e->X, e->Y);
DataGrid* datagrid = __try_cast(sender);
DataGrid::HitTestInfo* hti = datagrid->HitTest(pt);
if (hti->Type == DataGrid::HitTestType::Cell) {
datagrid->CurrentCell = DataGridCell(hti->Row, hti->Column);
datagrid->Select(hti->Row);
}
}
but got no responses:
http://www.codeproject.com/script/comments/forums.asp?msg=1872655&forumid=1650#xx1872655xx
I forgot to mention in the above post that I am using Managed C++
(Visual Studio .NET 2003 SP1) / .NET Framework 1.1.
In my application, I have a DataGrid that is used to display search
results from a query into a Firebird SQL database. The DataGrid is not
directly connected to the DB, but instead I perform my query and store
the results in a std::vector, perform some more work on it, then copy
the final results into an ArrayList that the DataGrid is connected to.
So, when I do a search, it goes like this:
/* Obtain final std::vector with query results */
/* search_results_list is an ArrayList */
search_results_list->Clear();
// copy data into ArrayList
tr = db_conn->BeginTransaction();
typedef vector::const_iterator CIter;
for (CIter it = run_ids.begin(); it != run_ids.end(); ++it) {
/* look up Run ID in DB and store the run in disp */
search_results_list->Add(disp);
}
tr->Commit();
/* reset DataSource to refresh the display */
datagrid_search_results->DataSource = 0;
datagrid_search_results->DataSource = search_results_list;
This works fine the first time, however I am having the following
problem. If I click on a row, then subsequently perform another search
that makes the row disappear, I get an IndexOutOfRange exception:
************** Exception Text **************
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
However, my DataGrid has set the ReadOnly property to true. In my other
DataGrid that is used for a different purpose, I was able to solve it by
setting the CurrentSelectedRow property to 0 before removing the
elements, but this does not work in this situation.
I am not sure if it is related to this, but the only event I am handling
for this DataGrid is the MouseUp event, in order to get it to select the
entire row when a cell is clicked:
// Select the entire row when the user clicks on a cell in the row
System::Void datagrid_MouseUp(System::Object* sender, System::Windows::Forms::MouseEventArgs* e)
{
using System:rawing:oint;
using System::Windows::Forms:ataGrid;
using System::Windows::Forms:ataGridCell;
Point pt = Point(e->X, e->Y);
DataGrid* datagrid = __try_cast(sender);
DataGrid::HitTestInfo* hti = datagrid->HitTest(pt);
if (hti->Type == DataGrid::HitTestType::Cell) {
datagrid->CurrentCell = DataGridCell(hti->Row, hti->Column);
datagrid->Select(hti->Row);
}
}