This is a migrated thread and some comments may be shown as answers.

CellFormatting + CellMouseMove bug?

1 Answer 108 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Cameron Hart
Top achievements
Rank 1
Cameron Hart asked on 15 Apr 2009, 05:08 AM
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:
            GridViewTextBoxColumn lengthColumn = new GridViewTextBoxColumn(); 
            lengthColumn.UniqueName = "LengthColumn"
            lengthColumn.HeaderText = "Length"
            lengthColumn.DataType = typeof(Int32); 
            lengthColumn.FieldName = "itemLength"
            lengthColumn.FormatString = "{0}cm"
            this.gvwManifestLines.Columns.Insert(i++, lengthColumn); 
 
            GridViewTextBoxColumn widthColumn = new GridViewTextBoxColumn(); 
            widthColumn.UniqueName = "WidthColumn"
            widthColumn.HeaderText = "Width"
            widthColumn.DataType = typeof(Int32); 
            widthColumn.FieldName = "itemWidth"
            widthColumn.FormatString = "{0}cm"
            this.gvwManifestLines.Columns.Insert(i++, widthColumn); 
 
            GridViewTextBoxColumn heightColumn = new GridViewTextBoxColumn(); 
            heightColumn.UniqueName = "HeightColumn"
            heightColumn.HeaderText = "Height"
            heightColumn.DataType = typeof(Int32); 
            heightColumn.FieldName = "itemHeight"
            heightColumn.FormatString = "{0}cm"
            this.gvwManifestLines.Columns.Insert(i++, heightColumn); 

I am handling that gridview's CellFormatting event as follows:
        private void gvwManifestLines_CellFormatting(object sender, CellFormattingEventArgs e) 
        { 
            GridViewCellInfo cellInfo = this.gvwManifestLines.Rows[e.CellElement.RowIndex].Cells[e.CellElement.ColumnIndex]; 
 
            if (e.CellElement.ColumnIndex == this.gvwManifestLines.Columns["LengthColumn"].Index || 
                e.CellElement.ColumnIndex == this.gvwManifestLines.Columns["WidthColumn"].Index || 
                e.CellElement.ColumnIndex == this.gvwManifestLines.Columns["HeightColumn"].Index) 
            { 
                if (cellInfo.Value == null || 
                    cellInfo.Value is DBNull || 
                    (int)cellInfo.Value <= 0) 
                { 
                    e.CellElement.FormatString = "n/a"
                } 
            }              
        } 

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.

1 Answer, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 22 Apr 2009, 12:09 PM
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,
Nikolay
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.
Tags
GridView
Asked by
Cameron Hart
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Share this question
or