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

radGridView1 CellFormatting problem

4 Answers 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bugra
Top achievements
Rank 1
Bugra asked on 11 Mar 2011, 12:54 PM

Hi everyone,


I'm trying to make a datagrid which allows custom components into cells. I used CellFormatting event of the radGridView. But when i use this event, something strange happening. I introduced the scenario with pictures as bellow;

First i open the form;

Please check 1.png

Then i resize the form;

Please check 2.png

Then i return back to its normal size;

Please check 3.png

Here's the code i wrote for cellformating;

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
       {
           if (e.CellElement.ColumnInfo is GridViewDataColumn && ((GridViewDataColumn)e.CellElement.ColumnInfo).Name == "column1")
           {
               //if (!(e.CellElement.RowElement is GridHeaderRowElement)) // I can't write this code because my codes can't find GridHeaderRowElement class.
               {
 
                   if (e.CellElement.Children.Count > 0)
                       return;
                   ucTextBoxButton txt = new ucTextBoxButton();
                   txt.KeyDown += new KeyEventHandler(txt_KeyDown);
 
                   e.Column.ReadOnly = true;
 
                   ucGridViewTextBoxButtonColumn gridviewclm = new ucGridViewTextBoxButtonColumn(txt);
                   gridviewclm.StretchHorizontally = true;
                   gridviewclm.StretchVertically = true;
                   e.CellElement.Children.Add(gridviewclm);
           
               }
           }
       }

Do you have any ideas?

4 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 11 Mar 2011, 01:11 PM
Hello,

I'm not entirely sure what you are trying to achieve here, but the CellFormatting event is an event that is fired almost constantly (when scrolling, mousing mover the cells etc) and is used to help give a visual formatting to cells. All conditions must be countered with a ResetValue when the conditions are not met. This is because the RadGridView uses a neat UI Virtualization system that essentially re-uses cells.

For more information on CellFormatting, please see this help topic

i hope that this helps but if you need further information please let me know
Thanks
Richard
0
Bugra
Top achievements
Rank 1
answered on 11 Mar 2011, 01:22 PM
Hello there,

I understand that it resets the value because of the performance. But i don't want to lose the places of my components. I mean how can i pretend my components to react like the same as the radgridview rowcells. Because it loses its position everytime i scroll or move my form.

How can i write a code to remember the row positions of my components?
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Mar 2011, 08:22 AM
Hello Bugra,

I'm guessing that this thread has the same purpose, please try to use that custom column if possible.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Ivan Todorov
Telerik team
answered on 16 Mar 2011, 08:13 AM
Hi Burga,

Please try the following code:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    foreach(RadElement element in e.CellElement.Children)
    {
        if(element is RadButtonElement)
        {
            e.CellElement.Children.Remove(element);
            break;
        }
    }
     
    if (e.CellElement.ColumnInfo is GridViewDataColumn && ((GridViewDataColumn)e.CellElement.ColumnInfo).Name == "column1")
    {
        if (!(e.CellElement.RowElement is GridTableHeaderRowElement)
            && !(e.CellElement.RowElement is GridNewRowElement))
        {
 
            if (e.CellElement.Children.Count > 0)
                return;
 
            e.Column.ReadOnly = true;
 
            RadButtonElement gridviewclm = new RadButtonElement("...");
            gridviewclm.StretchHorizontally = true;
            gridviewclm.StretchVertically = true;
            e.CellElement.Children.Add(gridviewclm);
 
        }
    }
}

It should work for your case. The reason for the wrong behavior was on one hand the commented condition in your snippet which checks if the cell is not a header cell or add-new-row cell. On the other hand, like Richard said, you should reset visual changes you make in the cell formatting, because cells are reused. That is why I remove the added button from the children of the cell on the first place.

I hope this will help you. Do not hesitate to ask if you have any further questions.

Best wishes,
Ivan Todorov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
Bugra
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Bugra
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Ivan Todorov
Telerik team
Share this question
or