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

Loop through grid and read values from server-side

3 Answers 1754 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thomas Derenthal
Top achievements
Rank 1
Thomas Derenthal asked on 31 Oct 2014, 03:33 PM
I have a grid with some bound columns and a template column with a textbox. Is it possible to have a button outside the grid which calls a serverside code to loop through the table and read the values from bound columns and the template column textbox?


                    <telerik:RadButton ID="btnApprove" runat="server" Text="Approved for Payment" OnClick="btnApprove_Click">
                    </telerik:RadButton>



<telerik:RadGrid ID="gridFundingBills" runat="server" Width="1100px" AllowMultiRowSelection="True"
                CssClass=" MyGridClass"
                AutoGenerateColumns="False"
                OnNeedDataSource="gridFundingBills_NeedDataSource"
                OnItemDataBound="gridFundingBills_ItemDataBound"
                 >
                <MasterTableView DataKeyNames="FundingDtlID" EditMode="InPlace">
                    <Columns>
                        <telerik:GridBoundColumn DataField="CustomerNm" FilterControlAltText="Filter CustomerNm column" HeaderText="Property" SortExpression="CustomerNm" UniqueName="CustomerNm">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="VendorNm" FilterControlAltText="Filter VendorNm column" HeaderText="Vendor" SortExpression="VendorNm" UniqueName="VendorNm">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="BankAcct" FilterControlAltText="Filter BankAcct column" HeaderText="Bank" ReadOnly="True" SortExpression="BankAcct" UniqueName="BankAcct">
                        </telerik:GridBoundColumn>
                        <telerik:GridHyperLinkColumn UniqueName="BillNo" HeaderText="Bill Reference Number" DataTextField="BillNo" DataNavigateUrlFields="BillUrl" DataNavigateUrlFormatString="http://{0}" Target="_blank">
                        </telerik:GridHyperLinkColumn>
                        <telerik:GridBoundColumn DataField="BillURL" FilterControlAltText="Filter BillURL column" ReadOnly="True" SortExpression="BillURL" UniqueName="BillURL" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="BillPeriod" FilterControlAltText="Filter BillPeriod column" HeaderText="Bill Period" ReadOnly="True" SortExpression="BillPeriod" UniqueName="BillPeriod" ItemStyle-Width="150px" HeaderStyle-Width="150px">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="DueDt" DataType="System.DateTime" FilterControlAltText="Filter DueDt column" HeaderText="Due Date" SortExpression="DueDt" UniqueName="DueDt" DataFormatString="{0:d}" ItemStyle-Width="75px" HeaderStyle-Width="75px">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="TotalAmt" DataType="System.Decimal" FilterControlAltText="Filter TotalAmt column" HeaderText="TotalAmt" SortExpression="Total Due" UniqueName="TotalAmt" DataFormatString="{0:F}" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="75px" HeaderStyle-Width="75px">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="RemainingAmt" DataType="System.Decimal" FilterControlAltText="Filter RemainingAmt column" HeaderText="Remaining Balance" ReadOnly="True" SortExpression="RemainingAmt" UniqueName="RemainingAmt" DataFormatString="{0:F}" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right" ItemStyle-Width="75px" HeaderStyle-Width="75px">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="FundingDtlID" DataType="System.Guid" FilterControlAltText="Filter FundingDtlID column" HeaderText="FundingDtlID" ReadOnly="True" SortExpression="FundingDtlID" UniqueName="FundingDtlID" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="SourceBankAcctID" DataType="System.Guid" FilterControlAltText="Filter BankAcctID column" HeaderText="BankAcctID" ReadOnly="True" SortExpression="BankAcctID" UniqueName="BankAcctID" Visible="false">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn HeaderText="Amount Approved" UniqueName="AmtApproved" HeaderStyle-Width="55px"
                            ItemStyle-Width="55px" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
                            <ItemTemplate>
                                <asp:TextBox ID="AmountApproved" runat="server" Width="55px" OnTextChanged="AmountApproved_TextChanged"
                                    AutoPostBack="true" CssClass="numericAlign"></asp:TextBox>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
                        </telerik:GridClientSelectColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings AllowKeyboardNavigation="true" EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
                    <ClientEvents OnRowSelected="handleTextBoxValue" OnRowDeselected="handleTextBoxValue" OnActiveRowChanged="sumAmtApproved" />
                    <Selecting AllowRowSelect="true" />
                </ClientSettings>
            </telerik:RadGrid>


protected void btnApprove_Click(object sender, EventArgs e)
    {
        //loop through each row in table
        //save value of bound column to var
        //save value of textbox in template column to var
    }



3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 04 Nov 2014, 12:31 PM
Hi Thomas,

There are different approach for retrieving values from columns, but for GridBoundColumn you could use the GridDataItem and access the column cell by its UniqueName and retrieve the value from the cell's Text property:
foreach (GridDataItem item in RadGrid1.Items)
{
    string id = item["ID"].Text;
  ....

However, for GridTemplateColumns you must first find the column cell, but then you have to use the FindControl() to find a specific control:
foreach (GridDataItem item in RadGrid1.Items)
{
    string id = item["ID"].Text;
    string firstName = (item["TempColumn1"].FindControl("TextBox1") as TextBox).Text;
}

For your convenience, following is the entire example:
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView EditMode="InPlace">
        <Columns>
            <telerik:GridBoundColumn DataField="ID"></telerik:GridBoundColumn>
            <telerik:GridTemplateColumn UniqueName="TempColumn1">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="TextBox1" Text='<%# Eval("FirstName") %>'></asp:TextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 
<telerik:RadButton runat="server" ID="RadButton1" Text="Click me" OnClick="RadButton1_Click"></telerik:RadButton>

And the code-behind:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName" + i);
    }
 
    (sender as RadGrid).DataSource = table;
}
 
protected void RadButton1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.Items)
    {
        string id = item["ID"].Text;
        string firstName = (item["TempColumn1"].FindControl("TextBox1") as TextBox).Text;
    }
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Bill Noble
Top achievements
Rank 1
answered on 13 Feb 2017, 05:27 PM

Hi

I am having a problem getting the data from a telerik GridCheckBoxColumn when using 

foreach (GridDataItem item in RadGrid1.Items)

item("my_info").Text  returns   "&nbsp;" not true or false

(this is NOT in a template column)

Any information as to how I can do this will be helpful

Best regards

Bill

 

 

0
Bill Noble
Top achievements
Rank 1
answered on 22 Feb 2017, 09:40 AM

Any help ?

Is this a BUG ?

Bill

Tags
Grid
Asked by
Thomas Derenthal
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Bill Noble
Top achievements
Rank 1
Share this question
or