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

Subtotal field

3 Answers 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 30 Oct 2013, 09:00 PM
Is it possible to take two text fields and total them up and show that total in another text field, but only show the total when both fields have a number? If so, how can you do that? But only do that for that row, so if i add another row, the same thing will happen but only to that row, and so on...Does anyone have an example on how to do something like that?

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 Oct 2013, 08:56 AM
Hi Brad,

Please try the sample code snippet that shows three GridTemplateColumn, using three RadNumericTextBox two column values are added to display the result in the third column.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"    GridLines="None" AllowPaging="true" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
            <telerik:GridTemplateColumn HeaderText="EmployeeID">
                <ItemTemplate>
                    <telerik:RadNumericTextBox ID="RadNumTxt1" runat="server" DbValue='<%#Eval("EmployeeID") %>'
                        AutoPostBack="true" OnTextChanged="RadNumTxt1_TextChanged">
                    </telerik:RadNumericTextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="ShipVia">
                <ItemTemplate>
                    <telerik:RadNumericTextBox ID="RadNumTxt2" runat="server" DbValue='<%#Eval("ShipVia") %>'
                        AutoPostBack="true" OnTextChanged="RadNumTxt2_TextChanged">
                    </telerik:RadNumericTextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Total">
                <ItemTemplate>
                    <telerik:RadNumericTextBox ID="RadNumTxt3" runat="server">
                    </telerik:RadNumericTextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem data = (GridDataItem)e.Item;
        RadNumericTextBox RadNumericTextBox1 = (RadNumericTextBox)data.FindControl("RadNumTxt1");
        RadNumericTextBox RadNumericTextBox2 = (RadNumericTextBox)data.FindControl("RadNumTxt2");
        RadNumericTextBox RadNumericTextBox3 = (RadNumericTextBox)data.FindControl("RadNumTxt3");
        int valueRadNumericTxtBox1 = Convert.ToInt16(RadNumericTextBox1.Text);
        int valueRadNumericTxtBox2 = Convert.ToInt16(RadNumericTextBox2.Text);
        int valueRadNumericTxtBox3 = valueRadNumericTxtBox1 + valueRadNumericTxtBox2;
        RadNumericTextBox3.Text = valueRadNumericTxtBox3.ToString();
    }
}
protected void RadNumTxt1_TextChanged(object sender, EventArgs e)
{
    RadNumericTextBox RadNumericTextBox1 = (RadNumericTextBox)sender;
    GridDataItem data = (GridDataItem)RadNumericTextBox1.NamingContainer;
    RadNumericTextBox RadNumericTextBox2 = (RadNumericTextBox)data.FindControl("RadNumTxt2");
    RadNumericTextBox RadNumericTextBox3 = (RadNumericTextBox)data.FindControl("RadNumTxt3");
    int valueRadNumericTxtBox1 = Convert.ToInt16(RadNumericTextBox1.Text);
    int valueRadNumericTxtBox2 = Convert.ToInt16(RadNumericTextBox2.Text);
    int valueRadNumericTxtBox3 = valueRadNumericTxtBox1 + valueRadNumericTxtBox2;
    RadNumericTextBox3.Text = valueRadNumericTxtBox3.ToString();      
}
protected void RadNumTxt2_TextChanged(object sender, EventArgs e)
{
    RadNumericTextBox RadNumericTextBox2 = (RadNumericTextBox)sender;
    GridDataItem data = (GridDataItem)RadNumericTextBox2.NamingContainer;
    RadNumericTextBox RadNumericTextBox1 = (RadNumericTextBox)data.FindControl("RadNumTxt1");
    RadNumericTextBox RadNumericTextBox3 = (RadNumericTextBox)data.FindControl("RadNumTxt3");
    int valueRadNumericTxtBox1 = Convert.ToInt16(RadNumericTextBox1.Text);
    int valueRadNumericTxtBox2 = Convert.ToInt16(RadNumericTextBox2.Text);
    int valueRadNumericTxtBox3 = valueRadNumericTxtBox1 + valueRadNumericTxtBox2;
    RadNumericTextBox3.Text = valueRadNumericTxtBox3.ToString();
}


Thanks,
Princy

0
Brad
Top achievements
Rank 1
answered on 31 Oct 2013, 01:30 PM
Awesome example, thank you exactly what i was looking for.
Just a few questions on it:
1. how/where can i add the event handler to a programatically created radgrid like this?
2. Is it possible to add 2 amounts, with one radnumerictextbox and one regular textbox

