Behavior of the Text property in GridView CellFormatting event

0 Answers 4 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.

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Marian
Top achievements
Rank 2
Bronze
Iron
Iron
Share this question
or