CellFormatting + CellMouseMove bug?

2 posts, 0 answers
  1. Cameron Hart
    Cameron Hart avatar
    17 posts
    Member since:
    May 2008

    Posted 15 Apr 2009 Link to this post

    Hey guys,

    First and formost, this may not be an actual bug - there's a good chance I'm doing something a little bit abnormal.

    I have a RadGridView (named gvwManifestLines) with these three columns:
    1             GridViewTextBoxColumn lengthColumn = new GridViewTextBoxColumn(); 
    2             lengthColumn.UniqueName = "LengthColumn"
    3             lengthColumn.HeaderText = "Length"
    4             lengthColumn.DataType = typeof(Int32); 
    5             lengthColumn.FieldName = "itemLength"
    6             lengthColumn.FormatString = "{0}cm"
    7             this.gvwManifestLines.Columns.Insert(i++, lengthColumn); 
    9             GridViewTextBoxColumn widthColumn = new GridViewTextBoxColumn(); 
    10             widthColumn.UniqueName = "WidthColumn"
    11             widthColumn.HeaderText = "Width"
    12             widthColumn.DataType = typeof(Int32); 
    13             widthColumn.FieldName = "itemWidth"
    14             widthColumn.FormatString = "{0}cm"
    15             this.gvwManifestLines.Columns.Insert(i++, widthColumn); 
    17             GridViewTextBoxColumn heightColumn = new GridViewTextBoxColumn(); 
    18             heightColumn.UniqueName = "HeightColumn"
    19             heightColumn.HeaderText = "Height"
    20             heightColumn.DataType = typeof(Int32); 
    21             heightColumn.FieldName = "itemHeight"
    22             heightColumn.FormatString = "{0}cm"
    23             this.gvwManifestLines.Columns.Insert(i++, heightColumn); 

    I am handling that gridview's CellFormatting event as follows:
    1         private void gvwManifestLines_CellFormatting(object sender, CellFormattingEventArgs e) 
    2         { 
    3             GridViewCellInfo cellInfo = this.gvwManifestLines.Rows[e.CellElement.RowIndex].Cells[e.CellElement.ColumnIndex]; 
    5             if (e.CellElement.ColumnIndex == this.gvwManifestLines.Columns["LengthColumn"].Index || 
    6                 e.CellElement.ColumnIndex == this.gvwManifestLines.Columns["WidthColumn"].Index || 
    7                 e.CellElement.ColumnIndex == this.gvwManifestLines.Columns["HeightColumn"].Index) 
    8             { 
    9                 if (cellInfo.Value == null || 
    10                     cellInfo.Value is DBNull || 
    11                     (int)cellInfo.Value <= 0) 
    12                 { 
    13                     e.CellElement.FormatString = "n/a"
    14                 } 
    15             }              
    16         } 

    Essentially, I am using the length, width, and height values to calculate the volume per line in the volume column (omitted). I am doing a calculation in the grid's CellEndEdit for the length, width, height, and volume columns that will allow the user to either a) fill in length, width, and height to calculate the volume, or b) just enter in the volume and have the length, width and height ignored.

    My implementation for this is to treat a value of '0' in the length, width or height column as a signifier that the calculation should ignore those three fields. However, it is confusing to have 0cm * 0cm * 0cm = 1000cm^2, so in the case of 0cm I want the display format to show as 'n/a'. It has a nicer feel.

    This all works fine. The only problem is that when I pass the mouse over a cell that contains an 'n/a', the cell reverts back to '0cm' again until I click somewhere in the grid. There's a good change that the user will be doing a lot of back-and-forth waiving of the mouse around in the app, and this just looks sloppy.

    What I wanted to do was to move my code in the CellFormatting handler into a private method, and then recall this method from within the CellMouseMove event. However, I am unsure how to use the MouseEventArgs to extract the relevant cell's row and column index information from the grid.

    Additionally, there could be a better solution to this problem - I'm all ears!

    Essentially, this isn't mission critical. It can wait for a bit, so I'm not going to be too worried about tinkering my way to a solution. I figure I'd just post here and see what comes up.
  2. Nikolay
    Nikolay avatar
    1805 posts

    Posted 22 Apr 2009 Link to this post

    Hello Cameron Hart,

    Thank you for the question and please excuse me for the delayed response.

    You get "0" text on MouseHover, because you are setting the FormatString property. Instead, you should set the Text property, which is the correct approach:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
        GridViewCellInfo cellInfo = e.CellElement.RowInfo.Cells[e.CellElement.ColumnIndex];  
        if (e.CellElement.ColumnIndex == this.radGridView1.Columns["LengthColumn"].Index ||  
                    e.CellElement.ColumnIndex == this.radGridView1.Columns["WidthColumn"].Index ||  
                    e.CellElement.ColumnIndex == this.radGridView1.Columns["HeightColumn"].Index)  
            if (cellInfo.Value == null ||  
                 cellInfo.Value is DBNull ||  
                 (int)cellInfo.Value <= 0)  
                e.CellElement.Text = "n/a";  

    I hope this helps. If you have additional questions, feel free to contact me.

    Kind regards,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top