Skip to content

DataGridView.SelectionChanged appears broken

brice

Asked Sep 25, 2010, 10:14 AM EDT2 answers
Share
LinkedInX
The DataGridView.SelectionChanged event appears broken in two places:
 
First it reports CurrentRow.Selected = false always.
    The past tense SelectionChanged should be called
    after the selection is done changing completely.
 
Second, we get two different results if we change the
    row by clicking in the datagrid or pressing buttons
    and changing the row programatially.  If we click
    in the datagrid to change the row, the RowIndex is set
    to the clicked row.  However, if we change the row
    programatically, the RowIndex is set to the row we
    are leaving.


Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Tester
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
         this.Load += new System.EventHandler(Form1_Load);
      }
      protected void Form1_Load(object sender, System.EventArgs e)
      {
         // Add five rows two columns
         for ( int x = 0; x < 5; x++ )
         {
            dataGridView1.Rows.Add("Row: " + x.ToString() + "  Col: 1",
                                                     "Row: " + x.ToString() + "  Col: 2");
         }

      }

      private void dataGridView1_SelectionChanged(object sender, EventArgs e)
      {
         String selected = dataGridView1.CurrentRow.Selected ? "True" : "False";
         int rowIndex = dataGridView1.CurrentCell.RowIndex;

         Debug.WriteLine("RowIndex: " + rowIndex.ToString() );
         Debug.WriteLine("selected: " + selected );
         Debug.WriteLine("=========================================");

      }

      private void button1_Click(object sender, EventArgs e)
      {
         dataGridView1.CurrentCell = dataGridView1[0, 3];
      }

      private void button2_Click(object sender, EventArgs e)
      {
         dataGridView1.CurrentCell = dataGridView1[0, 2];
      }

   }
}

Code

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I'm a bit further along with this. 

    DataGridView.RowEnter happens before DataGridView.SelectionChanged so it is worthless for what I need
    DataGridView.SelectionChanged appears to be the last event called on a row change.  Please let me know if anyone knows of a later event.
    DataGridView.SelectionChanged has the new dataGridView1.CurrentCell.RowIndex if the user clicked on a new row in the DataGridView
    DataGridView.SelectionChanged has the old dataGridView1.CurrentCell.RowIndex if the program moves the row using:

             dataGridView1.ClearSelection();
             dataGridView1.CurrentCell = dataGridView1.Rows[3].Cells[0]; // Or whatever new row you want
             dataGridView1.Rows[3].Selected = true;

    So, I am either calling the wrong selection methods, there is a later event that can be used to get the new row, or DataGridView is broken.
    0
  • 0
    BinaryCoder on the msdn windows forms forum just answered my question.  Everything as reported seems to hold true, but you can use the SelectedRows property, even though Selected is still false, and it will hold the correct Index value of the newly selected row whether you are clicking or selecting the row programatically.
    0