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

GridView : Change Text not Value in grid cell

3 Answers 1035 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nico
Top achievements
Rank 1
Nico asked on 08 Nov 2020, 03:43 AM
Dear sir/madam,

I am using Telerik Gridview in Winforms. Data is bound to the grid.
I tried to use the CellFormatting event to change the text (that is the visual part) of a particular cell, but it does not work. Changing the value of the cell does work, but that is not wanted.
What I try to achieve is to add some extra text at the end of the text in the cell and add an icon (image) in the cell, without changing the value of the databound item.

Any help is appreciated.

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Nov 2020, 12:09 PM
Hello, Nico, 

The CellFormatting event is an appropriate solution for changing only the text part of a cell and keeping its value unchanged. I have prepared a sample code snippet for your reference which result is illustrated in the attached screenshot. It seems that the appended text in the CellFormatting event is displayed as expected:
        public RadForm1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Description", typeof(string));
            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add(i, "Data" + i, "Description" + i);
            }
             
            this.radGridView1.CellFormatting += radGridView1_CellFormatting;
            this.radGridView1.DataSource = dt;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }

        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);
            }
        }

Am I missing something? Could you please specify the exact steps how to reproduce the problem? What changes do I need to perform on my end?

Once we replicate the issue locally, we would be able to make an adequate analysis of the precise case and think about a suitable solution. Thank you in advance.

I am looking forward to your reply.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Nico
Top achievements
Rank 1
answered on 12 Nov 2020, 09:24 AM

Hello Dess,

 

thank you for your explanation. In the meantime I succeeded to change the text part of the grid cell, but adding an image to the cell just does not work. Unfortunately I cannot give you any more details to reproduce the issue, because it is not that complicated. Your approach is okay.
Maybe I should use some extra properties of the cellelement, besides the ones you used:
e.CellElement.DrawImage = true;
e.CellElement.TextImageRelation = TextImageRelation.TextBeforeImage;

I hope for any suggestions.

Thank you.

 

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Nov 2020, 11:11 AM
Hello, Nico,   

I have attached my sample project for your reference. Give it a try. Please have in mind that I use a GridViewTextBoxColumn for the Description column. I suppose that you may be using another column's type. Is this the case in your project?

Feel free to modify the project in a way to reproduce the experienced issue and get back to me with it so I can investigate the precise case. Thank you in advance. 

I am looking forward to your reply.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Nico
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Nico
Top achievements
Rank 1
Share this question
or