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
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) { }