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

how to set value in new row

8 Answers 153 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bevis
Top achievements
Rank 1
Bevis asked on 04 Mar 2012, 07:29 PM
i have a column which is the sum of three other columns. how can i set it before it is added.

8 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 05 Mar 2012, 01:04 PM
Hi Bevis,

The most efficient way to do this would be to provide it to the grid already in the datasource if possible.
Hope that helps
Richard
0
Bevis
Top achievements
Rank 1
answered on 05 Mar 2012, 02:48 PM
My case is when user input the Length, Width and Height, the Volume column will display the CBM which is Length x Width x Height and they can adjust the result (CBM Column) as they wish.  And also I have another grid for input invoice charges with same behavior i.e. Unit Price x Quantity = Amount.

I don't want to popup a modal form to do the input, but in the grid.  Can it be done!


Thanks

0
Richard Slade
Top achievements
Rank 2
answered on 05 Mar 2012, 03:25 PM
Hi Bevis,

There may be many ways to do this. Here is a small example of one way. It just assumes you have a RadGridView on a form
using System.Windows.Forms;
using Telerik.WinControls.UI;
using System.Collections.Generic;
 
 
 
namespace RadControlsWinFormsApp1
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();
 
            List<Product> products = new List<Product>();
            products.Add(new Product(2, 2, 3));
            products.Add(new Product(3, 2, 3));
            products.Add(new Product(4, 2, 3));
 
            this.radGridView1.DataSource = products;
            this.radGridView1.Columns["Width"].ReadOnly = false;
            this.radGridView1.Columns["Height"].ReadOnly = false;
            this.radGridView1.Columns["Length"].ReadOnly = false;
            this.radGridView1.Columns["CBM"].ReadOnly = true;
 
            this.radGridView1.AutoGenerateColumns = true;
 
        }
 
    }
 
    public class Product
    {
 
        public Product(decimal length, decimal height, decimal width)
        {
            this.Length = length;
            this.Height = height;
            this.Width = width;
        }
 
        public decimal Length
        { get; set; }
 
        public decimal Height
        { get; set; }
 
        public decimal Width
        { get; set; }
 
        public decimal CBM
        {
            get
            {
                return (this.Length * this.Width * this.Height);
            }
            set
            {
            }
        }
 
    }
}


Hope that helps
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 05 Mar 2012, 03:29 PM
Bevis,

Apologies, I missed off the empty contructor for a new row.
public Product()
{
}
0
Bevis
Top achievements
Rank 1
answered on 05 Mar 2012, 03:36 PM
Simply I have four columns as follow:

|| length || width || height || CBM ||

When I input  10 in Length, 10 in width and 10 in height the the CBM column will automatic display 1000 (10x10x10).  It can be manage in edit mode, but how can I do it in the Add New Row which the RowIndex is -1 and cannot reference to any rows values.

Private Sub dbList_CellEndEdit(sender As Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles dbList.CellEndEdit
        Select Case e.Column.Name
            Case "p_len", "p_width", "p_height"
                Dim l As Decimal
                l = cn(dbList.Rows(e.RowIndex).Cells("p_len").Value) * cn(dbList.Rows(e.RowIndex).Cells("p_width").Value) * cn(dbList.Rows(e.RowIndex).Cells("p_height").Value)
                dbList.Rows(e.RowIndex).Cells("total_cbm").Value = l
        End Select
 
End Sub

0
Bevis
Top achievements
Rank 1
answered on 06 Mar 2012, 06:45 PM

Hi Richard,

I got a solution as following:

first check the e.RowIndex is -1 (=new row), then set the datasource default value with something with some calucation.

Thanks,

If e.RowIndex <> -1 Then
            Select Case e.Column.Name
                Case "p_len", "p_width", "p_height"
                    Dim l As Decimal
                    l = cn(dbList.Rows(e.RowIndex).Cells("p_len").Value) * cn(dbList.Rows(e.RowIndex).Cells("p_width").Value) * cn(dbList.Rows(e.RowIndex).Cells("p_height").Value) / 1000000
                    dbList.Rows(e.RowIndex).Cells("total_cbm").Value = l
            End Select
        Else
            Select Case e.Column.Name
                Case "p_len"
                    len = e.Value
                Case "p_width"
                    wid = e.Value
                Case "p_height"
                    hei = e.Value
                    rsDtl.Columns("total_cbm").DefaultValue = len * wid * hei / 1000000
            End Select
 
        End If
0
Ivan Petrov
Telerik team
answered on 07 Mar 2012, 12:32 PM
Hi guys,

Thank you for writing.

You can use the CellValueChanged to calculate the value in your sum column. Here is a code snippet which demonstrates this:
private void MasterTemplate_CellValueChanged(object sender, GridViewCellEventArgs e)
{
  if (e.Row is GridViewNewRowInfo)
  {
    decimal valueCol0 = 0;
    decimal valueCol1 = 0;
    decimal valueCol2 = 0;
 
    if (e.Row.Cells[0].Value != null)
    {
      valueCol0 = (decimal)e.Row.Cells[0].Value;
    }
 
    if (e.Row.Cells[1].Value != null)
    {
      valueCol1 = (decimal)e.Row.Cells[1].Value;
    }
 
    if (e.Row.Cells[2].Value != null)
    {
      valueCol1 = (decimal)e.Row.Cells[2].Value;
    }
 
    e.Row.Cells[3].Value = valueCol0 + valueCol1 + valueCol2;
  }
}

I hope this will be useful for you. Should you have further questions, I would be glad to assist.

All the best,
Ivan Petrov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Bevis
Top achievements
Rank 1
answered on 07 Mar 2012, 03:01 PM
Great, thats what I want and it works very well.  Thank you very much!

Tags
GridView
Asked by
Bevis
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Bevis
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or