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

How do I loop around a RadGrid to retrieve entered values

3 Answers 514 Views
Grid
This is a migrated thread and some comments may be shown as answers.
wonderbison
Top achievements
Rank 1
wonderbison asked on 22 Aug 2011, 05:49 PM
Hi

I may be doing this wrong but I have a programatically created hierarchical grid. How do I loop around each row and column to retrieve values when Button1 is clicked? I am especially interested in values that have changed.

Thanks

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="DataFormatting._Default" %>
  
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
  
  
<form id="form1" runat="server">
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
</telerik:RadAjaxManager>
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
</telerik:RadScriptManager>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
    Text="Loop The Grid" Width="235px" />
    <div class="myRadGrid" >
        <asp:PlaceHolder ID="PlaceHolder1" runat="server" >
          
        <telerik:RadGrid ID="myRadGrid" runat="server" 
            onitemdatabound="myRadGrid_ItemDataBound" onprerender="myRadGrid_PreRender" >
        </telerik:RadGrid>
        <telerik:GridTextBoxColumnEditor runat="server" ID="MyEditor">
            <TextBoxStyle Width="35px" />
        </telerik:GridTextBoxColumnEditor>
        </asp:PlaceHolder>
    </div>
      
</form>


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
  
namespace DataFormatting
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BuildTheGrid();
            }
  
        }
        protected void myRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
        }
        #region Build the grid
  
        protected void myRadGrid_PreRender(object sender, System.EventArgs e)
        {
            bool itemsUpdated = false;
            foreach (GridItem item in myRadGrid.MasterTableView.Items)
            {
                if (item is GridEditableItem)
                {
                    itemsUpdated = true;
  
                    GridEditableItem editableItem = item as GridDataItem;
                    editableItem.Edit = true;
                }
            }
            if (itemsUpdated)
                myRadGrid.Rebind();
        }
        private DataSet BuildTheGrid()
        {
            try
            {
  
                myRadGrid.DataSource = null;
                DataSet myDataToShow = LoadGridData();
  
  
                // Define the main grid
                for (int loopPos = myRadGrid.MasterTableView.Columns.Count; loopPos > 0; loopPos--)
                {
                    myRadGrid.MasterTableView.Columns.RemoveAt(loopPos - 1);
                }
                myRadGrid.ID = "MyGrid";
                myRadGrid.DataSource = myDataToShow;
                myRadGrid.Width = Unit.Percentage(98);
                myRadGrid.AutoGenerateColumns = false;
                myRadGrid.ShowStatusBar = true;
                myRadGrid.GroupingEnabled = true;
                myRadGrid.MasterTableView.Width = Unit.Percentage(100);
  
                // now build the hierarchical grid
                GridGroupByExpression groupExpression = new GridGroupByExpression();
                myRadGrid.MasterTableView.GroupByExpressions.Add(new GridGroupByExpression("GroupValue1 group by GroupValue1"));
                myRadGrid.MasterTableView.GroupByExpressions.Add(new GridGroupByExpression("GroupValue2 group by GroupValue2"));
  
                myRadGrid.MasterTableView.ShowGroupFooter = true;
                myRadGrid.GroupingSettings.RetainGroupFootersVisibility = true;
                myRadGrid.AllowMultiRowEdit = true;
                myRadGrid.MasterTableView.EditMode = GridEditMode.InPlace;
  
                GridBoundColumn boundColumn = new GridBoundColumn();
  
                foreach (DataColumn col in myDataToShow.Tables[0].Columns)
                {
                    boundColumn = new GridBoundColumn();
                    myRadGrid.MasterTableView.Columns.Add(boundColumn);
                    boundColumn.DataField = col.ColumnName;
                    boundColumn.HeaderText = col.ColumnName;
                    boundColumn.Visible = true;
                    if (col.Ordinal > 1)
                    {
                        boundColumn.Aggregate = GridAggregateFunction.Sum;
                        boundColumn.FooterText = "";
                        boundColumn.DataFormatString = "{0:##,##0}";
                        boundColumn.ColumnEditorID = "MyEditor";
                        boundColumn.ReadOnly = false;
                    }
  
                }
                myRadGrid.DataBind();
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
                return null;
            }
            finally
            {
            }
            return null;
  
        }
        #endregion
  
        #region Load the Grid Data
        private DataSet LoadGridData()
        {
            // return the data to display
            DataSet contributionData = new DataSet("MyData");
  
            DataTable gridData = contributionData.Tables.Add("ContData");
  
            gridData.Columns.Add(new DataColumn("GroupValue1"));
            gridData.Columns.Add(new DataColumn("GroupValue2"));
            DataColumn decCol = new DataColumn("ValueToSum");
            decCol.DataType = System.Type.GetType("System.Int32");
            gridData.Columns.Add(decCol);
            DataColumn perCol = new DataColumn("ValueToIgnore");
            perCol.DataType = System.Type.GetType("System.Int32");
            gridData.Columns.Add(perCol);
            DataColumn decCol2 = new DataColumn("ValueToSum2");
            decCol2.DataType = System.Type.GetType("System.Int32");
            gridData.Columns.Add(decCol2);
            DataColumn perCol2 = new DataColumn("ValueToIgnore2");
            perCol2.DataType = System.Type.GetType("System.Int32");
            gridData.Columns.Add(perCol2);
            DataColumn decCol3 = new DataColumn("ValueToSum3");
            decCol3.DataType = System.Type.GetType("System.Int32");
            gridData.Columns.Add(decCol3);
            DataColumn perCol3 = new DataColumn("ValueToIgnore3");
            perCol3.DataType = System.Type.GetType("System.Int32");
            gridData.Columns.Add(perCol3);
  
  
            for (int j = 1; j < 5; j++)
            {
                for (int i = 1; i < 5; i++)
                {
                    DataRow dataRow = contributionData.Tables[0].NewRow();
  
                    dataRow["GroupValue1"] = "Header 1 as " + j.ToString();
                    dataRow["GroupValue2"] = "Heading 2 as " + i.ToString();
                    dataRow["ValueToSum"] = 1234 * i * j;
                    dataRow["ValueToIgnore"] = 805 * i * j;
                    dataRow["ValueToSum2"] = 42 * i * j;
                    dataRow["ValueToIgnore2"] = 901 * i * j;
                    dataRow["ValueToSum3"] = 651 * i * j;
                    dataRow["ValueToIgnore3"] = 104 * i * j;
  
                    contributionData.Tables[0].Rows.Add(dataRow);
                }
            }
            return contributionData;
        }
        #endregion
  
        protected void Button1_Click(object sender, EventArgs e)
        {
  
        }

