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

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

5 Answers 510 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sree
Top achievements
Rank 1
Sree asked on 15 Jan 2013, 12:58 AM
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

5 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 16 Jan 2013, 02:14 AM
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.
0
Sree
Top achievements
Rank 1
answered on 16 Jan 2013, 04:17 AM
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;
0
Julian Benkov
Telerik team
answered on 16 Jan 2013, 01:26 PM
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.
0
Sree
Top achievements
Rank 1
answered on 17 Jan 2013, 02:13 AM
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 ????
0
Accepted
Julian Benkov
Telerik team
answered on 21 Jan 2013, 06:10 AM
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.
Tags
GridView
Asked by
Sree
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Sree
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or