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

RadGrid Pragmatically add columns

1 Answer 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nahwin
Top achievements
Rank 1
Nahwin asked on 17 Dec 2012, 05:09 AM
Hi there,

let's just straight to the case:

  • I Have a RadGrid Which i have declared in Design Time:
<telerik:RadGrid ID="grdResult" runat="server" Width="100%" AutoGenerateColumns="false"
        AllowPaging="true" PageSize="25" ShowHeader="true">
        <MasterTableView AutoGenerateColumns="False">
            <Columns>
            </Columns>
            <NoRecordsTemplate>
                <div> Search did not match with any of the existing data.</div>
            </NoRecordsTemplate>
        </MasterTableView>
    </telerik:RadGrid>
  • What i want to do with it is that i want to add the column dynamically while in run time, and the data is comming from Sharepoint List
GridBoundColumn col = new GridBoundColumn();
 
            col = new GridBoundColumn();
            col.HeaderText = SystemConstants.ExtenalName_Col1;
            col.DataField = SystemConstants.InternalName_Col1;
            grdResult.MasterTableView.Columns.Add(col);
 
            col = new GridBoundColumn();
            col.HeaderText = SystemConstants.ExtenalName_Col2;
            col.DataField = SystemConstants.InternalName_Col2;
            grdResult.Columns.Add(col);
 
            col = new GridBoundColumn();
            col.HeaderText = SystemConstants.ExtenalName_Col3;
            col.DataField = SystemConstants.InternalName_Col3;
            grdResult.MasterTableView.Columns.Add(col);
 
            col = new GridBoundColumn();
            col.HeaderText = SystemConstants.ExtenalName_Col4;
            col.DataField = SystemConstants.InternalName_Col4;
            grdResult.MasterTableView.Columns.Add(col);
 
            col = new GridBoundColumn();
            col.HeaderText = SystemConstants.ExtenalName_Col5;
            col.DataField = SystemConstants.InternalName_Col5;
            grdResult.MasterTableView.Columns.Add(col);
 
            col.HeaderText = "View"; //this will be hyperlink column
            grdResult.Columns.Add(col);

The Problem is:
  • When I have not bind it to any data why the does not show up at all? not event the header text?
  • How do i bind / populate the grid ? *tried the documentation but as far as i see everything is using database..

Notes:
  • I do not use the onNeedData event because i want to populate the grid after the user finish inputing the search criteria in the form and click the search button.

thanks in advance

1 Answer, 1 is accepted

Sort by
0
Nahwin
Top achievements
Rank 1
answered on 17 Dec 2012, 07:29 AM
Just found out the problem (*hopefully):

  • RadGrid need to be Binded (atleast call "RadGrid.DataBind()") to start showing its form / visibile
  • To populate from the list:
  • ListItemCollection itemColl = new ListItemCollection();
    itemColl = SPList.GetItems(SPQuery);
     
    if (itemcoll.count > 0) RadGrid.DataSource = itemColl.GetDataTable();
    else RadGrid.DataSource = new Object[0]; //to keep the empty grid appearing
    RadGrid.DataBind();
Tags
Grid
Asked by
Nahwin
Top achievements
Rank 1
Answers by
Nahwin
Top achievements
Rank 1
Share this question
or