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 8278885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:08:58+00:00 2026-06-08T09:08:58+00:00

I am working on a datagrid and on cellClick event get data on the

  • 0

I am working on a datagrid and on cellClick event get data on the relevent texbox for editing.
When I click on the row it works fine but when I click on any of the column it gives exception and I dont know why.

Here’s my code :

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    itmId.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
    itmNme.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
    untCst.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
    qntty.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
    manfDate.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
}
  • 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-06-08T09:08:59+00:00Added an answer on June 8, 2026 at 9:08 am

    When you click on the header of the table, it sends the row index of -1. There are other instances where this event can fire with invalid rowindex and columnindex values.

    You should test rowindex and columnindex before using them.

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
        if (e.RowIndex >= 0 && e.ColumnIndex >= 0) {
            // .. my code goes here ..
        }
    }
    

    In some cases, you may also need to test whether the rowindex and columnindex go beyond the limits of the backing data.

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

Sidebar

Related Questions

I've List and i bind these list to datagrid that is working fine, but
I'm trying to get this code working, any help would be greatly appreciated. <mx:DataGrid
im using the WPFToolkit's DataGrid and im trying to get an edit button working,
I'm trying to create a DataGrid dynamically. Declarative creation is working fine: A source
I am working in Flex3. Here I have a datagrid which contains data. some
While working with an excel file (.xlsx), the data I get from it is
I have a working dataset and datagrid already in my project, but I want
I am experiencing some very strange mouse-event behaviour when working with Silverlights DataGrid: What
I'm working with textbox in a datagrid. Before saving the data into the database,
I am working on Datagrid RowHeader mouse over thing. How would I get DataGridRowHeader

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.