getting error when trying to read from DataGridView with event handler winform

Terran28

I have a winform that has a gridview that I am applying data to via a Dataset. When the data binds, it calls the SelectionChanged event handler. I researched that and found a way around it by adding an if clause to see if the DGV has focus (all other resolutions did not work that I found). That part is working as planned. When I step through the program, the event handler tries to go through the code 3 times when it binds the data. The if clause stops it from reaching the code. My issue is after the data binds and I then choose a row in the DGV, the event handler then throws "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll". When stepping through the code, the DGV is returning the proper row index to my 'int row' variable, but the code I use to get the row/cell info throws the error before it applies it to the 'loadtableID' variable. I need help. You can ignore the second DGV in the top. It takes the selected row's info and gets another DB table info. Also, if it helps, I did not apply a datasource to the program or create datasets for each individual dataset that is returned, I am using the system's generic Dataset when returning data.

     private void gvMainSelectResults_SelectionChanged(object sender, EventArgs e)
    {
        if (gvMainSelectResults.Focused)
        {
            gvMainArchiveResults.DataSource = null;  //second DGV that is populated later and everytime is cleared with a new selection

            loadTableID = 0;
            orgID = 0;
            dbFileName = "";
            sourceType = "";

            int row = gvMainSelectResults.CurrentCell.RowIndex;
            loadTableID = Convert.ToInt32(gvMainSelectResults.SelectedRows[row].Cells["LoadTableID"].Value);  //this is where I get the error, even if the "int row" has the correct index number
            orgID = Convert.ToInt32(gvMainSelectResults.SelectedRows[row].Cells["OrganizationID"].Value);
            dbFileName = Convert.ToString(gvMainSelectResults.SelectedRows[row].Cells["FileName"].Value);
            sourceType = Convert.ToString(gvMainSelectResults.SelectedRows[row].Cells["SourceType"].Value);

            more code here...
Steve

You are using the RowIndex value to get your text from the SelectedRows collection.
But this collection contains only

Gets the collection of rows selected by the user.

This means that the collection contains only a subset of the rows present in your grid. When RowIndex is 2 and you have just one row in the SelectedRows collection you get the OutOfRange exception.

With the RowIndex value you should refer to the Rows collection instead

loadTableID = Convert.ToInt32(gvMainSelectResults.Rows[row].Cells["LoadTableID"].Value);
orgID = Convert.ToInt32(gvMainSelectResults.Rows[row].Cells["OrganizationID"].Value);
dbFileName = Convert.ToString(gvMainSelectResults.Rows[row].Cells["FileName"].Value);
sourceType = Convert.ToString(gvMainSelectResults.Rows[row].Cells["SourceType"].Value);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error when trying to read to read from Entry

From Dev

Getting an error when trying to read all rows of a recordset

From Dev

Getting a Error: Endpoint read failed when trying to deploy a .bna

From Dev

Deadlock when trying to display text while data read in DataReceived event handler of serialport

From Dev

Getting the error 'Event handler '' not found on class '' '

From Dev

getting "fatal error: NSArray element failed to match the Swift Array Element type" when trying to read values from an array of class objects

From Dev

getting "fatal error: NSArray element failed to match the Swift Array Element type" when trying to read values from an array of class objects

From Dev

Getting Event Handler from Instance of Class

From Dev

getting object properties from a event handler function

From Dev

Getting error when trying to delete record from hasMany collection

From Dev

I'm getting error when trying to import reserve from urlresolver

From Java

Error when trying to read excel file from web site

From Dev

C Readfile error 87 when trying to read from mailslot

From Dev

out of range error when trying to read from a file?

From Dev

C Readfile error 87 when trying to read from mailslot

From Dev

Error: NoSuchElementException when trying to read values from file to array

From Dev

object has no attribute error when trying to read from a file

From Dev

XamlParseException when trying to bind subclass Loaded event handler

From Dev

Getting error "Cannot read property 'show' of null" in console when trying to show images with jQuery

From Dev

Read response headers from image error handler

From Dev

Read response headers from image error handler

From Dev

Getting the id of a table row and passing it to a event handler when a checkbox is checked

From Dev

Getting data from event args in ListViewItem click handler

From Dev

java.net.SocketTimeoutException: Read timed out error when trying to read from table

From Dev

Error when trying to select event data with BigQuery

From Dev

Getting Internal Error when trying Resque in Heroku

From Dev

Getting error when trying to use hasNext in Java

From Dev

VBA Getting an error when trying to move a chart

From Dev

Getting a heap error when trying to free

Related Related

  1. 1

    Error when trying to read to read from Entry

  2. 2

    Getting an error when trying to read all rows of a recordset

  3. 3

    Getting a Error: Endpoint read failed when trying to deploy a .bna

  4. 4

    Deadlock when trying to display text while data read in DataReceived event handler of serialport

  5. 5

    Getting the error 'Event handler '' not found on class '' '

  6. 6

    getting "fatal error: NSArray element failed to match the Swift Array Element type" when trying to read values from an array of class objects

  7. 7

    getting "fatal error: NSArray element failed to match the Swift Array Element type" when trying to read values from an array of class objects

  8. 8

    Getting Event Handler from Instance of Class

  9. 9

    getting object properties from a event handler function

  10. 10

    Getting error when trying to delete record from hasMany collection

  11. 11

    I'm getting error when trying to import reserve from urlresolver

  12. 12

    Error when trying to read excel file from web site

  13. 13

    C Readfile error 87 when trying to read from mailslot

  14. 14

    out of range error when trying to read from a file?

  15. 15

    C Readfile error 87 when trying to read from mailslot

  16. 16

    Error: NoSuchElementException when trying to read values from file to array

  17. 17

    object has no attribute error when trying to read from a file

  18. 18

    XamlParseException when trying to bind subclass Loaded event handler

  19. 19

    Getting error "Cannot read property 'show' of null" in console when trying to show images with jQuery

  20. 20

    Read response headers from image error handler

  21. 21

    Read response headers from image error handler

  22. 22

    Getting the id of a table row and passing it to a event handler when a checkbox is checked

  23. 23

    Getting data from event args in ListViewItem click handler

  24. 24

    java.net.SocketTimeoutException: Read timed out error when trying to read from table

  25. 25

    Error when trying to select event data with BigQuery

  26. 26

    Getting Internal Error when trying Resque in Heroku

  27. 27

    Getting error when trying to use hasNext in Java

  28. 28

    VBA Getting an error when trying to move a chart

  29. 29

    Getting a heap error when trying to free

HotTag

Archive