Normal
0
false
false
false
EN-US
X-NONE
X-NONE
Code Query Language (CQL)
can be used to write all sorts of code convention. I’ve described some CQL range
of usage,
and as an illustration I also exposed some CQL rules dedicated to the .NET Framework usage.
We discovered a weird issue in our
last NDepend version: to select programmatically a particular row in a Windows
Form DataGridView you need to call the setter DataGridView.FirstDisplayedCell. However if the DataGridView height is too small and
doesn’t let show any row, then an exception will be raised:
Exception.Type {System.InvalidOperationException}
Exception.Message {No room is available to display rows.}
Exception.StackTrace {
at
System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingRowIndex(Int32
value)
at System.Windows.Forms.DataGridView.set_FirstDisplayedCell(DataGridViewCell
value)
…
IMHO this behavior is so
counter-intuitive and prone to error that I would qualify it as a .NET
Framework bug. But Microsoft is not willing to fix such behavior in the name of
ascendant compatibility. The solution is then to capitalize on
your own knowledge database. Typically we included the following CQL rule in
our set of rules. It warns as soon as there is a method that doesn’t check the rows
available height before setting FirstDisplayedCell.
// <Name>Check height space before setting FirstDisplayedCell
of a DataGridView</Name>
SELECT METHODS WHERE
IsDirectlyUsing "System.Windows.Forms.DataGridView.set_FirstDisplayedCell(DataGridViewCell)" AND
!(IsDirectlyUsing "System.Windows.Forms.Control.get_Height()" AND
IsDirectlyUsing "System.Windows.Forms.DataGridView.get_ColumnHeadersHeight()")
/* The pattern implementation is:
if
((rowToSelect.State & DataGridViewElementStates.Displayed) == 0) {
if
(this.Height - this.ColumnHeadersHeight > 0) {
this.FirstDisplayedCell = rowToSelect.Cells[0];
}
}
*/
We immediately spotted 3 others
risky areas in our code.
Posted
Sun, Jun 21 2009 11:26 AM
by
Patrick Smacchia