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

Not Working: MasterTableView.GetItems(GridItemType.Header)

4 Answers 207 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mehdi
Top achievements
Rank 1
Mehdi asked on 25 Aug 2011, 12:40 PM
The function is returning no items. Following is a copy of my markup and code:

protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.DataSource = GetDataSource();
    RadGrid1.DataBind();
}
 
private DataTable GetDataSource()
{
    ...
}
 
private void AddGroupHeadersToGrid()
{
    GridItem[] header = RadGrid1.MasterTableView.GetItems(GridItemType.Header);
    //GridTHead head = ((GridTHead)header[0].Parent.Controls[0].Parent);
 
    GridHeaderItem newHeaderItem = new GridHeaderItem(RadGrid1.MasterTableView, 0, 0);
    newHeaderItem.Cells.Add(new GridTableHeaderCell());
    newHeaderItem.Cells[0].Text = "I'm a header.";
 
    //head.Controls.AddAt(0, newHeaderItem);
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    AddGroupHeadersToGrid();
}

The markup looks like this:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
    onprerender="RadGrid1_PreRender">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Question" DataField="Question" UniqueName="Question">
            </telerik:GridBoundColumn>
             
            <telerik:GridBoundColumn HeaderText="Score" DataField="MTDScore" UniqueName="MTDScore">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Area" DataField="MTDArea" UniqueName="MTDArea">
             
            <telerik:GridBoundColumn HeaderText="Score" DataField="QTDScore" UniqueName="QTDScore">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Area" DataField="QTDArea" UniqueName="QTDArea">
             
            <telerik:GridBoundColumn HeaderText="Score" DataField="YTDScore" UniqueName="YTDScore">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Area" DataField="YTDArea" UniqueName="YTDArea">
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

I would really appreciate if somebody can tell me what I'm doing wrong here!

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 25 Aug 2011, 12:49 PM
Hello Mehdi,

Here is the sample code that I tried to access HeaderItem in PreRender event which worked as expected.

C#:
protected  void RadGrid1_PreRender(object sender, EventArgs e)
{
  GridHeaderItem hitem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
}

Thanks,
Princy.
0
Mehdi
Top achievements
Rank 1
answered on 25 Aug 2011, 01:20 PM
Thanks for your reply princy.

I tried doing the same in a new project using the line you mentioned and it worked. I must be doing something wrong in my other project, then.

Thanks again,
Mehdi

EDIT: I just got the issue. My GetDataSource function wasn't returning anything. And I spent 3 hours on this issue!
0
Renee
Top achievements
Rank 1
answered on 14 Oct 2013, 04:34 PM
How do you do this for a detail table?  This code: 

GridHeaderItem headerItem = (GridHeaderItem)AppsGrid.MasterTableView.DetailTables[0].GetItems(GridItemType.Header)[0];

returns an index out of bounds error.
0
Princy
Top achievements
Rank 1
answered on 15 Oct 2013, 05:25 AM
Hi Renee,

Please try the following code snippet to access the detail table header.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
 if ((e.Item is GridHeaderItem) && (e.Item.OwnerTableView.Name == "DetailTableName"))
  {
    GridHeaderItem header = (GridHeaderItem)e.Item;
    LinkButton button = header["UniqueName"].Controls[0] as LinkButton;
    button.Text = "Customized HeaderText";
  }
}

Thanks,
Princy
Tags
Grid
Asked by
Mehdi
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Mehdi
Top achievements
Rank 1
Renee
Top achievements
Rank 1
Share this question
or