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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. DataGridView Access Problem [modified]

DataGridView Access Problem [modified]

Scheduled Pinned Locked Moved Database
csharpdatabasehelpvisual-studiodata-structures
4 Posts 2 Posters 1 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.
  • G Offline
    G Offline
    Grollub
    wrote on last edited by
    #1

    Hello, I have been working with VB ADO for some time using Visual Studio 2003 and created several applications connecting to Excel with ADO. However, I have moved to Visual Studio 2008 now and have run into some trouble with the new DatagridView in the latest .NET version. Let me step through the process. Upon 'Form_Load', I connect to my database, fill the dataset, bind it to my DataGridView in a different Form named, which is publicly defined and created upon startup. I utilize the DataGridView values to populate several text boxes and richtextboxes based upon the data. This is all working great. The problem came when I tried to create other controls to manipulate this datagridView. The lines I am using to access this are: Form2.DataGridView1.Item("ColumnName", CurrentRecordRow).Value() Whenever I try this same line of code through a different subroutine (a button click for example) I get an index out of array error. Through some troubleshooting, I have managed to discover that the 'RowCount' property of the DataGridView changes to '0' when outside the Form_load subroutine where I populate the DataGridView from the created dataset. (in the FormLoad, the rowcount is 236, when I click a button that shoots a messagebox with the Rowcount again, it says its 0) This explains the 'index' fatal error generated, but not why the data seems suddenly 'gone' once the form has loaded? Can anyone make a recommendation of what I may be doing wrong in accessing the data within this DataGridView? thanks, Grollub

    modified on Thursday, October 8, 2009 3:35 PM

    M 1 Reply Last reply
    0
    • G Grollub

      Hello, I have been working with VB ADO for some time using Visual Studio 2003 and created several applications connecting to Excel with ADO. However, I have moved to Visual Studio 2008 now and have run into some trouble with the new DatagridView in the latest .NET version. Let me step through the process. Upon 'Form_Load', I connect to my database, fill the dataset, bind it to my DataGridView in a different Form named, which is publicly defined and created upon startup. I utilize the DataGridView values to populate several text boxes and richtextboxes based upon the data. This is all working great. The problem came when I tried to create other controls to manipulate this datagridView. The lines I am using to access this are: Form2.DataGridView1.Item("ColumnName", CurrentRecordRow).Value() Whenever I try this same line of code through a different subroutine (a button click for example) I get an index out of array error. Through some troubleshooting, I have managed to discover that the 'RowCount' property of the DataGridView changes to '0' when outside the Form_load subroutine where I populate the DataGridView from the created dataset. (in the FormLoad, the rowcount is 236, when I click a button that shoots a messagebox with the Rowcount again, it says its 0) This explains the 'index' fatal error generated, but not why the data seems suddenly 'gone' once the form has loaded? Can anyone make a recommendation of what I may be doing wrong in accessing the data within this DataGridView? thanks, Grollub

      modified on Thursday, October 8, 2009 3:35 PM

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      1 Excel is NOT a database and should not be used as a database. I recommend that you change to either XML files for VERY small volumes or SQL Server/Express for more robust data storage. Take Excel out of the design. 2 When manipulating the data in a datagridview you work with the underlying data container (datatable probably) 3 Check the CurrentRecordRow using a break point, it is probably not set

      G 1 Reply Last reply
      0
      • M Mycroft Holmes

        1 Excel is NOT a database and should not be used as a database. I recommend that you change to either XML files for VERY small volumes or SQL Server/Express for more robust data storage. Take Excel out of the design. 2 When manipulating the data in a datagridview you work with the underlying data container (datatable probably) 3 Check the CurrentRecordRow using a break point, it is probably not set

        G Offline
        G Offline
        Grollub
        wrote on last edited by
        #3

        Thanks very much for the reply. 1.) I agree whole-heartedly. Excel is not a database, I am just trying to get something up and running while I learn MySQL and implement. The spreadsheet is already in place, and I had hoped to get something quick and dirty up and running for those who utilize it... and then come back later and move the program to SQL. (under a time crunch). 2.) In this case, I have a dataset.. should I just move through the dataset itself (make this a public variable?) and just use the DataGridView to display the data? 3.) I checked this... and I set the value right above this... I have checked it using a breakpoint and the index I provide is valid... its just the datagridView that seems to 'lose' rows. Could this be a private vs. public variable issue?

        M 1 Reply Last reply
        0
        • G Grollub

          Thanks very much for the reply. 1.) I agree whole-heartedly. Excel is not a database, I am just trying to get something up and running while I learn MySQL and implement. The spreadsheet is already in place, and I had hoped to get something quick and dirty up and running for those who utilize it... and then come back later and move the program to SQL. (under a time crunch). 2.) In this case, I have a dataset.. should I just move through the dataset itself (make this a public variable?) and just use the DataGridView to display the data? 3.) I checked this... and I set the value right above this... I have checked it using a breakpoint and the index I provide is valid... its just the datagridView that seems to 'lose' rows. Could this be a private vs. public variable issue?

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Take a look at this article[^], it is designed to service the user control but may give you some ideas around the DGV, it uses an XML datasource. Also look into the use of the bindingsource in the article, adds another layer but is quite useful. Remember do NOT allow your users to do inline editing in a list control (DGV, listview). I force the user to a dialog for CRUD, supply a toolstrip with Add/Edit/Delete, double click the grid also does Edit. Pop a dialog with selected or blank record for the add/edit, write back to your data store before closing the dialog and reload the grid on a successful CRUD. This may not suit the purist because there are multiple I/O to the database but it works very well and you can never lost data.

          Never underestimate the power of human stupidity RAH

          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