Agile Behavior: Nurture Knowledge Database




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_FirstDisplayedCell(DataGridViewCell


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

// <Name>Check height space before setting FirstDisplayedCell
of  a DataGridView


  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:

((rowToSelect.State & DataGridViewElementStates.Displayed) == 0) {

(this.Height – this.ColumnHeadersHeight > 0) {

this.FirstDisplayedCell = rowToSelect.Cells[0];





We immediately spotted 3 others
risky areas in our code.


This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Martin Harris

    I have been looking for a .NET equivalent to Sonar:

    This project provides similar metrics to NDepend, but its real advantage is its ability to surface the data in different ways. There is a portal that collects a view across projects. This also tracks history and records all the metrics in terms of historic change.

    Then you can see the results for a single project in your build.

    Finally IDE plugins bring a similar experience to the facilities provided by NDepend in Visual studio.

    Do you have any plans to take NDepend in this direction?

  • Bita

    Hi there!
    I’ve been struggling with this problem for 3-4 days. There’s no solution on the web, but the one here.
    But unfortunately I can’t get the point! How should I change the DataGridView height to the size it fits? How can I finally resolve the problem?
    Thanks for your help in advance.

  • Doug B

    Thanks for this great solution! I’m glad to have this check rather than catching and ignoring the exception (slow). However, I believe you want to check for Visible, not Displayed, and the correct comparison would be:
    if ((rowToSelect.State & DataGridViewElementStates.Visible) == DataGridViewElementStates.Visible)