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

Calculated Column

3 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
rohit
Top achievements
Rank 1
rohit asked on 28 Feb 2011, 01:28 AM
Hi,

In my RadGrid, i have 4 textboxes in item template.
Now i want apply one calculation formula on these columns.
I want to do auto calculation of txt4 value, when values in txt1, txt2, txt3 changed.
My formula is:
txt4 = txt1 + txt2 - txt3

How can i do this, on which event of grid, or on each textbox text changed event.
Any sample code is helpful.

Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Feb 2011, 06:31 AM
Hello Rohit,

You can try the following approach to achieve the same.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
 {
 
     if (e.Item is GridDataItem)
     {
         GridDataItem item = (GridDataItem)e.Item;
         TextBox T1 = item.FindControl("TextBox1") as TextBox;
         TextBox T2 = item.FindControl("TextBox2") as TextBox;
         TextBox T3 = item.FindControl("TextBox3") as TextBox;
         TextBox T4 = item.FindControl("TextBox4") as TextBox;
         T1.Attributes.Add("onblur", "Total(" + T1.ClientID + "," + T2.ClientID + "," + T3.ClientID + "," + T4.ClientID + ")");
         T2.Attributes.Add("onblur", "Total(" + T1.ClientID + "," + T2.ClientID + "," + T3.ClientID + "," + T4.ClientID + ")");
         T3.Attributes.Add("onblur", "Total(" + T1.ClientID + "," + T2.ClientID + "," + T3.ClientID + "," + T4.ClientID + ")");
     }
 }

ClientSide:
<script type="text/javascript">
    function Total(t1, t2, t3, t4)
     {
        t4.value = parseInt(t1.value) + parseInt(t2.value) - parseInt(t3.value);
     }
</script>

Thanks,
Shinu.
0
rohit
Top achievements
Rank 1
answered on 28 Feb 2011, 04:05 PM
Hi,
I tried  your code approach.But I got this error:

Microsoft JScript runtime error: 'ContentPlaceHolder1_RadGrid1_ctl00_ctl04_TextBox1' is undefined.

Please help.
0
Princy
Top achievements
Rank 2
answered on 02 Mar 2011, 10:40 AM
Hello Rohit,

Give a try with following approach.
C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
           GridDataItem item = (GridDataItem)e.Item;
           TextBox T1 = item.FindControl("TextBox1") as TextBox;
           TextBox T2 = item.FindControl("TextBox2") as TextBox;
           TextBox T3 = item.FindControl("TextBox3") as TextBox;
           TextBox T4 = item.FindControl("TextBox4") as TextBox;
           T1.Attributes.Add("onblur", "Total1(" + item.ItemIndex+ ")");
           T1.Attributes.Add("onblur", "Total2(" + item.ItemIndex+ ")"); 
           //.  .   .   .
 
        }
    }

Java Script:
<script type="text/javascript">
        function Total1(index) {
           var grid = $find("<%=RadGrid1.ClientID %>");
           var MasterTable = grid.get_masterTableView();
           var row = MasterTable.get_dataItems()[index];
           var Value1 = row.findElement("TextBox2").value;
           //access appropriate TextBox and do calculation
       }
        function Total2(index) {
      //similarly access appropriate TextBox and do calculation      
       }
   </script>

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