Solved

.NET GridView SelectedRows - Set it to Nothing (No selection)

Posted on 2010-11-23
6
345 Views
Last Modified: 2012-06-27
I am performing a text search in the cells of the GridView.  As I enumerate the cells, I set the row visibility to false.  However, the 'selectedrow' can never be set to false.  How do I make SelectedRows empty.  Nada.  And this is not a webcontrol, where you can set SelectedIndex to -1.
0
Comment
Question by:jigdog
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34200132
Are you using a foreach? If so, you cannot modify the collection referenced in the foreach. You could use a while loop:
while (this.dataGridView1.SelectedRows.Count > 0)
{
    foreach (DataGridViewCell cell in this.dataGridView1.SelectedRows[0].Cells)
    {
        // search cell
    }

    this.dataGridView1.SelectedRows[0].Selected = false;
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34200140
>>  you cannot modify the collection referenced in the foreach

I should state this as, "you cannot remove/add elements to the collection referenced in the foreach within the foreach itself."
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34200153
Use the Grid's ClearSelection() method
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jigdog
ID: 34200242
It is still not effective.  Try this:  if you enumerate all rows in a datagrid, setting them to Visible = False ... the first row (or whichever row has the carat beside it)  will still show.  How do we prevent this?
0
 

Accepted Solution

by:
jigdog earned 0 total points
ID: 34200291
THIS DID IT:


YourGridView.CurrentCell = Nothing
0
 

Author Closing Comment

by:jigdog
ID: 34224661
Solved!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now