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

calculation inside radgrid..

3 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shivesh
Top achievements
Rank 1
shivesh asked on 29 Oct 2010, 05:59 AM
hi,
i am trying to calculation in radgrid by some sample available in telerik site but getting error
System.NullReferenceException: Object reference not set to an instance of an object.

in my grid label display in label column on selection of dropdown and quantity insert by user and output should come in some other column label amount.
protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                DropDownList list = (DropDownList)item.FindControl("ddl1");
                list.Items.Insert(0, new ListItem("select", "-1"));
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("select product_name from product_detail", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                list.DataSource = ds;
                list.DataBind();
 
                //For calculate item Amount....
                Label lbl_Rate = (Label)item.FindControl("lblRate");
 
                TextBox txt_Quantity = (TextBox)item.FindControl("txtQuantity");
                Label lbl_Amount = (Label)item.FindControl("lblAmount");
 
                txt_Quantity.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
                lbl_Rate.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
                lbl_Amount.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
 
 
            }
        }
<telerik:RadGrid ID="radGrid1" runat="server" OnInsertCommand="radGrid1_InsertCommand" AutoGenerateColumns="False" GridLines="None"
           AllowMultiRowEdit="True" AllowAutomaticInserts="true" OnItemDataBound="radGrid1_ItemDataBound" OnNeedDataSource="radGrid1_NeedDataSource">
           <MasterTableView AutoGenerateColumns="false" EditMode="InPlace" ShowFooter="false" CommandItemDisplay="Top" OnPreRender="radGrid1_PreRender">
               <Columns>
                   <telerik:GridTemplateColumn HeaderText="Product Name" UniqueName="Product_Name">
                       <ItemTemplate>
                           <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" DataTextField="product_Name"
                               DataValueField="product_Name" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
                           </asp:DropDownList>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="Product_rate">
                       <ItemTemplate>
                           <asp:Label ID="lblrate" runat="server"></asp:Label>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="Product Quantity">
                       <EditItemTemplate>
                           <asp:TextBox ID="txtQuantity" runat="server"/>
                          
                       </EditItemTemplate>
                        
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="Product Amount">
                       <ItemTemplate>
                           <asp:Label ID="lblAmount" runat="server" />
                       </ItemTemplate>
                       <FooterTemplate>
                       <asp:Button ID="btn1" runat="server" Text="Add New Item" OnClick="btn1_click" />
                       </FooterTemplate>
                   </telerik:GridTemplateColumn>
<script type="text/javascript" language="javascript">
       function show(labelrate, labelamount, txtqty) {
           var txt_Quantity = $find(txtqty);
           var lbl_Rate = $find(labelrate)
           var lbl_Amount = $find(labelamount);
           
           var total = txt_Quantity.Getvalue() * lbl_Rate.Getvalue();
           labelamount.Setvalue(total);
 
            }
 
      
    
    
   </script>
now just tell me where is the prob???

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Oct 2010, 07:57 AM
Hello Shivesh,

This error is getting because you have added the TextBox 'txtQuantity' in EditItemTemplate of GridTemplateColumn and trying to access it in normal mode of ItemDataBound event. In order to eliminate this error add the TextBox inside ItemTemplate of GridTemplateColumn like below.

ASPX:
<telerik:GridTemplateColumn>
  <ItemTemplate>
      <asp:TextBox ID="txtQuantity" runat="server" />
  </ItemTemplate>
</telerik:GridTemplateColumn>


Thanks,
Princy.
0
shivesh
Top achievements
Rank 1
answered on 29 Oct 2010, 08:51 AM
Thanks for your response
but still it is not calculating value.
can u tell me my radGrid1_ItemDataBound code is right format may be something mistake here?

or can i put code something like this

protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem item = (GridDataItem)e.Item;
               DropDownList list = (DropDownList)item.FindControl("ddl1");
               list.Items.Insert(0, new ListItem("select", "-1"));
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
               SqlDataAdapter da = new SqlDataAdapter();
               da.SelectCommand = new SqlCommand("select product_id,product_name from product_detail", con);
               DataSet ds = new DataSet();
               da.Fill(ds);
               if (ds != null)
               {
                   list.DataSource = ds;
                   list.DataTextField = "product_name";
                   list.DataValueField = "product_id";
                   list.DataBind();
               }
               list.Items.Insert(0,new ListItem("Select Item..","0"));
               list.SelectedIndex = 0;
 
               //For calculate item Amount....
               
            }
           if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
           {
               GridEditFormInsertItem item1 = (GridEditFormInsertItem)e.Item;
               Label lbl_Rate = item1.FindControl("lblRate") as Label;
               TextBox txt_Quantity = item1.FindControl("txtQuantity") as TextBox;
               Label lbl_Amount = item1.FindControl("lblAmount") as Label;
               //Label lbl_Rate = (Label)item.FindControl("lblRate");
 
               //TextBox txt_Quantity = (TextBox)item.FindControl("txtQuantity");
               //Label lbl_Amount = (Label)item.FindControl("lblAmount");
 
               txt_Quantity.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
               lbl_Rate.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
               lbl_Amount.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
           }
       }
I have change in qtyTextbox to item template.
Thanks
Shivesh
0
Marin
Telerik team
answered on 29 Oct 2010, 02:01 PM
Hello shivesh,

If I got right your idea from the code you need the id of the label from the item template and the id of the text box from the edit item template of the template column. Since you cannot access both controls at the same time on the ItemDataBound event (because only one item is bound and accessible at a time in the handler) you might try accessing them in a later event in the page cycle (e.g. PreRender) there you can filter out the single item that is in edit mode and after that retrieve its corresponding item in normal mode in the grid, then you can retrieve each control (label and textbox) from the relevant item and get their IDs. Here is a sample code snippet visualizing my idea:
protected void Page_PreRender(object sender, EventArgs e)
        {
            GridItem gridEditItem = RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem).SingleOrDefault(item => item.IsInEditMode);
            if (gridEditItem!=null)
            {
                GridItem item = RadGrid1.MasterTableView.GetItems(GridItemType.Item, GridItemType.AlternatingItem).
                    SingleOrDefault(i => i.ItemIndex == gridEditItem.ItemIndex);
                if (item!=null)
                {
                    Label lbl = item.FindControl("lblRate") as Label;
                    TextBox radtxb = gridEditItem.FindControl("txtQuantity") as TextBox;
                }
            }
        }

Hope this help, let me know if you have any other questions.

Best wishes,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
shivesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
shivesh
Top achievements
Rank 1
Marin
Telerik team
Share this question
or