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

Retrieve column value from grid in code behind.

13 Answers 957 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 13 Mar 2013, 10:15 AM
Hi,

Can you help me figure out how to retrieve a grid column value from GridTemplateColumn in code behind? I have a grid template column that uses a DataBinder.Eval but for some reason I can't figure how to retrieve the value in code behind. Here is the script...
<telerik:GridTemplateColumn DataField="Amount" HeaderText="Amount" UniqueName="Amount" >
                            <ItemStyle CssClass="ItemsGridAmount" BorderStyle="Solid" BorderColor="Black" BorderWidth="1" />
                            <HeaderStyle Width="85px" HorizontalAlign="Center" />
                        <ItemTemplate >
                            <asp:Label runat="server" style="position:relative; top:2px; margin-right:8px" ID="lblAmount" Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}") %>'>></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" style="margin-right:10px" ID="lblAmount" Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}") %>'>></asp:Label>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
and here is the code behind...
ry
        {
            foreach (GridDataItem item in grdInvoiceItems.Items)
            {
                int id = (int)item.OwnerTableView.DataKeyValues[item.ItemIndex]["Id"];
                string lineItem = (item["Items"].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;
 
                Invoice.UpdateInvoiceItems(id, lineItem, description, Convert.ToInt32(price),
                    Convert.ToInt32(qty), Convert.ToDouble(discount), 0, 100);
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message;
            this.Master.FindControl("ErrorContentPlaceHolder").Visible = true;
        }
Nothing I've tried seems to work thus far. Any help would be greatly appreciated.

Thanks,
Ron.

13 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 13 Mar 2013, 01:23 PM
Hello,

Please try with below code snippet.

foreach (GridDataItem item in grdInvoiceItems.Items)
        {  
            Label amount1 = (item.FindControl("lblAmount") as Label);
        }

if above code is not working for you then please give different ID to label.
<telerik:GridTemplateColumn DataField="Amount" HeaderText="Amount" UniqueName="Amount">
                        <ItemStyle CssClass="ItemsGridAmount" BorderStyle="Solid" BorderColor="Black" BorderWidth="1" />
                        <HeaderStyle Width="85px" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:Label runat="server" Style="position: relative; top: 2px; margin-right: 8px"
                                ID="lblAmount" Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}") %>'>></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label runat="server" Style="margin-right: 10px" ID="lblAmountEdit" Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}") %>'>></asp:Label>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>

Let me know if any concern.


Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 16 Mar 2013, 11:37 AM
Thanks for your reply.

It still did not work. Can you take a look at the image?
0
Shinu
Top achievements
Rank 2
answered on 18 Mar 2013, 04:32 AM
HI,

Try the following code to achieve your scenario.
c#:
foreach (GridEditableItem item in RadGrid1.EditItems)
{
            Label amount1 = (item.FindControl("lblAmount") as Label);
 }

Thanks,
Shinu
0
Ron
Top achievements
Rank 1
answered on 20 Mar 2013, 02:31 AM
That too did not work. The compiler never entered the foreach loop as to say it's not a GridEditableItem.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Mar 2013, 04:47 AM
Hello,

Please achieve this thing with another way.

<MasterTableView DataKeyNames="Amount" >

foreach (GridDataItem item in grdInvoiceItems.Items)
{
    int id = (int)item.OwnerTableView.DataKeyValues[item.ItemIndex]["Id"];
    string lineItem = (item["Items"].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;
     
     
    string amount = item.GetDataKeyValue("Amount").ToString();
 
    Invoice.UpdateInvoiceItems(id, lineItem, description, Convert.ToInt32(price),
        Convert.ToInt32(qty), Convert.ToDouble(discount), 0, 100);
}


Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 21 Mar 2013, 10:35 AM
That also did not work. I've attached a screen shot to show you the value. Once again I appreciate your help with this.

Thanks,
Ron.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 21 Mar 2013, 02:11 PM
Hello Ron,

I think at that time of binding of grid "Amount" Is "0".  That's why you get the zero in this.

If possible then can you please provide screenshot of your grid with data.

Note : because DataKeyValue never change neither in normal mode nor in edit mode.

Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 22 Mar 2013, 10:13 AM
I've attached a screen shot of the grid along with a screen shot of the on click event code. On click there's a saveInvoiceItem() method thats executed first which loops through the grid and save each column of each row to a separate table. I'm able to get the values for all column expect the amount column granted the amount column is rendered differently.
<telerik:GridTemplateColumn DataField="Discount" HeaderText="Discount" UniqueName="Discount">
                       <ItemStyle Width="75px" CssClass="ColumnDiscount" />
                       <ItemTemplate>
                           <telerik:RadNumericTextBox ID="txtDiscount" Width="75px" CssClass="ItemsGridTextBox" Value='<%# Eval("Discount") %>' runat="server"
                               FocusedStyle-BackColor="#fff6dc">
                               <ClientEvents OnValueChanged="DiscountChanged" OnFocus="OnDiscountFocusGetColumnValues"  />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <telerik:RadNumericTextBox ID="txtDiscount" Width="75px" Value='<%# Eval("Discount") %>' runat="server">
                               <ClientEvents OnValueChanged="DiscountChanged" OnFocus="OnDiscountFocusGetColumnValues"  />
                           </telerik:RadNumericTextBox>
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn DataField="Amount" HeaderText="Amount" UniqueName="Amount" >
                           <ItemStyle CssClass="ItemsGridAmount" BorderStyle="Solid" BorderColor="Black" BorderWidth="1" />
                           <HeaderStyle Width="85px" HorizontalAlign="Center" />
                       <ItemTemplate >
                           <asp:Label runat="server" style="position:relative; top:2px; margin-right:8px" ID="lblAmount" Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}") %>'>></asp:Label>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <asp:Label runat="server" style="margin-right:10px" ID="lblAmount" Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}") %>'>></asp:Label>
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn>
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Mar 2013, 12:24 PM
Hello,

I am not able to find any issue in your code.

If possible then can you please try one more time and it is not worked for you then please provide project with support ticket.

<telerik:GridBoundColumn DataField="Amount" Visible="false" UniqueName="MyColumn1"></telerik:GridBoundColumn>
 
string strAmount = item["MyColumn1"].Text;


Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 02 Apr 2013, 12:48 AM
It's still not working but I think it's because of the way it's binded. What do you think? See Text='<%# DataBinder.Eval (Container, "DataItem.Amount", "{0:F}")        vs Value='<%# Eval("Discount") %>'
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Apr 2013, 04:31 PM
Hello,

Can you please provide your code/project using support ticket?

Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 05 Apr 2013, 11:20 AM
I'm sorry but I'm unable to provide the entire project. It is a large project, very large. I can provide the entire page including code behind and all JavaScript and css. Would that help?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Apr 2013, 05:16 PM
Hello,

Yes, If possible then can you please create one sample application (web site/web application/radcontrols website).

And add your (single/one) page into this application.

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