Another Bug in the .net datagrid ???
-
Hi! The point is that while scrolling within a datagrid, sometimes the gridloses focus with no reason because I was just scrolling.The class below simply illustrates this(while scrolling you'll get a message) Thank you in advance. ps: I'm using vs.net 2003. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace test2k3 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dg; private DataTable dt; private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // dt = new DataTable(); DataColumn dc = new DataColumn("Nr"); dt.Columns.Add(dc); for(int i = 0 ; i < 100; i++) { DataRow dr = dt.NewRow(); dr[0] = "row #"+i; dt.Rows.Add(dr); } this.dg.DataSource = dt; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.dg = new System.Windows.Forms.DataGrid(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit(); this.SuspendLayout(); // // dg // this.dg.DataMember = ""; this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dg.Location = new System.Drawing.Point(24, 16); this.dg.Name = "dg"; this.dg.Size = new System.Drawing.Size(160, 128); this.dg.TabIndex = 0; this.dg.Leave += new System.EventHandler(this.dg_Leave); // // button1 // this.button1.Location = new System.Drawing.Point(232, 64); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(56, 32); this.button1.TabIndex = 1; this.button1.Text = "button1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(328, 173); this.Controls.Add(this.button1); this.C
-
Hi! The point is that while scrolling within a datagrid, sometimes the gridloses focus with no reason because I was just scrolling.The class below simply illustrates this(while scrolling you'll get a message) Thank you in advance. ps: I'm using vs.net 2003. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace test2k3 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dg; private DataTable dt; private System.Windows.Forms.Button button1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // dt = new DataTable(); DataColumn dc = new DataColumn("Nr"); dt.Columns.Add(dc); for(int i = 0 ; i < 100; i++) { DataRow dr = dt.NewRow(); dr[0] = "row #"+i; dt.Rows.Add(dr); } this.dg.DataSource = dt; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.dg = new System.Windows.Forms.DataGrid(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit(); this.SuspendLayout(); // // dg // this.dg.DataMember = ""; this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dg.Location = new System.Drawing.Point(24, 16); this.dg.Name = "dg"; this.dg.Size = new System.Drawing.Size(160, 128); this.dg.TabIndex = 0; this.dg.Leave += new System.EventHandler(this.dg_Leave); // // button1 // this.button1.Location = new System.Drawing.Point(232, 64); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(56, 32); this.button1.TabIndex = 1; this.button1.Text = "button1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(328, 173); this.Controls.Add(this.button1); this.C
More specifically, the .Leave event is fired when the currently selected cell is scrolled out of view (up or down). Curiously, no Enter event is fired when it is scrolled back in view but a Leave *will* be fired again if you continue scrolling. And in neither case is Got/LostFocus() fired. I don't usually like to call things bugs in frameworks like this as I usually find the reason or solution as soon as I do. Depending on what you want to do when focus is lost and gained, I think you can find a pattern of events to identify this situation. For example, clicking between cells causes: GotFocus, CurrentCellChanged, LostFocus However in the scrolling scenario: Leave, Validating, Validated