public void DefineGridStructure(int i, PlaceHolder ph, Boolean bl)
    {

        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid" + i.ToString();
        grid.Visible = bl;
        grid.Width = Unit.Pixel(775);
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
        grid.AllowAutomaticInserts = false;
        //grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.AllowFilteringByColumn = false;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        //grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.Width = Unit.Pixel(775);
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.AllowAutomaticDeletes = false;
        grid.AllowAutomaticUpdates = false;
        grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);
        grid.InsertCommand += grid_InsertCommand;
        grid.DeleteCommand += grid_DeleteCommand;
        grid.UpdateCommand += grid_UpdateCommand;
        grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "RowNumber";
        boundColumn.HeaderText = "RowNumber";
        boundColumn.ItemStyle.Width = Unit.Pixel(5);
        boundColumn.ReadOnly = true;
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Size";
        boundColumn.HeaderText = "Size";
        boundColumn.ItemStyle.Width = Unit.Pixel(5);
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(boundColumn);

        
        GridAutoCompleteColumn aboundColumn = new GridAutoCompleteColumn();
        aboundColumn.DataField = "Description";
        aboundColumn.HeaderText = "Description";
        aboundColumn.DataTextField = "Description";
        aboundColumn.DataValueField = "Description";
        boundColumn.ItemStyle.Width = Unit.Pixel(20);
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(aboundColumn);

        GridNumericColumn nboundColumn = new GridNumericColumn();
        nboundColumn.DataField = "Quantity";
        nboundColumn.HeaderText = "Quantity";
        boundColumn.ItemStyle.Width = Unit.Pixel(5);
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(nboundColumn);

        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Unit";
        boundColumn.HeaderText = "Unit";
        boundColumn.ItemStyle.Width = Unit.Pixel(5);
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(boundColumn);


        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Duration";
        boundColumn.HeaderText = "Duration";
        boundColumn.ItemStyle.Width = Unit.Pixel(5);
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(boundColumn);

        GridTemplateColumn objGridTemplateColumn = new GridTemplateColumn();
        objGridTemplateColumn.HeaderText = "DurationType";
        objGridTemplateColumn.DataField = "DurationType";
        objGridTemplateColumn.ItemTemplate = new MyTemplate("DurationType");
        objGridTemplateColumn.EditItemTemplate = new MyEditTemplate();
        objGridTemplateColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.Columns.Add(objGridTemplateColumn);

        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Amount";
        boundColumn.HeaderText = "Amount";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn.ItemStyle.Width = Unit.Pixel(10);
        boundColumn.ItemStyle.CssClass = "maximize";
        grid.MasterTableView.EditMode = GridEditMode.InPlace;


        grid.ItemCreated += grid_ItemCreated;

ph.controls.add(grid);
}
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2013, 05:18 AM
Hi Brad,

Please try the sample code snippet. I have used a RadNumericTextBox and a TextBox to calculate the total to show in a TextBox. Template columns can be defined by a custom class that implements the ITemplate interface. Then you can assign an instance of this class to the ItemTemplate property of the GridTemplateColumn object. Inside the class the events can be raised.
Hope this helps, let me know if any concerns.

ASPX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

C#:
protected void Page_Init(object sender, EventArgs e)
{
    RadGrid grid = new RadGrid();
    grid.AutoGenerateColumns = false;
    grid.DataSourceID = "SqlDataSource1";
 
    GridBoundColumn boundColumn1 = new GridBoundColumn();
    grid.MasterTableView.Columns.Add(boundColumn1);
    boundColumn1.DataField = "OrderID";
    boundColumn1.UniqueName = "OrderID";
    boundColumn1.HeaderText = "OrderID";
 
    //Creating TemplateColumn with RadNumericTextBox
    string templateColumnName = "ShipVia";
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    templateColumn.ItemTemplate = new MyTemplate();
    templateColumn.HeaderText = templateColumnName;
    grid.MasterTableView.Columns.Add(templateColumn);
 
    //Creating TemplateColumn with TextBox
    string templateColumnNameId = "EmployeeID";
    GridTemplateColumn templateColumnId = new GridTemplateColumn();
    templateColumnId.ItemTemplate = new MyTemplateId();
    templateColumnId.HeaderText = templateColumnNameId;
    grid.MasterTableView.Columns.Add(templateColumnId);
 
   //Creating TemplateColumn with TextBox for total
    string templateColumnNametotal = "Total";
    GridTemplateColumn templateColumnTotal = new GridTemplateColumn();
    templateColumnTotal.ItemTemplate = new MyTemplatetotal();
    templateColumnTotal.HeaderText = templateColumnNametotal;
    grid.MasterTableView.Columns.Add(templateColumnTotal);           
         
    grid.AllowPaging = true;
    grid.PageSize = 8;
    grid.Skin = "Outlook";
    PlaceHolder1.Controls.Add(grid);
}
private class MyTemplate : ITemplate
{
    protected RadNumericTextBox RadNumTxt;      
    private string colname;
    public MyTemplate()
    {
 
    }       
    public void InstantiateIn(System.Web.UI.Control container)
    {
        RadNumTxt = new RadNumericTextBox();
        RadNumTxt.ID = "RadNumTxt";
        RadNumTxt.AutoPostBack = true;
        RadNumTxt.DataBinding += new EventHandler(RadNumTxt_DataBinding);// Event to Bind the RadNumericTextbox with Value
        RadNumTxt.TextChanged += new EventHandler(RadNumTxt_TextChanged);// Raising the TextChanged Event for RadNumericTextBox
        container.Controls.Add(RadNumTxt);          
    }
 
