Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
739
Problem when first row in hierarchical grid has no children
posted

Hi - 

 I am having a problem exporting hierarchical data from an UltraWebGrid to Excel in one scenario:  When the first parent row has no children.  It took me a while to figure out what the problem was.  Basically I have one on-screen grid with paging that shows the records fine in all scenarios.  Since it has paging, when the user clicks an Export button I create a second, non-paged grid in code and pass that to the Excel exporter control.  However, when I bind the hierarchical DataSet to the programmatically created grid, I get an "Object reference not set to an instance of an object" error *only* if the first row has no children.  

 To illustrate the problem I created a simple console app that uses a manually created DataSet that I borrowed from an Infragistics How To article.  This is the whole thing:

using System;
using System.Data;
using System.Diagnostics;

//the project also needs references to System.Xml and Infragistics.WebUI.Shared

using Infragistics.WebUI.UltraWebGrid;

namespace GridExportProblem
{
    class Program
    {
        private enum ChildRowMode
        {
            AllHaveChildren,
            FirstHaveChildren,
            LastHaveChildren
        }

        static void Main(string[] args)
        {
            CreateGrid(ChildRowMode.AllHaveChildren);
            CreateGrid(ChildRowMode.FirstHaveChildren);

            //this blows up

            CreateGrid(ChildRowMode.LastHaveChildren);
        }

        private static void CreateGrid(ChildRowMode childRowMode)
        {
            DataSet ds = GetDataset(childRowMode);

            UltraWebGrid uwg = new UltraWebGrid("myGrid");
            // Set the WebGrid's view type to Hierarchical
            uwg.DisplayLayout.ViewType =
              Infragistics.WebUI.UltraWebGrid.ViewType.Hierarchical;

            uwg.DataSource = ds.Tables["Customer"];

            Debug.WriteLine("Binding grid with ChildRowMode " + childRowMode.ToString());

            try
            {
                uwg.DataBind();
                Debug.WriteLine("Success!");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failure.  Error: " + ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
        }


        private static DataSet GetDataset(ChildRowMode childRowMode)
        {
            DataSet ds = new DataSet();

            // Customer DataTable
            DataTable dt = new DataTable("Customer");
            dt.Columns.Add("CustomerID", typeof(int));
            dt.Columns.Add("CustomerName", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Rows.Add(new object[] { 1, "John Lever", DateTime.Now });
            dt.Rows.Add(new object[] { 2, "Walter Smith", DateTime.Now.AddDays(1) });
            dt.Rows.Add(new object[] { 3, "Kathy Lever", DateTime.Now.AddDays(2) });
            dt.Rows.Add(new object[] { 4, "George Wills", DateTime.Now.AddDays(3) });
            ds.Tables.Add(dt);

            // Orders DataTable
            dt = new DataTable("Orders");
            dt.Columns.Add("OrderID", typeof(int));
            dt.Columns.Add("ProductName", typeof(string));
            dt.Columns.Add("Price", typeof(double));
            dt.Columns.Add("Quantity", typeof(int));

            if ((childRowMode == ChildRowMode.FirstHaveChildren) || (childRowMode == ChildRowMode.AllHaveChildren))
            {
                dt.Rows.Add(new object[] { 1, "Bolts", 2.30, 15 });
                dt.Rows.Add(new object[] { 1, "Screws", 2.03, 55 });
                dt.Rows.Add(new object[] { 1, "Nails", 1.01, 25 });
                dt.Rows.Add(new object[] { 2, "Bolts", 2.30, 65 });
                dt.Rows.Add(new object[] { 2, "Screws", 2.03, 75 });
                dt.Rows.Add(new object[] { 2, "Nails", 1.01, 95 });
            }

            if ((childRowMode == ChildRowMode.LastHaveChildren) || (childRowMode == ChildRowMode.AllHaveChildren))
            {
                dt.Rows.Add(new object[] { 3, "Bolts", 2.30, 65 });
                dt.Rows.Add(new object[] { 3, "Screws", 2.03, 205 });
                dt.Rows.Add(new object[] { 3, "Nails", 1.01, 265 });
                dt.Rows.Add(new object[] { 4, "Bolts", 2.30, 695 });
            }

            ds.Tables.Add(dt);

            // Create customers/orders relationship and add to DataSet
            ds.Relations.Add("Customer_Order", ds.Tables["Customer"].Columns["CustomerID"],
                ds.Tables["Orders"].Columns["OrderID"]);

            return ds;


        }
    }
}

Parents Reply Children
No Data