how to set value in new row

9 posts, 0 answers
  1. Bevis
    Bevis avatar
    8 posts
    Member since:
    Jan 2011

    Posted 04 Mar 2012 Link to this post

    i have a column which is the sum of three other columns. how can i set it before it is added.
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Mar 2012 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Bevis
    Bevis avatar
    8 posts
    Member since:
    Jan 2011

    Posted 05 Mar 2012 Link to this post

    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

  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Mar 2012 Link to this post

    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
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 05 Mar 2012 Link to this post

    Bevis,

    Apologies, I missed off the empty contructor for a new row.
    public Product()
    {
    }
  7. Bevis
    Bevis avatar
    8 posts
    Member since:
    Jan 2011

    Posted 05 Mar 2012 Link to this post

    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

  8. Bevis
    Bevis avatar
    8 posts
    Member since:
    Jan 2011

    Posted 06 Mar 2012 Link to this post

    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
  9. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 07 Mar 2012 Link to this post

    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 >>
  10. Bevis
    Bevis avatar
    8 posts
    Member since:
    Jan 2011

    Posted 07 Mar 2012 Link to this post

    Great, thats what I want and it works very well.  Thank you very much!

Back to Top
UI for WinForms is Visual Studio 2017 Ready