    void RadNumTxt_TextChanged(object sender, EventArgs e)
    {
        RadNumericTextBox RadNumericTextBox = (RadNumericTextBox)sender;
        GridDataItem data = (GridDataItem)RadNumericTextBox.NamingContainer;
        TextBox txtnum = (TextBox)data.FindControl("txtbox");
        TextBox txttotal = (TextBox)data.FindControl("txtboxtotal");
        int valueRadNumericTxtBox = Convert.ToInt16(RadNumericTextBox.Text);
        int valueTxtBox = Convert.ToInt16(txtnum.Text);
        int valuetotal = valueRadNumericTxtBox + valueTxtBox;
        txttotal.Text = valuetotal.ToString();   
    }      
 
    void RadNumTxt_DataBinding(object sender, EventArgs e)
    {
        RadNumericTextBox radnum = (RadNumericTextBox)sender;
        GridDataItem container = (GridDataItem)radnum.NamingContainer;
        radnum.DbValue = ((DataRowView)container.DataItem)["ShipVia"];
    }      
}
 
private class MyTemplateId : ITemplate
{      
    protected TextBox txtbox;       
    public MyTemplateId()
    {
 
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        txtbox = new TextBox();
        txtbox.ID = "txtbox";
        txtbox.AutoPostBack = true;
        txtbox.DataBinding += new EventHandler(txtbox_DataBinding);// Event to Bind the Textbox with Value
        txtbox.TextChanged += new EventHandler(txtbox_TextChanged);// Raising the TextChanged Event for TextBox
        container.Controls.Add(txtbox);
    }
 
    void txtbox_TextChanged(object sender, EventArgs e)
    {
        TextBox txtnum = (TextBox)sender;
        GridDataItem data = (GridDataItem)txtnum.NamingContainer;
        RadNumericTextBox RadNumericTextBox = (RadNumericTextBox)data.FindControl("RadNumTxt");
        TextBox txttotal = (TextBox)data.FindControl("txtboxtotal");
        int valueRadNumericTxtBox = Convert.ToInt16(RadNumericTextBox.Text);
        int valueTxtBox = Convert.ToInt16(txtnum.Text);
        int valuetotal = valueRadNumericTxtBox + valueTxtBox;
        txttotal.Text = valuetotal.ToString(); 
    }
    void txtbox_DataBinding(object sender, EventArgs e)
    {
        TextBox txtnum = (TextBox)sender;
        GridDataItem container = (GridDataItem)txtnum.NamingContainer;
        txtnum.Text = ((DataRowView)container.DataItem)["EmployeeID"].ToString();
    }     
}
 
private class MyTemplatetotal : ITemplate
{      
    protected TextBox txtboxtotal;     
    public MyTemplatetotal()
    {
 
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        txtboxtotal = new TextBox();
        txtboxtotal.ID = "txtboxtotal";
        txtboxtotal.DataBinding += new EventHandler(txtbox_DataBinding);
        container.Controls.Add(txtboxtotal);
    }
    void txtbox_DataBinding(object sender, EventArgs e)
    {
        TextBox txttotal = (TextBox)sender;
        GridDataItem data = (GridDataItem)txttotal.NamingContainer;
        RadNumericTextBox radnumtxt = (RadNumericTextBox)data.FindControl("RadNumTxt");
        TextBox txtnum = (TextBox)data.FindControl("txtbox");
        int valueRadNumericTxtBox = Convert.ToInt16(radnumtxt.Text);
        int valueTxtBox = Convert.ToInt16(txtnum.Text);
        int valuetotal = valueRadNumericTxtBox + valueTxtBox;
        txttotal.Text = valuetotal.ToString();
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Brad
Top achievements
Rank 1
Share this question
or