3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 23 Aug 2011, 07:47 AM
Hello Wonderbison,

For more information about how to loop through all detail tables/items in Telerik RadGrid you can refer to the help article below:
http://www.telerik.com/help/aspnet-ajax/grid-traversing-detail-tables-items-in-grid.html

To retrieve the newly entered values from the GridEditableItem upon update you can use ExtractValuesFromItem() method:
http://www.telerik.com/help/aspnet-ajax/grid-retrieve-original-values-for-edited-item.html

All the best,
Pavlina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
wonderbison
Top achievements
Rank 1
answered on 23 Aug 2011, 07:54 AM
I did see those articles but the Traversing detail tables/items I cannot get to work which is why I posted.

So using the traversing method described, my "button" click method looks like the following however I get "Object reference not set to an instance of an object"

protected void Button1_Click(object sender, EventArgs e)
{
    GridTableView nestedTableView = (myRadGrid.MasterTableView.Items[0] as GridDataItem).ChildItem.NestedTableViews[0];
    foreach (GridNestedViewItem nestedViewItem in nestedTableView.GetItems(GridItemType.NestedView))
    {
        foreach (GridDataItem item in nestedViewItem.NestedTableViews[0].Items)
        {
            Console.WriteLine(item["ValueToSum"].Text.ToString());
        }
    }
}
0
Pavlina
Telerik team
answered on 25 Aug 2011, 11:02 AM
Hello Wonderbison,

Please go throught the following help article and see if it helps:
http://www.telerik.com/help/aspnet-ajax/grid-extract-primary-key-for-parent-item-in-hierarchy-on-update-insert.html

Greetings,
Pavlina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
wonderbison
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
wonderbison
Top achievements
Rank 1
Share this question
or