Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Help using DataGrid

Help using DataGrid

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasegraphicshelpdata-structures
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ricecake
    wrote on last edited by
    #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::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
    
    R 1 Reply Last reply
    0
    • R ricecake

      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::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
      
      R Offline
      R Offline
      ricecake
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups