How to multiply the gridview column with a number without saving to database

6 posts, 1 answers
  1. Sree
    Sree avatar
    30 posts
    Member since:
    Dec 2012

    Posted 14 Jan 2013 Link to this post

    Hello Team,

    I have a gridview with 6 columns , when clicked on contextmenu of grid for specific column, a conversion from one unit of measurement to other should take place




    like the above image showing the conversion units, for example from meter-feet is m-ft
    when user clicks this, i need to take the entire column of grid and multiply the column with 3.281 and rebind it to grid
    i dont want to loop through the grid as the number of records are more than 40,000

    is there any fastest way to achieve this, or in telerik any other options ???

    this one i am doing with out saving to database, please help me
  2. Jeff
    Jeff avatar
    48 posts
    Member since:
    Apr 2012

    Posted 15 Jan 2013 Link to this post

    Here's what I do, it might work for you.

    I add a column to the DataGridView and set DataPropertyName to "NewColumn".
    The column that shows the calculated number is bound to a column that I add at run time like this:
    DataSet.Table.Columns.Add("NewColumn", GetType(String), "MyNumberColumn * " & NumericUpDown1.Value)
    I've just created a new column that's an expression. 
    Now Fill.

    Any time I need to calculate new numbers I do this:
    DataSet.Table.Columns.Remove("NewColumn")
    DataSet.Table.Columns.Add("NewColumn", GetType(String), "MyNumberColumn * " & NumericUpDown1.Value)

    This seems to refill but I am dealing with so few rows that it really isn't an issue for me. There's probably a better way but this works for me. Just modify the expression ("MyNumberColumn * " & NumericUpDown1.Value) to suit your needs.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sree
    Sree avatar
    30 posts
    Member since:
    Dec 2012

    Posted 15 Jan 2013 Link to this post

    Hello Jeff,

    Its working fine when added new column and changing the dataproperty name of gridview, but here i got some error
    when scrolling the gridview records down words, i got an error saying "There is no property descriptor corresponding to property name: NewColumn"
    i have checked below link, but couln't

    http://www.telerik.com/community/forums/winforms/gridview/problem-with-loadlayout.aspx

    my code is

     if (odt != null && odt.Columns.Contains("NewColumn"))
                           {
                               odt.Columns.Remove("NewColumn");
                           }

    odt.Columns.Add("NewColumn4", typeof(String), odt.Columns[stvdcolname].ColumnName + " * " + odt.Columns[colIndex].ColumnName + " * 0.052" );
                           orgv.Columns[colIndex].FieldName = "NewColumn";
                           orgv.DataSource = odt;
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 16 Jan 2013 Link to this post

    Hello Lakshmi,

    You can use Jeff's suggestion and add one unbound expression based column to calculate the value at run-time. To setup this type of column the
    FieldName property is not needed. Please view our online documentation for details about this topic. When the value of this calculated column must be refreshed you can use Refresh method of the MasterTemplate instead of removing and adding the column from the GridViewColumnsCollection.

    I hope this helps.

    All the best,
    Julian Benkov
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  6. Sree
    Sree avatar
    30 posts
    Member since:
    Dec 2012

    Posted 16 Jan 2013 Link to this post

    Hello Julian,

    i need to add the expression to the selected column which is already bound to datasource,
    example selected column is ABC
    and  it is having fieldName some "X",
    when user right clicks and want to convert the unit of measurement of the ABC column, i need to convert the entire column data muliplied by some factor

    i tried this option initially without creating new column like
    odt.Columns[ABC].Expression= "ABC  *  0.120048019 ";        

    which thrown an exception saying "Cannot set Expression property due to circular reference in the expression." that means expression property of column shouldnt contain the same name of selected column , means column ABC expression shouldn't apply on the same selected column ABC, ABC to ABC is conflicting or i don't know exactly the reason

    thats why i have created new datacolumn and applied the expression on it and then added new column name as fieldname to datagrid selected column ABC, which solved my problem
    without using the newly added column name to my datagridview ABC column fieldName, how will i get the calculated result to ABC column ????
  7. Answer
    Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 21 Jan 2013 Link to this post

    Hi Lakshmi,

    To prevent the circular reference expression in your application you can hide the bound column, which contains the original bound value and manually create one expression column to use only for calculation. The initial values of this column will be just the name of the hidden bound column. Here is a sample application:
    Imports System.Data
    Imports System.Windows.Forms
    Imports Telerik.WinControls.UI
     
    Namespace Lab.Grid
        Public Partial Class GridColumnExpressionConverterForm
            Inherits Form
            Private gridView As New RadGridView()
     
     
            Public Sub New()
                InitializeComponent()
     
                gridView.Dock = DockStyle.Fill
                gridView.Parent = Me
                gridView.AllowCellContextMenu = True
                AddHandler gridView.ContextMenuOpening, AddressOf gridView_ContextMenuOpening
            End Sub
     
            Protected Overrides Sub OnLoad(e As EventArgs)
                MyBase.OnLoad(e)
     
                Dim table As New DataTable()
                table.Columns.Add("Id")
                table.Columns.Add("Name")
                table.Columns.Add("Value", GetType(Double))
     
                For i As Integer = 0 To 9
                    table.Rows.Add("ID" & i, "Name" & i, i)
                Next
     
                gridView.DataSource = table
     
                Dim expressionColumn As New GridViewTextBoxColumn()
                expressionColumn.Name = "ExpressionValue"
                gridView.Columns.Add(expressionColumn)
                gridView.Columns("Value").IsVisible = False
                gridView.Columns("ExpressionValue").Expression = "Value"
            End Sub
     
            Private Sub gridView_ContextMenuOpening(sender As Object, e As ContextMenuOpeningEventArgs)
                Dim item As New RadMenuItem("Convert")
                AddHandler item.Click, AddressOf item_Click
                e.ContextMenu.Items.Add(item)
            End Sub
     
            Private Sub item_Click(sender As Object, e As EventArgs)
                gridView.Columns("ExpressionValue").Expression = "Value *  0.120048019"
            End Sub
        End Class
    End Namespace

    I hope this helps.

    Kind regards,
    Julian Benkov
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Back to Top
UI for WinForms is Visual Studio 2017 Ready