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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:53:34+00:00 2026-06-05T02:53:34+00:00

If I select more then one cell in a DataGridView, how can I get

  • 0

If I select more then one cell in a DataGridView, how can I get (in a MsgBox) the value of the cell with the highest ColumnIndex (amongst the selected) ?

List<string> a = new List<string>();

foreach (DataGridViewCell cell in dgvC.SelectedCells)
{
    a.Add(cell.Value.ToString());
}

So, I need the rest of cells in a row to populate by repeating the list made by selecting some cells.

  • 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-05T02:53:35+00:00Added an answer on June 5, 2026 at 2:53 am

    If I select more then one cell in a DataGridView, how can I get (in a MsgBox) value of the cell with the highest ColumnIndex (among selected)

    One way to do it is to use linq to order the collection of selected cells by ColumnIndex and then get the first one.

    var selectedCells =dgvC.SelectedCells;
    
    var MaxColumn = (from DataGridViewCell cell in selectedCells
                        orderby cell.ColumnIndex descending
                        select cell).FirstOrDefault();
    
    MessageBox.Show(MaxColumn.Value.ToString());    
    

    You should note that depending on how the user made the selection (top to bottom or bottom to top) you will either get the value from the first row or the last row. If that matters you should also order by RowIndex

    Another way is to examine the first and last cells in the collection and compare the indexes . We need to look at both because depending on how the user made the selection the one with the greater column index will change.

    var selectedCells = dgvc.SelectedCells;
    
    var cellStart = selectedCells[0];
    var cellEnd = selectedCells[selectedCells.Count -1];
    
    if (cellStart.ColumnIndex > cellEnd .ColumnIndex)
        MessageBox.Show(cellStart.Value.ToString());
    else
        MessageBox(cellEnd.Value.ToString());
    

    Note: you should check for Null values if AllowUsersToAddRow is true

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

Sidebar

Related Questions

I want to generate a grid where people can select one or more cells
I'd like to select all rows from one table which match one or more
guys i have to select every employees,who has salary more then avarage salary in
I use following code to select file but i can select one file only.
I have a select box which has more than 200 items. Below the select
I like to select an element by more than two condition. for example name
which will take more execution time insert operation or select operation if both are
which is more efficient (when managing over 100K records): A. Mysql SELECT * FROM
I am looking for a more jquery/cleaner way to be able to select all
I have a function that, when the user selects one or more items, it

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.