Search code examples
winformsexceptiondatagridvieweventhandler

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


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...

Solution

  • 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);