Computed Column/Expression?

4 posts, 0 answers
  1. Mark
    Mark avatar
    116 posts
    Member since:
    Oct 2014

    Posted 23 Apr 2018 Link to this post

    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. 

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3089 posts

    Posted 24 Apr 2018 Link to this post

    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.
  3. Mark
    Mark avatar
    116 posts
    Member since:
    Oct 2014

    Posted 24 Apr 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    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

  4. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3089 posts

    Posted 25 Apr 2018 Link to this post

    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.
Back to Top