Click here to Skip to main content
13,502,268 members
Rate this:
Please Sign up or sign in to vote.
Hi everyone!

I am having an issue with checking for whether or not cells/columns/rows are selected in a datagridview.

Here is my code:

bool isRowSelected = dataGridView1.Rows[e.RowIndex - 1].Selected;
            bool isColumnSelected = dataGridView1.Columns[e.ColumnIndex].Selected;

            if (((dataGridView1.CurrentCell.ColumnIndex == 0) || (dataGridView1.CurrentCell.ColumnIndex == 1) || (dataGridView1.CurrentCell.ColumnIndex == 2)
                || (dataGridView1.CurrentCell.ColumnIndex == 3) || (dataGridView1.CurrentCell.ColumnIndex == 4)) && (!isRowSelected) || (!isColumnSelected))
                this.comboBox1.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                comboBox1.Width = 100;
                this.comboBox1.SelectedValue = this.dataGridView1.CurrentCell.Value;

Expected Output:
Cell selected - combobox is supposed to be on top of the selected cell
Row selected - highlight that row and nothing else
Column selected - highlight that column (or) sort by that column

Actual Output:
Cell and Row selection - same
Column selection - this is erroring out pointing to this line.
bool isRowSelected = dataGridView1.Rows[e.RowIndex - 1].Selected;
For the reason of: Index was out of range. Must be non-negative and less than the size of the collection. (ArgumentOutOfRangeException)

I want to avoid having the combobox appear when a row or column is selected, just cell selection only. That is my intent.

Thanks everyone!
Posted 29-Jan-13 10:03am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The first bug I can see is: e.RowIndex can be 0, in this case, e.RowIndex − 1 will be −1, which is of course out of range, indexing with this index value throws ArgumentOutOfRangeException.

Fix this trivial bug first, and then see if you have other problems. :-)

joshrduncan2012 29-Jan-13 16:32pm
That fix didn't help in solving clicking on a column that will not through an ArgumentOutOfRangeException. No changes in output.
joshrduncan2012 29-Jan-13 16:45pm
It looks like if I check for both, then both of them error out.
joshrduncan2012 29-Jan-13 16:53pm
I found the issue. I had to enclose this plus a lot of other stuff inside of a try/catch and now I can select rows/columns/cells no problem. :)
Sergey Alexandrovich Kryukov 29-Jan-13 16:58pm
OK, great, but I correctly identified the bug. Will you accept the answer formally (green button)? — thanks.
joshrduncan2012 29-Jan-13 17:00pm
The answer really didn't solve what I was looking for, but since you asked nicely, I did accept anyway. :)
Sergey Alexandrovich Kryukov 29-Jan-13 17:04pm
Thank you very much. Most answers are not supposed to solve every problem to the very end, do they? Fix the critical issue, then go to the next one.
joshrduncan2012 29-Jan-13 17:08pm
My philosophy is that I normally don't accept a solution until everything is resolved, not one at a time.
Sergey Alexandrovich Kryukov 29-Jan-13 17:11pm
This is totally up to you, but my philosophy is to give the inquirers the opportunities to do as much as possible by themselves. Isn't it fair and reasonable? :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100