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

Computed Column/Expression?

3 Answers 112 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 2
Veteran
Mark asked on 23 Apr 2018, 12:53 PM

I have a grid view that has data bound to it.  What I am looking to do, is add a column, have an expression that computes the value in the field to text based on another columns value. I am unsure how to accomplish this.  Any help would be greatly appreciated.

 

For example, I have a foreign key column called PersonKey.  What I want to be able to do is call a public method that returns a string of the Person's name based on the key that is passed to it. 

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Apr 2018, 07:51 AM
Hello, Mark,   

RadGridView supports calculated columns which are identified by an expression. The only condition necessary to make a regular column behave like a calculated column is to set an expression to it. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/columns/calculated-columns-(column-expressions)

However, according to the provided information it seems that you don't need to calculate a certain expression but display different text considering another cell's value. For this  purpose, it is suitable to use the CellFormatting event and set the text of the cell element according to your requirement. Here is demonstrated a sample code snippet:
public RadForm1()
{
    InitializeComponent();
 
    GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn("PersonKey");
    radGridView1.MasterTemplate.Columns.Add(decimalColumn);
 
    GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn("PersonName");
    radGridView1.MasterTemplate.Columns.Add(textBoxColumn);
 
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    this.radGridView1.CellFormatting += radGridView1_CellFormatting;
 
    for (int i = 0; i < 5; i++)
    {
        this.radGridView1.Rows.Add(i);
    }
}
 
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Row is GridViewDataRowInfo && e.Column.Name == "PersonName")
    {
        e.CellElement.Text = GetPersonNameByKey((decimal)e.Row.Cells["PersonKey"].Value);
    }
}
 
private string GetPersonNameByKey(decimal key)
{
    return "Name" + key;
}



I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 2
Veteran
answered on 24 Apr 2018, 01:37 PM

Yea, I thought you were going to suggest that.  I was hoping there was something I could add to the grid column as an expression to do the same thing

 

Thanks

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Apr 2018, 05:42 AM
Hello, Mark,   

Such expression columns are relevant for numeric fields because you can specify a certain aggregate function that RadGridView can easily calculate. For the text fields it is not suitable to use any expression. That is why the suitable solution that I can suggest is to use the CellFormatting event as it was demonstrated in my previous reply.

If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Mark
Top achievements
Rank 2
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mark
Top achievements
Rank 2
Veteran
Share this question
or