Behavior of the Text property in GridView CellFormatting event

1 Answer 25 Views
GridView
Marian
Top achievements
Rank 2
Bronze
Iron
Iron
Marian asked on 11 Dec 2025, 11:17 PM

Hello,

I have a question about the behavior of the Text property in the CellFormatting event in GridView. I understand that GridView uses UI virtualization, and that I need to reset all modified properties for the rest of cells. But how does the Text property behave? I looked through the forum and found this example:

private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.Name == "Description")
            {
                e.CellElement.Text = e.CellElement.Value + " END";
                e.CellElement.Image = Properties.Resources.OutlookViewCalendar;
                e.CellElement.DrawImage = true;
                e.CellElement.TextImageRelation = TextImageRelation.TextBeforeImage;
            }
            else
            { 
                e.CellElement.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawImageProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.TextImageRelationProperty, ValueResetFlags.Local);
            }
        }

In this example, all properties are reset except Text. When I tried to reset Text, all cell texts disappeared, except the ones I set manually in CellFormatting. So I think Text has special behavior in CellFormatting, and it's always set from Value at the beginning and it's not affected by UI virtualization and reusing cells. Is that correct?

Also, just to be sure, in all examples I saw the if/else pattern. But functionally, is it the same as if I reset all properties I want to change at the beginning, and then set them later? Or can this have some performance impact? The reason I ask is that I use a switch with more branches, and it would be hard to track what needs to be reset in each one.

Here is my case. And I was also thinking, in my case, is it correct to reset the properties before checking ColumnIndex and RowIndex, or after?

private void rgv_CellFormatting(object sender, CellFormattingEventArgs e)
		{
			e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
			e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
			e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
			e.CellElement.ResetValue(LightVisualElement.PaddingProperty, ValueResetFlags.Local);
			//e.CellElement.ResetValue(LightVisualElement.TextProperty, ValueResetFlags.Local);

			if ((e.ColumnIndex < 0) || (e.RowIndex < 0))
				return;

			var ed = e.Row.DataBoundItem as TestParEditorItem;
			if (ed == null)
				return;

			bool isSub = ed.IsSubItem;
			bool hasSub = ed.HasSubItems;

			switch (e.Column.Name)
			{
				case "colR":
				case "colFi":
				case "colR2":
				case "colFi2":
					if (isSub)
						e.CellElement.Text = "";
					break;

				case "colMin":
				case "colMax":
					if (hasSub)
						e.CellElement.Text = "";
					break;

				case "colAct":
					e.CellElement.BackColor = SystemColors.ControlLight;
					e.CellElement.NumberOfColors = 1;
					e.CellElement.DrawFill = true;
					break;

				case "colType":
					if (isSub)
					{
						e.CellElement.Text = ed.SubItemName;
						e.CellElement.Padding = new Padding(30, 0, 0, 0);
					}
					else
					{
						e.CellElement.Padding = new Padding(0, 0, 0, 0);
					}
					break;
			}
		}

Thanks.

1 Answer, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 16 Dec 2025, 01:09 PM

Hello, Marian,

Behavior of the Text Property in CellFormatting

Yes, you are right about the Text property. The Text property of a cell element in the CellFormatting event is automatically set to the cell's Value at the start of each formatting cycle. So you only need to set it if you want to display something different; otherwise, it will represent the value. You do not need to reset the Text property unless you have set it manually and want to clear or change it. If you reset the Text, the text would disappear from the cell, and it is expected, which is why it is not reset in the example you shared. UI virtualization will not cause the text to "stick" between reused cell elements unless you explicitly assign a value to Text in the event handler. Resetting is mandatory for all properties that visually have an effect on how the cell looks, such as font, forecolor, backcolor, borders, etc.

If you call ResetValue(LightVisualElement.TextProperty, ValueResetFlags.Local), the cell will appear empty unless you set the Text property manually within the event handler. This is expected, as resetting removes the local value and leaves the cell without text unless the grid assigns it again.

Resetting Properties: If/Else Pattern vs. Resetting at the Start

Both approaches—using if/else branches to reset only the properties you change, or resetting all relevant properties at the start—are functionally equivalent. The key is to ensure that any property you modify is reset for cells that do not meet your formatting criteria, so reused cell elements do not display incorrect formatting. There is no significant performance impact between these two patterns, as long as you reset only the properties you actually change. Choosing to reset at the start can simplify your code.

    I checked the provided code snippet regarding the CellFormatting event, and it seems ok to me. It is correct to reset the properties before checking ColumnIndex and RowIndex. This ensures that every cell element starts with default formatting, preventing leftover formatting from affecting cells that do not meet your logic criteria. Resetting other properties at the beginning of the event handler is a good practice, especially with complex logic. So, if this is your preferred way and the formatting is applied as per your needs, you can continue that way.

      Regards,
      Nadya | Tech Support Engineer
      Progress Telerik

      Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

      Marian
      Top achievements
      Rank 2
      Bronze
      Iron
      Iron
      commented on 16 Dec 2025, 02:09 PM

      Hello, thanks. I was almost sure it works like this, but I have asked for sure. I have found one more thing in my code, the else branch in case "colAct" is unnecessary, because it's reset to default in the beginning by ResetValue, I had that row there before added those resets.
      Nadya | Tech Support Engineer
      Telerik team
      commented on 16 Dec 2025, 02:58 PM

      Yes, of course, you can customize your code in the CellFormatting event to match your specific requirements and special setup. This event is especially useful for handling such customizations and fine-tuning the appearance of your grid.

      If you have any other questions or concerns, do not hesitate to contact me again.

      Tags
      GridView
      Asked by
      Marian
      Top achievements
      Rank 2
      Bronze
      Iron
      Iron
      Answers by
      Nadya | Tech Support Engineer
      Telerik team
      Share this question
      or