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

dynamically adding griddataitem

6 Answers 360 Views
Grid
This is a migrated thread and some comments may be shown as answers.
debbie f
Top achievements
Rank 1
debbie f asked on 15 May 2008, 10:37 PM
I am converting an existing datagrid to a radgrid.  During PreRender, I dynamically add a new row and set the cell with some text with a colspan to match the visible columns.  For some reason, this does not work with a radgrid.  The new row is created (I can view source of the rendered html and see the <tr> tags), but the cell is not added to the row when rendered.

Here is my code snippet:

int cols = 11;
foreach (GridItem dItem in gridResults.Items)
{
    if (dItem.ItemType == GridItemType.Item || dItem.ItemType == GridItemType.AlternatingItem)
        {
            GridDataItem item = new GridDataItem(gridResults.MasterTableView, 0, 0, dItem.ItemType);
               
        TableCell cell = new TableCell();
                cell.ColumnSpan = cols;
                cell.HorizontalAlign = HorizontalAlign.Justify;
                cell.Text = ((DataRowView)dItem.DataItem)["Description"].ToString();

              item.Cells.Add(cell);
                       
                int iPos = 0;
                foreach (GridItem dgitem in dItem.Parent.Controls)
                {
                    if (dgitem == dItem)
                        {
                            dItem.Parent.Controls.AddAt(iPos + 1, item);
                                break;
                        }
                        iPos++;
                }
         }
}

Suggestions?  I am looking at the nestedviewtemplate, but I don't want users to have access to expand/collapse the section.  I don't see a way to hide the expansion control in the grid.

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 May 2008, 05:46 AM
Hi,

You can refer the following help article to get details about hiding the expand/collapse button.
Hiding expand/collapse images when no records

Shinu.
0
debbie f
Top achievements
Rank 1
answered on 19 May 2008, 08:31 PM
But there is no reasoning for why the output of the cells should work?  It's just very odd that it will add the row to the html output during prerender, but not a cell.
0
Yavor
Telerik team
answered on 20 May 2008, 05:27 AM
Hi debbie f,

Basically, it is not recommended to add rows in a similar manner. Another approach that you can take would be to include an empty row to the underlying datasource, at the relevant position. Subsequently, you can span the row as per your needs.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
debbie f
Top achievements
Rank 1
answered on 19 Jun 2008, 05:23 PM
I'm not having much luck in hiding the expand image.  I've setup my radgrid with a nestedviewtemplate as follow:

<telerik:RadGrid ID="gridResults" Runat="server" AllowSorting="True" AllowPaging="True" AllowFilteringByColumn="True"
                    OnSortCommand="mSortGrid" OnPageIndexChanged="mChangePage" OnPreRender="mAddDescription"
                    OnItemDataBound="gridItemDataBound" OnItemCommand="gridResults_ItemCommand" OnNeedDataSource="gridResults_NeedDataSource">
                        <MasterTableView AllowNaturalSort="false" DataKeyNames="id" HierarchyLoadMode="ServerBind" HierarchyDefaultExpanded="true">
                            <Columns>
                                <telerik:GridTemplateColumn FilterListOptions="VaryByDataType" DataType="System.Int32" SortExpression="id" DataField="id" UniqueName="id" HeaderText="ID" ItemStyle-VerticalAlign="Top">
                                    <ItemTemplate>
                                        <asp:LinkButton CssClass="smallLink" ID="LinkButton2" runat="server" OnCommand="mLoadJob">
                                            <%# DataBinder.Eval(Container.DataItem, "id") %>
                                         </asp:LinkButton><img src="images/1x1.gif" width="1" alt="spacer" title="spacer" />
                                     </ItemTemplate>
                                </telerik:GridTemplateColumn>
                            </Columns>
                            <NestedViewTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
                            </NestedViewTemplate>
                         </MasterTableView>
                     </telerik:RadGrid>

I'm able to iterate through the grid items and set the visibility of the nested template based on permission as so:
GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView);
            foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
            {
                nestedViewItem.Visible = visible; //user has permission?
            }
I just have not figured out how to ALWAYS disable the expand/collapse column no matter what.  I've looked at the code you suggested but that is not working for my scenario.  I must be missing something.  Any help would be appreciated.

0
Yavor
Telerik team
answered on 23 Jun 2008, 08:06 AM
Hello debbie,

Attached to this message, is a sample application, which handles the desired functionality.
I hope it helps.

All the best,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
debbie f
Top achievements
Rank 1
answered on 24 Jun 2008, 03:10 PM
That's perfect.  I had found another suggestion for implementation during itemdatabound, but your solution requires less code.  Thanks so much.
Tags
Grid
Asked by
debbie f
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
debbie f
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or