Tuesday, 4 September 2012

how to calculate totalfield value at clientside based other 2 fields

<script type="text/javascript">
function OnCalculateTotal(visibleIndex, keyValue) {
var controlCollection = ASPxClientControl.GetControlCollection();
var RatePerUnit = controlCollection.GetByName("seClientPrice_" + visibleIndex).GetNumber();
var NoOfUnits = controlCollection.GetByName("seClientQuantity_" + visibleIndex).GetNumber();
var total = RatePerUnit * NoOfUnits;controlCollection.GetByName(
"tbClientTotal_" + visibleIndex).SetText(total.toFixed(2));hfChanges.Set(
"#" + visibleIndex.toString(), keyValue + "|" + RatePerUnit + "|" + NoOfUnits);}
</javascript>

<dx:ASPxGridView ID="ProductGridView" runat="server" AutoGenerateColumns="False" EnableCallBacks="true"
DataSourceID="dsService" KeyFieldName="ProductCode" OnCustomUnboundColumnData="ProductGridView_CustomUnboundColumnData">
<dx:GridViewDataTextColumn FieldName="ProductListPrice" VisibleIndex="4" Caption="RatePerUnit"><DataItemTemplate>
<dx:ASPxSpinEdit ID="RatePerUnit" runat="server" Height="21px" Number="0" Value='<%# Bind("ProductListPrice") %>'OnInit="RatePerUnit_Init"/></DataItemTemplate>
</dx:GridViewDataTextColumn><dx:GridViewDataTextColumn FieldName="NoOfUnits" VisibleIndex="5"><DataItemTemplate><dx:ASPxSpinEdit ID="NoOfUnits" runat="server" Height="21px" Number="0"OnInit="NoOfUnits_Init"/></DataItemTemplate>


C#

protected void ProductGridView_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e){

if (e.Column.FieldName == "Total"){

decimal price = (decimal)e.GetListSourceFieldValue("RatePerUnit");
int quantity = Convert.ToInt32(e.GetListSourceFieldValue("NoOfUnits"));e.Value = price * quantity;
}
}

protected void tbTotal_Init(object sender, EventArgs e){

ASPxTextBox textBox = sender as ASPxTextBox;
GridViewDataItemTemplateContainer container = textBox.NamingContainer as GridViewDataItemTemplateContainer;textBox.Text = container.Text;
textBox.ClientInstanceName =
String.Format("tbClientTotal_{0}", container.VisibleIndex);}

protected void NoOfUnits_Init(object sender, EventArgs e){

ASPxSpinEdit spinEdit = (ASPxSpinEdit)sender;
GridViewDataItemTemplateContainer container = spinEdit.NamingContainer as GridViewDataItemTemplateContainer;spinEdit.ClientInstanceName =
String.Format("seClientQuantity_{0}", container.VisibleIndex);spinEdit.ClientSideEvents.NumberChanged =
String.Format("function(s, e) {{ OnCalculateTotal({0},{1}); }}", container.VisibleIndex, container.KeyValue);}

protected void RatePerUnit_Init(object sender, EventArgs e){

ASPxSpinEdit spinEdit = (ASPxSpinEdit)sender;
GridViewDataItemTemplateContainer container = spinEdit.NamingContainer as GridViewDataItemTemplateContainer;spinEdit.ClientInstanceName =
String.Format("seClientPrice_{0}", container.VisibleIndex);spinEdit.ClientSideEvents.NumberChanged =
String.Format("function(s, e) {{ OnCalculateTotal({0},{1}); }}", container.VisibleIndex, container.KeyValue);}


Link:
http:
//www.devexpress.com/Support/Center/e/E3929.aspx

1 comment: