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

Accessing cells and rows in code behind.

3 Answers 971 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 14 Jan 2014, 01:25 AM
Hi,

I'm using a radgrid to load 3 rows of data on the page initially. The grid is bound to the client and it all works fine. The first column is the ID column which I need to access in codebehind to get the value of the column in order to save the newly added data. I've tried everything to access the data in the ID column with no success. There is data in the ID because I can see it in the grid and even when I open firebug. It is so weird. Here's the code and a screen shot which shoes the data in the ID column is present.
<telerik:RadGrid ID="grdInvoiceItems" runat="server"
                       Width="934px" AutoGenerateColumns="false" EnableViewState="true"
                       AllowMultiRowEdit="false"
                       onitemcreated="grdInvoiceItems_ItemCreated" Skin="Silk"
                       ShowHeader="false" style="border:0" BorderColor="#CCCCCC">
            <ClientSettings>
               <ClientEvents  OnRowMouseOver="showContent" OnRowMouseOut="hideContent" OnCommand="gridCommand"  OnRowDataBound="RowDataBound" />
           </ClientSettings>
           <MasterTableView ClientDataKeyNames="Id,Description" DataKeyNames="Id"
            CommandItemDisplay="None" >
               <ItemStyle Height="50px" />
               <AlternatingItemStyle Height="50px" />
               <Columns>
                   <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id"></telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn DataField="Item" UniqueName="Item">
                       <ItemStyle />
                       <ItemTemplate>
                         <telerik:RadComboBox ID="ddlItems" runat="server" AutoPostBack="false"
                               DataSourceID="objItems" DataTextField="Items" DataValueField="Items"
                               Skin="Default"  OnClientDropDownOpening="DropDownOpening"
                               OnClientSelectedIndexChanged="ItemValueChanged" EnableViewState="true"
                               AllowCustomText="False"   Width="160px" Height="150px"
                               ondatabound="ddlItems_DataBound" EmptyMessage="Select an item"
                               FocusedStyle-BackColor="#fff6dc" >
                           </telerik:RadComboBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn DataField="Description" HeaderText="Description" UniqueName="Description">
                       <ItemStyle />
                       <ItemTemplate>
                           <telerik:RadTextBox ID="txtDescription" Width="320px" Height="30px" CssClass="ItemsGridTextBoxAlighLeft"
                               runat="server" FocusedStyle-BackColor="#fff6dc"></telerik:RadTextBox>
                       </ItemTemplate>
                  </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn DataField="Price" HeaderText="Price" UniqueName="Price">
                        <ItemStyle Width="100px"   />
                        <ItemTemplate>
                           <telerik:RadNumericTextBox ID="txtPrice" Width="100px" Height="30px" CssClass="ItemsGridPriceTextBox" runat="server"
                               FocusedStyle-BackColor="#fff6dc" >
                                <ClientEvents OnValueChanged="PriceChanged" OnFocus="OnPriceFocusGetColumnValues" />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn DataField="Qty" HeaderText="Qty" UniqueName="Qty">
                       <ItemStyle Width="50px" />
                       <ItemTemplate>
                           <telerik:RadNumericTextBox ID="txtQty" Width="50px" Height="30px" CssClass="ItemsGridQtyTextBox"  runat="server"
                            FocusedStyle-BackColor="#fff6dc" >
                               <NumberFormat DecimalDigits="0" />
                               <ClientEvents OnValueChanged="QtyChanged" OnFocus="OnQtyFocusGetColumnValues"  />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn DataField="Discount" HeaderText="Discount" UniqueName="Discount">
                       <ItemStyle Width="85px" CssClass="ColumnDiscount" />
                       <ItemTemplate>
                           <telerik:RadNumericTextBox ID="txtDiscount" Width="85px" Height="30px" CssClass="ItemsGridDiscountTextBox" runat="server"
                               FocusedStyle-BackColor="#fff6dc">
                               <ClientEvents OnValueChanged="DiscountChanged" OnFocus="OnDiscountFocusGetColumnValues"  />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn DataField="Amount" HeaderText="Amount" UniqueName="Amount" >
                           <ItemStyle Width="105px" />
                           <HeaderStyle Width="105px" HorizontalAlign="Center" />
                       <ItemTemplate >
                           <telerik:RadNumericTextBox ID="lblAmount" runat="server" CssClass="ItemsGridAmount" Width="105px" Height="30px" >
                                <ClientEvents OnValueChanged="AmountChanged" />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn>
                       <HeaderStyle Width="0px" />
                       <ItemStyle BorderStyle="None"  CssClass="ImgColumn"  />
                       <ItemTemplate>
                           <div id="deleteRow" class="myWrap1" style="visibility:hidden" onclick="DeleteRow();">
                               <asp:HyperLink ID="DeleteLink" runat="server">  
                                <asp:Image ID="DeleteCategory" runat="server" ImageUrl="~/Images/icons/test/Delete.png" AlternateText="Delete Row" /> 
                                </asp:HyperLink>
                           </div>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridBoundColumn DataField="InvoiceId" HeaderText="InvoiceId" UniqueName="InvoiceId" ItemStyle-CssClass="InvoiceIdColumn">
                   </telerik:GridBoundColumn>
foreach (GridDataItem item in grdInvoiceItems.Items)
          {
              //object id = (object)item.OwnerTableView.DataKeyValues[item.ItemIndex]["Id"];
              object value = item["Id"].Text;
              id = (item["Id"].FindControl("lblId") as RadTextBox).Text;
              string lineItem = (item["Item"].FindControl("ddlItems") as RadComboBox).Text;
              string description = (item["Description"].FindControl("txtDescription") as RadTextBox).Text;
              double? price = (item["Price"].FindControl("txtPrice") as RadNumericTextBox).Value;
              double? qty = (item["Qty"].FindControl("txtQty") as RadNumericTextBox).Value;
              double? discount = (item["Discount"].FindControl("txtDiscount") as RadNumericTextBox).Value;
              //double tax = double.Parse(lblTaxes.Text);
              //Label amount1 = (item["Amount"].FindControl("lblAmount") as Label);
              //string amount = item["Amount"].Text;
              //Label amount1 = (item.FindControl("lblAmount") as Label);
              double? amounnt = (item["Amount"].FindControl("lblAmount") as RadNumericTextBox).Value;
 
              if (!string.IsNullOrEmpty(lineItem))
              {
                  Invoice.UpdateInvoiceItems(Convert.ToInt32(id), lineItem, description, Convert.ToDouble(price),
                      Convert.ToInt32(qty), Convert.ToDouble(discount), 0, Convert.ToDouble(amounnt));
              }
          }

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Jan 2014, 05:37 AM
Hi Ron,

Please try the following code snippet to access the data in the GridBoundColumn using UniqueName.

C#:
protected void grdInvoiceItems_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    foreach (GridDataItem item in grdInvoiceItems.Items)
    {
        string itemValue = item["OrderID"].Text; // unique name of the column
    }
}

Thanks,
Shinu.
0
Ron
Top achievements
Rank 1
answered on 14 Jan 2014, 03:04 PM
Thanks for your reply Shinu, but getting the value in ItemDataBound will not work even if the code worked. I need that data on a specific request. I need it on a button click event when the user has completed the form and ready to save.
0
Shinu
Top achievements
Rank 2
answered on 15 Jan 2014, 03:58 AM
Hi Ron,

You can access the ID on a button click when the Grid is in EditMode, either accessing it as a BoundColumn or by DataKeyValue as follows:

C#:
protected void Savebtn_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.EditItems)
    {
        GridEditableItem edititem = (GridEditableItem)item.EditFormItem;
        string id = edititem.GetDataKeyValue("ID").ToString();// Access using DataKeyValue
         //OR
        TextBox txt1 = (TextBox)edititem["ID"].Controls[0];//Access BoundColumn
        string id1 = txt1.Text;
    }
}

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