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

RadGrid Programmatic Load-On-Demand Troubles

2 Answers 250 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felipe
Top achievements
Rank 1
Felipe asked on 10 Dec 2010, 11:07 PM
Hello,

I really need to be able to programmatically create, and populate a hierarchical radgrid.

right now I am just working on two levels (master, one detail) and am trying to setup the load-on-demand functionality. I am getting the master to load, but when i try and expand a row, it just sits forever (with the statusbar doing something).

The query it is running for a row shouldn't take much longer than a second...  here is the code I am working with:

protected void Page_Load(object sender, EventArgs e)
    {
        _Grid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(_Grid_DetailTableDataBind);
        _Grid.NeedDataSource +=new GridNeedDataSourceEventHandler(_Grid_NeedDataSource);
 
        LoadGrid();
    }
 
    void  _Grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        if (!e.IsFromDetailTable)
        {
            _Grid.DataSource = GetDataTable("county:zavala");
        }
    }
 
    void _Grid_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        switch (e.DetailTableView.Name)
        {
            case "PARENT":
                {
                    //string ID = dataItem.GetDataKeyValue("ID").ToString();
                    e.DetailTableView.DataSource = GetDataTable("county:zavala");
                    break;
                }
 
            case "CHILD":
                {
                    string cscpID = dataItem.GetDataKeyValue("cscpID").ToString();
                    e.DetailTableView.DataSource = GetDataTable("cscpID:" + cscpID + " view:WP");
                    break;
                }
        }
    }
 
    private SqlCommand QueryCommand(string Query)
    {
        SqlConnection conn = new SqlConnection(GetConnectionString("connTGS_"));
        SqlCommand cmd = new SqlCommand("tgs_.pd.NewQuery", conn);
        cmd.CommandType = CommandType.StoredProcedure;
 
        SqlParameter qry = cmd.Parameters.Add("@Query", SqlDbType.NVarChar, 2000);
        qry.Value = Query;
        SqlParameter iserror = cmd.Parameters.Add("@iserror", SqlDbType.Bit, 1);
        iserror.Value = false;
 
        return cmd;
    }
 
    private DataTable GetDataTable(string query)
    {
        SqlCommand cmd = QueryCommand(query);
        SqlDataAdapter dA = new SqlDataAdapter(cmd);
        DataTable dT = new DataTable();
        dA.Fill(dT);
 
        return dT;
    }
 
    private void LoadGrid()
    {
        if (!Page.IsPostBack)
        {
            _Grid.PageSize = 20;
            _Grid.AllowPaging = true;
            _Grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            _Grid.AutoGenerateColumns = false;
            _Grid.ShowStatusBar = true;
 
            _Grid.ClientSettings.AllowColumnsReorder = true;
            _Grid.ClientSettings.AllowDragToGroup = true;
            _Grid.ClientSettings.AllowColumnHide = true;
 
            InitRootBand(ref _Grid);
            InitChildBand(ref _Grid);
        }
        _radAjaxMan.AjaxSettings.AddAjaxSetting(_Grid, _Grid);
    }
 
 
    private void InitRootBand(ref RadGrid grid)
    {
        //grid.DataMember = "PARENT";
        grid.MasterTableView.DataKeyNames = new string[] { "id" };
 
        grid.Columns.Add(newColumn("id", "ID", "id", null, false));
        grid.Columns.Add(newColumn("cid", "cID", "p_cID", null, false));
        grid.Columns.Add(newColumn("opid", "opID", "p_opID", null, false));
        grid.Columns.Add(newColumn("CompanyName", "Company Name", "p_CompanyName", null, false));
    }
 
    private void InitChildBand(ref RadGrid grid)
    {
        GridTableView tableWP = new GridTableView(grid);
        grid.MasterTableView.DetailTables.Add(tableWP);
 
        //tableWP.DataMember = "CHILD";
        tableWP.Name = "CHILD";
        tableWP.DataKeyNames = new string[] { "cscpID" };
 
        tableWP.Columns.Add(newColumn("ID", "ID", "ID", null, false));
        tableWP.Columns.Add(newColumn("cscpID", "cscpID", "cscpID", null, false));
        tableWP.Columns.Add(newColumn("cid", "cID", "c_cID", null, false));
         
        tableWP.Columns.Add(newColumn("FirstMonth", "First Month", "c_FirstMonth", "{0:MMM-yy}", false));
 
        GridRelationFields drMain = new GridRelationFields();
 
        drMain.MasterKeyField = "id";
        drMain.DetailKeyField = "cscpID";
        tableWP.ParentTableRelation.Add(drMain);
    }
 
    private GridBoundColumn newColumn(string fieldName, string headerText, string Key, string format, bool hidden)
    {
        GridBoundColumn fld = new GridBoundColumn();
        fld.DataField = fieldName;
        fld.DataFormatString = format;
        fld.HeaderText = headerText;
        fld.Display = !hidden;
        return fld;
    }


Please, any help is appreciated!  Getting past this road block is crucial!

2 Answers, 1 is accepted

Sort by
0
Felipe
Top achievements
Rank 1
answered on 11 Dec 2010, 02:03 AM
OK, i figured out part of the problem...

I forgot to flag the e.canceled=true in the detailtabledatabind event.

now the child table rows show up, but all of the fields just say "System.Data.DataRowView" instead of the actual data

any idea?
0
Felipe
Top achievements
Rank 1
answered on 11 Dec 2010, 07:04 AM
OK,

figured that issue out as well.

when I was creating the GridBoundColumns, i was adding them to the RadGrid AFTER i was setting their properties (DataField, format, header, etc.)

when i changed it so that I added it to the grid first, all worked fine!  

hope that helps someone else...
Tags
Grid
Asked by
Felipe
Top achievements
Rank 1
Answers by
Felipe
Top achievements
Rank 1
Share this question
or