Help using DataGrid
-
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 totrue
. 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::Drawing::Point;
using System::Windows::Forms::DataGrid;
using System::Windows::Forms::DataGridCell;Point pt = Point(e->X, e->Y); DataGrid\* datagrid = \_\_try\_cast(sender); DataGrid::HitTestInfo\* hti = datagrid->HitTest(pt); i
-
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 totrue
. 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::Drawing::Point;
using System::Windows::Forms::DataGrid;
using System::Windows::Forms::DataGridCell;Point pt = Point(e->X, e->Y); DataGrid\* datagrid = \_\_try\_cast(sender); DataGrid::HitTestInfo\* hti = datagrid->HitTest(pt); i
ricecake wrote:
/* reset DataSource to refresh the display */
datagrid_search_results->DataSource = 0;
datagrid_search_results->DataSource = search_results_list;So, the solution was to refresh the currency manager after resetting the data source by adding the following two lines:
CurrencyManager* cm = __try_cast(BindingContext->Item[search_results_list]);
cm->Refresh();-- Marcus Kwok