Having trouble converting RadGrid Demo to WebPart

4 posts, 0 answers
  1. J
    J avatar
    143 posts
    Member since:
    Jun 2006

    Posted 13 Jan 2010 Link to this post

    I am working with emulating the RadGrid demo found here: Grid Demo - Hierarchy.  I am able to attach to the w3wp.exe process to debug and even get inside the event 'ItemDataBound' when it is raised.  Instead of using the control SQLDataSource I in turn am using a DataSet populated with DataTables.  Here is my code for my webpart:

            protected override void CreateChildControls()  
            {  
                base.CreateChildControls();  
     
                try 
                {  
                    da = new DataAccess();  
     
                    strArea = "SubSite Name";  
                    strListName = "List Name";  
                    strView = "All Documents";  
     
                    ds = da.GetListData(strArea, strListName, strView);  
     
                    this.EnsureUpdatePanelFixups();  
                    UpdatePanel up = new UpdatePanel();  
                    up.ID = "UpdatePanel1";  
                    up.ChildrenAsTriggers = true;  
                    up.UpdateMode = UpdatePanelUpdateMode.Conditional;  
                    this.Controls.Add(up);  
     
                    //  Example of adding RadGrid to Moss 2007 site using a RadGrid  
                    // *************************************************************  //  
                    rdDocs = new RadGrid();  
                    rdDocs.ID = "rdDocs";  
     
                    rdDocs.DetailTableDataBind += new GridDetailTableDataBindEventHandler(rdDocs_DetailTableDataBind);  
                    rdDocs.ItemCommand += new GridCommandEventHandler(rdDocs_ItemCommand);  
                    rdDocs.ItemDataBound += new GridItemEventHandler(rdDocs_ItemDataBound);  
     
                    rdDocs.PageSize = 30;  
                    rdDocs.AllowPaging = true;  
                    rdDocs.AllowSorting = true;  
                    rdDocs.GridLines = GridLines.None;  
                    rdDocs.ShowStatusBar = true;  
                    rdDocs.PagerStyle.Mode = GridPagerMode.NumericPages;  
     
                    rdDocs.AutoGenerateColumns = false;  
     
                    GridBoundColumn gbcID = new GridBoundColumn();  
                    GridBoundColumn gbcLinkFileName = new GridBoundColumn();  
                    GridBoundColumn gbcPrimaryPOC = new GridBoundColumn();  
     
                    gbcID.DataField = "ID";  
                    gbcID.HeaderText = "Document ID";  
                    gbcID.Visible = false;  
     
                    gbcLinkFileName.DataField = "LinkFilename";  
                    gbcLinkFileName.HeaderText = "Documents";  
     
                    gbcPrimaryPOC.DataField = "Primary_x0020_POC";  
                    gbcPrimaryPOC.HeaderText = "Primary POC";  
     
                    rdDocs.Columns.Add(gbcID);  
                    rdDocs.Columns.Add(gbcLinkFileName);  
                    rdDocs.Columns.Add(gbcPrimaryPOC);  
     
                    rdDocs.Width = Unit.Percentage(95);  
                    rdDocs.DataSource = ds.Tables[0];  
                    rdDocs.DataBind();  
     
                    up.ContentTemplateContainer.Controls.Add(rdDocs);  
     
                    // *************************************************************  //  
     
                    //  Example of adding RadGrid to Moss 2007 site using a RadGrid  
                    // *************************************************************  //  
                    GridTableView rdSndPOCs = new GridTableView();  
                    GridTableViewRelation gtvrSndPOCs = new GridTableViewRelation();  
     
                    rdSndPOCs.ID = "rdSndPOCs";  
                    rdSndPOCs.Caption = "Secondary POCs";  
     
                    GridRelationFields grfSndPOCs = new GridRelationFields();  
                    grfSndPOCs.DetailKeyField = "ID";  
                    grfSndPOCs.MasterKeyField = "ID";  
     
                    rdDocs.MasterTableView.ParentTableRelation.Add(grfSndPOCs);  
     
                    gtvrSndPOCs.Add(grfSndPOCs);  
     
                    rdSndPOCs.PageSize = 30;  
                    rdSndPOCs.AllowPaging = true;  
                    rdSndPOCs.AllowSorting = true;  
                    rdSndPOCs.GridLines = GridLines.None;  
                    rdSndPOCs.PagerStyle.Mode = GridPagerMode.NumericPages;  
     
                    rdSndPOCs.AutoGenerateColumns = false;  
     
                    gbcID = new GridBoundColumn();  
                    GridBoundColumn gbcSecondaryPOC = new GridBoundColumn();  
     
                    gbcID.DataField = "ID";  
                    gbcID.HeaderText = "Document ID";  
                    gbcID.Visible = true;  
     
                    gbcSecondaryPOC.DataField = "SecondaryPOC";  
                    gbcSecondaryPOC.HeaderText = "Secondary POC";  
     
                    rdSndPOCs.Columns.Add(gbcID);  
                    rdSndPOCs.Columns.Add(gbcSecondaryPOC);  
     
                    rdSndPOCs.Width = Unit.Percentage(95);  
                    rdSndPOCs.DataSource = ds.Tables[1];  
                    rdSndPOCs.DataBind();  
     
                    rdDocs.MasterTableView.DetailTables.Add(rdSndPOCs);  
                     
                }  
                catch (Exception ex)  
                {  
                    Context.Response.Write(ex.Message);  
                }  
            }  
     
     void rdDocs_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
                {  
                    string str = "";  
                }  
     
                void rdDocs_ItemDataBound(object sender, GridItemEventArgs e)  
                {  
                    strTest += e.Item.ItemIndex + "<br>";  
                }  
     
                void rdDocs_ItemCommand(object source, GridCommandEventArgs e)  
                {  
                    //throw new NotImplementedException();  
                    if (e.CommandName == "ExpandCollapse")  
                    {  
                        DataTable dt = (DataTable)e.Item.OwnerTableView.DataSource;  
                        if ((dt != null) && (dt.TableName == "DocTable"))  
                        {  
                            GridTableView detailView = (e.Item as GridDataItem).ChildItem.NestedTableViews[1];  
                            detailView.Visible = false;  
                        }  
                    }  
                } 

    The link between the two tables (Tables are named "DocTable" and "SPOCTable") is the column "ID".  When this control is displayed on the page, the first table shows up fine.  The second table however, doesnt show up at all.  The hierarchal demo is really cool and useful for what we want but I just do not understand what is going on.

    J
  2. J
    J avatar
    143 posts
    Member since:
    Jun 2006

    Posted 13 Jan 2010 Link to this post

    As an update, I modified the code to use some 'dummy data' that just creates and uses simple DataSets/DataTables instead of Sharepoint Lists.

            protected void Page_PreRenderComplete(object sender, EventArgs e)  
            {  
                if (!Page.IsPostBack)  
                {  
                    rdDocs.MasterTableView.Items[0].Expanded = true;  
                }  
            }  
     
            protected override void CreateChildControls()  
            {  
                base.CreateChildControls();  
     
                try 
                {  
                    this.Page.PreRenderComplete +=new EventHandler(Page_PreRenderComplete);  
     
                    da = new DataAccess();  
     
                    ds = da.GetDummyData();  
     
                    this.EnsureUpdatePanelFixups();  
                    UpdatePanel up = new UpdatePanel();  
                    up.ID = "UpdatePanel1";  
                    up.ChildrenAsTriggers = true;  
                    up.UpdateMode = UpdatePanelUpdateMode.Conditional;  
                    this.Controls.Add(up);  
     
                    //  Example of adding RadGrid to Moss 2007 site using a RadGrid  
                    // *************************************************************  //  
                    rdDocs = new RadGrid();  
                    rdDocs.ID = "rdDocs";  
     
                    rdDocs.DetailTableDataBind += new GridDetailTableDataBindEventHandler(rdDocs_DetailTableDataBind);  
                    rdDocs.ItemCommand += new GridCommandEventHandler(rdDocs_ItemCommand);  
                    rdDocs.ItemDataBound += new GridItemEventHandler(rdDocs_ItemDataBound);  
     
                    rdDocs.PageSize = 30;  
                    rdDocs.AllowPaging = true;  
                    rdDocs.AllowSorting = true;  
                    rdDocs.GridLines = GridLines.None;  
                    rdDocs.ShowStatusBar = true;  
                    rdDocs.PagerStyle.Mode = GridPagerMode.NumericPages;  
     
                    rdDocs.AutoGenerateColumns = false;  
     
                    GridBoundColumn gbcID = new GridBoundColumn();  
                    GridBoundColumn gbcLinkFileName = new GridBoundColumn();  
                    //GridBoundColumn gbcPrimaryPOC = new GridBoundColumn();  
     
                    gbcID.DataField = "ID";  
                    gbcID.HeaderText = "Document ID";  
                    gbcID.Visible = false;  
     
                    gbcLinkFileName.DataField = "LinkFilename";  
                    gbcLinkFileName.HeaderText = "Documents";  
     
                    //gbcPrimaryPOC.DataField = "Primary_x0020_POC";  
                    //gbcPrimaryPOC.HeaderText = "Primary POC";  
     
                    rdDocs.Columns.Add(gbcID);  
                    rdDocs.Columns.Add(gbcLinkFileName);  
                    //rdDocs.Columns.Add(gbcPrimaryPOC);  
     
                    rdDocs.Width = Unit.Percentage(95);  
                    rdDocs.DataSource = ds.Tables[0];  
                    rdDocs.DataBind();  
     
                    //up.ContentTemplateContainer.Controls.Add(rdDocs);  
     
                    // *************************************************************  //  
     
                    //  Example of adding RadGrid to Moss 2007 site using a RadGrid  
                    // *************************************************************  //  
                    GridTableView rdSndPOCs = new GridTableView();  
                    GridTableViewRelation gtvrSndPOCs = new GridTableViewRelation();  
     
                    rdSndPOCs.ID = "rdSndPOCs";  
                    rdSndPOCs.Caption = "POCs";  
     
                    GridRelationFields grfSndPOCs = new GridRelationFields();  
                    grfSndPOCs.DetailKeyField = "ID";  
                    grfSndPOCs.MasterKeyField = "ID";  
     
                    rdDocs.MasterTableView.ParentTableRelation.Add(grfSndPOCs);  
     
                    gtvrSndPOCs.Add(grfSndPOCs);  
     
                    //rdSndPOCs = new RadGrid();  
     
                    rdSndPOCs.PageSize = 30;  
                    rdSndPOCs.AllowPaging = true;  
                    rdSndPOCs.AllowSorting = true;  
                    rdSndPOCs.GridLines = GridLines.None;  
                    //rdSndPOCs.ShowStatusBar = true;  
                    rdSndPOCs.PagerStyle.Mode = GridPagerMode.NumericPages;  
     
                    //rdSndPOCs.AutoGenerateColumns = true;  
                    rdSndPOCs.AutoGenerateColumns = false;  
     
                    gbcID = new GridBoundColumn();  
                    GridBoundColumn gbcSecondaryPOC = new GridBoundColumn();  
                    GridBoundColumn gbcPOCRole = new GridBoundColumn();  
     
                    gbcID.DataField = "ID";  
                    gbcID.HeaderText = "Document ID";  
                    gbcID.Visible = true;  
     
                    gbcSecondaryPOC.DataField = "SecondaryPOC";  
                    gbcSecondaryPOC.HeaderText = "Secondary POC";  
     
                    gbcPOCRole.DataField = "role";  
                    gbcPOCRole.HeaderText = "POC Role";  
     
                    rdSndPOCs.Columns.Add(gbcID);  
                    rdSndPOCs.Columns.Add(gbcSecondaryPOC);  
                    rdSndPOCs.Columns.Add(gbcPOCRole);  
     
                    rdSndPOCs.Width = Unit.Percentage(95);  
                    rdSndPOCs.DataSource = ds.Tables[1];  
                    rdSndPOCs.DataBind();  
     
                    rdDocs.MasterTableView.DetailTables.Add(rdSndPOCs);  
     
                    up.ContentTemplateContainer.Controls.Add(rdDocs);  
                     
                }  
                catch (Exception ex)  
                {  
                    Context.Response.Write(ex.Message);  
                }  
     
            }  
     
    void rdDocs_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
                {  
                    //throw new NotImplementedException();  
                    string str = "";  
                }  
     
                void rdDocs_ItemDataBound(object sender, GridItemEventArgs e)  
                {  
                    strTest += e.Item.ItemIndex + "<br>";  
                }  
     
                void rdDocs_ItemCommand(object source, GridCommandEventArgs e)  
                {  
                    //throw new NotImplementedException();  
                    if (e.CommandName == "ExpandCollapse")  
                    {  
                        DataTable dt = (DataTable)e.Item.OwnerTableView.DataSource;  
                        if ((dt != null) && (dt.TableName == "DocTable"))  
                        {  
                            GridTableView detailView = (e.Item as GridDataItem).ChildItem.NestedTableViews[1];  
                            //detailView.Visible = false;  
                        }  
                    }  
                } 

    Here is my DataAccess class that just creates some dumb data:
    public class DataAccess  
        {  
            public DataAccess()  
            {  
                //  
                // TODO: Add constructor logic here  
                //  
            }
            #region Public Methods  
            public DataSet GetDummyData()  
            {  
                DataSet ds = new DataSet();  
     
                //  
                // Here we create a DataTable with 2 columns.  
                //  
                DataTable table1 = new DataTable();  
                table1.Columns.Add("ID"typeof(int));  
                table1.Columns.Add("LinkFilename"typeof(string));  
     
                //  
                // Here we add 3 DataRows.  
                //  
                table1.Rows.Add(1, "Doc 1");  
                table1.Rows.Add(2, "Doc 2");  
                table1.Rows.Add(3, "Doc 3");  
     
                ds.Tables.Add(table1);  
     
                //  
                // Here we create a DataTable with four columns.  
                //  
                DataTable table2 = new DataTable();  
                table2.Columns.Add("ID"typeof(int));  
                table2.Columns.Add("SecondaryPOC"typeof(string));  
                table2.Columns.Add("role"typeof(string));  
     
                //  
                // Here we add five DataRows.  
                //  
                table2.Rows.Add(1, "D""P");  
                table2.Rows.Add(1, "J""S");  
                table2.Rows.Add(1, "N""S");  
                table2.Rows.Add(2, "L""P");  
                table2.Rows.Add(3, "E""P");  
                table2.Rows.Add(3, "B""S");  
     
                ds.Tables.Add(table2);  
     
                //  
                // Here we create a DataTable with 3 columns.  
                //  
                DataTable table3 = new DataTable();  
                table3.Columns.Add("ID"typeof(int));  
                table3.Columns.Add("version"typeof(int));  
                table3.Columns.Add("LinkFilename"typeof(string));  
     
                //  
                // Here we add five DataRows.  
                //  
                table3.Rows.Add(1, "1""Doc 1");  
                table3.Rows.Add(1, "2""Doc 1.1");  
                table3.Rows.Add(1, "3""Doc 1.2");  
                table3.Rows.Add(2, "1""Doc 2");  
                table3.Rows.Add(3, "1""Doc 3");  
                table3.Rows.Add(3, "2""Doc 3.1");  
                table3.Rows.Add(3, "3""Doc 3.2");  
                table3.Rows.Add(3, "4""Doc 3.3");  
     
                ds.Tables.Add(table3);  
     
                return ds;  
            } 

    So what I can get to load is the first table with that column that looks like it will expand, but when I click this arrow icon nothing happens.  Even if I attach to the w3wp process and debug, my breakpoint inside ItemCommand never gets hit even though the breakpoint inside the event ItemDataBound gets hit.

    J

  3. J
    J avatar
    143 posts
    Member since:
    Jun 2006

    Posted 13 Jan 2010 Link to this post

    Ok an update.  I ran across this advanced Telerik RadGrid Hierarchy tutorial: Hierarchical grid, what you should know.  It stated that if you choose to use the Hierarchial model then you cant databind your datasource.  Instead an option is to bind your data via the event 'NeedDataSource'.  So I did that, removed my command:
    rdDocs.DataSource = ds.Tables[0];     
    rdDocs.DataBind();     
     
    And instead added this:
    rdDocs.NeedDataSource += new GridNeedDataSourceEventHandler(rdDocs_NeedDataSource);  
     
    void rdDocs_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
    {  
      rdDocs.DataSource = ds;  

    Also, I think another thing that helped was that the last thing I do is to add the RadGrid control to my UpdatePanel AFTER I have added the GridTableViews to the RadGrid.MasterTableView.DetailTables collection.

    But, new question.  While I get the Hierarchical struture to display with the correct Detail tables, I cant seem to get the relationships to filter correctly.  Mainly I have a column in each of my DataTables of (int)ID.  Havent seem to find anything helpful, beyond the SQLDataSource example, that explains connecting RadGrids parent table to its correct child Detail tables.

    J
  4. J
    J avatar
    143 posts
    Member since:
    Jun 2006

    Posted 13 Jan 2010 Link to this post

    Ok I figured it out.  I read the article on 'Hierarchical data-binding using DetailTableDataBind event' focusing on the DataTable.Select portion.  In the end what seemed to work for me was to handle the DetailTableDataBind event like this:

    void rdDocs_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
    {  
        //throw new NotImplementedException();  
        GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;  
     
        if((parentItem.Expanded) && ((DataTable)e.DetailTableView.DataSource).TableName == "SPOCTable")  
        {  
            DataTable dtPOC = (DataTable)e.DetailTableView.DataSource;  
            e.DetailTableView.DataSource = dtPOC.Select("ID = '" + parentItem["ID"].Text + "'");  
        }  
        else if ((parentItem.Expanded) && ((DataTable)e.DetailTableView.DataSource).TableName == "VersionTable")  
        {  
            DataTable dtVersion = (DataTable)e.DetailTableView.DataSource;  
            e.DetailTableView.DataSource = dtVersion.Select("ID = '" + parentItem["ID"].Text + "'");  
        }  

    I guess a better approach to this is to replace the if...ifelse condition with a switch statement instead.  But for now this works. 

    J
Back to Top