Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4541804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:17:43+00:00 2026-05-21T15:17:43+00:00

I have a DataGridView populated from a database. I am trying to get the

  • 0

I have a DataGridView populated from a database.

I am trying to get the contents of the row that is selected on the RowEnter event. I have set the grid’s selection mode to FullRowSelect

I have tried the following:

int orderId = (int)dgUnprocessedCards.Rows[dgUnprocessedCards.SelectedCells[0].RowIndex].Cells[0].Value;

this keep throwing the error

Index was out of range. Must be non-negative and less than the size of the collection.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:17:44+00:00Added an answer on May 21, 2026 at 3:17 pm

    I’ve just tried this in a sample datagridview app and it works just fine so there must be something going on which you haven’t told us about.

    First thing to do is break your one big statement up into discrete smaller statements so you can see exactly where the failure is.

    You can rewrite the code above to something like this for debugging purposes:

    var cellindex = dgUnprocessedCards.SelectedCells[0].RowIndex;           
    var cellcollection = dgUnprocessedCards.Rows[cellindex].Cells[0];
    
    int orderId = (int)dgUnprocessedCards.Value;
    

    Also, you should be able to do the following to achieve what you want:

    int orderId = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
    

    That uses the SelectedRows collection which is a little bit more concise and I’d say the more usual way of accessing selected items from the datagridview.

    Finally, you probably want to do checking around your cast of the value, since the Value might not necessarily be an int. Something like:

    int orderid;
    if (!int.TryParse(cellcollection.Value.ToString(), out orderid))
    {
        // Some logic to deal with the fact that Value was not an int
    }
    

    When is the SelectionChanged event raised?

    Now – as you mention, your selection changed event fires while loading data into the grid. This doesn’t seem to cause a problem in my test version but could be part of your issue.

    Why this happens should not be related to the type of data source you are using, but to when you attach the selection changed eventhandler. This is because databinding causes a selection changed event to be raised.

    If you add an eventhandler for the DataBindingComplete event and attach your SelectionChanged or RowEnter eventhandlers there, you should not see the handler invoked during databinding.

    void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        this.dataGridView1.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_RowEnter);
        this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
    }
    

    Note that you will need to delete the designer generated event attachment and reference the designer generated methods for this to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView that is populated from a DataSet. How can I change
In my application I have a DataGridView control that displays data for the selected
I have a DataGridView that I'm trying to populate using a For loop: Dim
I have a DataGridView on my Form which is being populated with database records
I have a DataGridView that I'm populating from a list. The function that edits
I have a DataGridView bound to a DataTable. The DataTable is populated from a
I have a DataGridView with its datasource set to a generic list of custom
I have a DataGridView that I want to query using Linq (C# WinForm). I
I have a DataGridView control in a winforms app that I'm working on. The
How can I have a datagridview that will autogenerate a textbox instead of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.