Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Select multiple rows in a C# DataGridView programmatically?

Posted on 2013-12-09
8
5,965 Views
Last Modified: 2013-12-11
I need to select multiple rows in a DataGridView (MultiSelect = true, SelectionMode = RowHeaderSelect). From the user interface there is no problem.  I simply hold down the <CTRL> key and click on all of the row headers one-by-one.  When I click on a row, all of the cells in the row are redrawn with a blue background indicating that they have been selected and the arrow that appears in the row header column is updated so that it is in the last row header that I clicked.

When attempting to replicate this behavior programmaticly I run into a problem.  The code shown below is supposed to do this - first it fills the DataGridView with data (the DataGridView is not DataBound) and second it selects all of the rows.  The System.Diagnostics lines output what I would expect when there are 3 rows in the DataGridView; before = 0 and after = 3. However, on the UserInterface the background of the cells in the first row are blue but the cells in the second and third rows are white and the triangle is still sitting in the row header of the first row.  I expected it to be in the row header of the third row.

I tried to select all cells of all rows using the line that has been commented-out in the second loop of the code below but that didn't solve my problem.

Any thoughts on how to fix this (without making the DataGridView DataBound)?

Thanks.

public void                     displayMeasurements(List<clsMeasurement> L_Msrmt)
{
     DGV.Rows.Clear();
     foreach (clsMeasurement Msrmt in L_Msrmt)
     {
          int row = DGV.Rows.Add();

          DGV.Rows[row].Cells["col_x"].Value = Msrmt.x;
          DGV.Rows[row].Cells["col_y"].Value = Msrmt.y;
          DGV.Rows[row].Cells["col_x"].Value = Msrmt.y;
          DGV.Rows[row].Tag                           = Msrmt;
     }

     System.Diagnostics.Trace.WriteLine("before = " + DGV.SelectedRows.Count.ToString());
     DGV.ClearSelection();
     foreach (DataGridViewRow r in DGV.Rows)
     {
          r.Selected = true;
          //foreach (DataGridViewCell c in r.Cells) c.Selected = true;
     }
     System.Diagnostics.Trace.WriteLine("after = " + DGV.SelectedRows.Count.ToString());
}
0
Comment
Question by:e_livesay
  • 4
  • 2
  • 2
8 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39708719
Mmmm.... strange. I've tried to replicate your issue but in my case all the rows are selected well. The background of all cells in all rows is blue...

Can you post all the properties of your DataGridView as shown in the [formname].designer.cs file?

Thanks.
0
 

Author Comment

by:e_livesay
ID: 39708814
Roland,

   Odd stuff.  I have tried this on a Windows XP machine and a Windows 7 machine and on both of them I am getting the same results - only the cells of the first row have blue backgrounds.

   Below are the settings from the [UserControl].Designer.cs file.

   Thanks for the feedback.

//
// DGV
//
this.DGV.AllowUserToAddRows = false;
this.DGV.AllowUserToDeleteRows = false;
this.DGV.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.DGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.DGV.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.col_modfile,
this.col_Angle,
this.col_RMS_fitError});
this.DGV.ContextMenuStrip = this.contextMenuStrip1;
this.DGV.Location = new System.Drawing.Point(3, 3);
this.DGV.Name = "DGV";
this.DGV.Size = new System.Drawing.Size(732, 376);
this.DGV.TabIndex = 0;
this.DGV.CellValidated += new System.Windows.Forms.DataGridViewCellEventHandler(this.DGV_CellValidated);
this.DGV.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.DGV_CellValidating);
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39708833
Same results even applying all your properties to my grid. All rows are selected well. Sorry, I don't know what to do more.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 40
ID: 39709049
The MultiSelect property of the grid should be True in order to be able to select in more than one row.

And instead of looping through the cells, simply call the SelectAll method of the grid.
0
 

Author Comment

by:e_livesay
ID: 39709486
Thanks for the feedback.

The MultiSelect property is set to true when I look at the Property page for the DataGridView (i.e., in the MSVS 2010 Designer).  I suppose that the value of the MultiSelect proeprty does not appear in the Designer.cs file because 'true' is the default value for the MultiSelect property.  Similarly, the Enabled property is true in the Property page but it does not appear in Designer.cs.

Actually, I'm not looping through the cells.  I tried doing so at one point but that didn't help matters.

James - does the example code above work for you like it does for Roland?
0
 
LVL 40
ID: 39709565
You are right, default values do not need to be set and thus are not defined in Designer.cs.

Have you tried SelectAll?

Have you search your code in case the MultiSelect property is changed in the code somewhere?

Sorry, I do not have any unbound DataGridView in my environment right now and do not have time to set up a test.
0
 

Author Comment

by:e_livesay
ID: 39712826
Thanks for all the help.  The issue has turned out to not be with the code that I originally submitted at all but with the way that I was using it.  Since Roland Deschain confirmed that the code was working in his initial replies I'm going to award the points to him.

Below are the details of what was going on.

The displayMeasurements(List<clsMeasurement> L_Msrmt) routine shown in my first post resides in a UserControl, UC_Bar.  An instance of UC_Bar sits on a Form that was opened using the following code:

frmFoo f = new frmFoo();
f.Initialize(L_Measurements);   // L_Measurements is of type List<clsMeasurement>
f.ShowDialog();

The Initialize(...) routine of frmFoo calls UC_Bar's displayMeasurements(...) routine (i.e., the one shown in my first post) which I thought would lead to all rows being selected.  Apparently calling ShowDialog() undid the selection of all except the first row.

To fix the issue I did a few things.  First, I got rid of the Initialize(...) routine altogether and added a public List<clsMeasurements> named myMeasurements to frmFoo.  This allowed the above code to be replaced with the following:

frmFoo f = new frmFoo();
f.myMeasurements = L_Measurements;
f.ShowDialog();

I also added a 'Load' EventHandler to frmFoo which initializes the instance of UC_Bar that sits on frmFoo:

private void frmFoo_Load(object sender, EventArgs e)
{
   uC_Bar.displayMeasurements(myMeasurements);
}
0
 

Author Closing Comment

by:e_livesay
ID: 39712835
I very much appreciated Roland's effort to test the code snippet that I submitted - it was very useful confirmation that forced me to find the source of the problem which happened to not be in the code that I submitted at all.  Thanks for the help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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