Here is part of grid setup
<telerik:RadGrid ID="RadGridM" runat="server" DataSourceID="DatabaseItem" OnPageIndexChanged="PageChange"
AutoGenerateColumns="False" GridLines="None" AllowMultiRowSelection="true" EnableViewState="false"
Skin="Office2007" AllowPaging="True" PageSize="25"
AllowFilteringByColumn="True" OnPreRender="RadGridM_PreRender" >
HeaderText="Qty Ord" ReadOnly="False" SortExpression="QtyOrd" UniqueName="QtyOrd" Visible="true" AllowFiltering="false" >
<asp:TextBox ID="txtQtyOrdI" runat="server" Text='<%# Bind( "QtyOrd") %>'
# DataBinder.Eval(Container.DataItem, "QtyOrd")%>
<asp:TextBox ID="txtQtyOrd" runat="server" Text='<%# Bind( "QtyOrd") %>' Width="10pt"></asp:TextBox>
<asp:TableCell><asp:CustomValidator ID="Validate_Qty" runat="server" ControlToValidate="txtQtyOrd" ErrorMessage="Amount must be greater than 0." OnServerValidate="CustomValidator_Qty"></asp:CustomValidator>
<telerik:GridBoundColumn DataField="ItemNumber" HeaderText="Item Number" ReadOnly="True" SortExpression="ItemNumber" UniqueName="ItemNumber" Visible="true">
<asp:Button ID="Selection" runat="server" Text="Add Selected" onclick="Selection_Click" />
On the server side when the user presses the Selection button I need to read all the values of each line on the RadGrid.
I am able to get the items with a GridTemplateColumn using this code
foreach (GridDataItem item1 in RadGridM.Items)
GridDataItem item = (GridDataItem)item1;
TextBox QtyVal = item.FindControl("txtQtyOrdI") as TextBox;
This does not work for the GridTemplateColumn, ItemNumber
I have tried getting the DataRowView, and adding the Item Template to the GridTemplateColumn, but the value or item comes back null.
I have also tried item["ItemNumber"] and item["ItemNumber"].Control
If I have a grid that does not allow any editing, but diplays values I need to process server side, and all items are in the form, below, how to a loop through each row on the server side and read the text from every column?
<telerik:GridBoundColumn DataField="testvalue" HeaderText="testvalue"
SortExpression="testvalue" UniqueName="testvalue" Visible="true" ReadOnly="True">
I have also tried the things in this post, and they only work on an Item Template.
17 Answers, 1 is accepted
Try the following approach to get the value in ItemTemplate of GridTemplateColumn.
Also please go through the following documentation which explains how to access the values in grid cells.
Accessing cells and rows
I have already tried this method and every other one here Accessing cells and rows before posting.
Both the lines suggested below return and "Object not set to an instance of an object" error.
Can you provide working sample code that will read from a GridBoundColumn and GridTemplateColumn?
The above code is working fine at my end. Another approach is you can use one Label inside ItemTemplate of GridTemplateColumn. Here is the sample code which shows how to loops through each grid item and access its BoundColumn and TemplateColumn's value on an external button click.
my markup is like
some html text including anchors, img etc. (Not server side controls)
and i'm doing
For Each item As GridDataItem In grid.MasterTableView.Items
Dim cell As TableCell = TryCast(item("published"), TableCell)
cell.Text = ...
cell.Text is empty, i don't understand why, because.. it's not.
cell = TryCast(item("published"), TableCell)
For Each ctrl As DataBoundLiteralControl In cell.Controls
Dim strtIndex As Integer = ctrl.Text.IndexOf("<img")
If strtIndex > 0 Then
Dim endIndex As Integer = ctrl.Text.IndexOf("/>", strtIndex)
Dim substr As String = ctrl.Text.Substring(strtIndex, (endIndex - strtIndex) + 2)
cell.Text = ctrl.Text.Replace(substr, "")
infact any changes i make to the Cell.Text property do not seem to appear in the exported file, why is this ?
When you have a GridTemplateColumn, the cell that has your content rendered for this column does not have its Text property set. Instead, a LiteralControl is rendered in the Controls collection of the cell and this LiteralControl contains the text in your column's ItemTemplate. If you have server controls in the ItemTemplate, they also appear in the Controls collection of your cell. So, you need to find the appropriate control and get/set its value instead of attempting to read and set the cell.Text property.
the Telerik team
Note how the setter clears the controls collection of the table cell if you try to set its text. This means your controls may be cleared before you loop through them all.
the Telerik team
here's all the code
Using the above approach, the cell text is replaced just at the right moment.
the Telerik team
the Telerik team
I'm having the same issue where the values are empty strings. but besides Princy's second post which also worded for me, I was able to get the value this way TryCast(item("FirstName").Controls(0), textbox).text (VB).
(item("FirstName").Controls(0) as textbox).text (C#)
Thank you for sharing your approach and experience with our community. I hope it may prove helpful to other developers as well.