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

Dynamic Column Header Text Disapper

2 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mohamed
Top achievements
Rank 1
Mohamed asked on 20 Jan 2011, 11:11 AM
hi,

i am trying to use rad grid to create a dynamic control that will bind some data.
the scenario will be like that:
1- i am already declaring a rad grid in the aspx page

<telerik:RadGrid ID="GrdDocuments" runat="server" Skin="WebBlue" dir="rtl" PageSize="10"
            OnItemCreated="GrdDocuments_ItemCreated" OnNeedDataSource="GrdDocuments_NeedDataSource"
            OnDetailTableDataBind="GrdDocuments_DetailTableDataBind"
            AllowMultiRowSelection="false" AllowFilteringByColumn="true" AutoGenerateColumns="false">
            <ClientSettings EnableRowHoverStyle="true">
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
            <MasterTableView AutoGenerateColumns="false" DataKeyNames="arcId" Dir="RTL">
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="40px"></HeaderStyle>
                </ExpandCollapseColumn>
                <PagerStyle AlwaysVisible="true" Mode="NextPrevAndNumeric" />
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="arcId,arcFileName" AllowFilteringByColumn="false" Name="Files" Caption="Document Files" Width="100%" AutoGenerateColumns="false" Dir="RTL">
                        <Columns>
                            <telerik:GridBoundColumn SortExpression="arcOrgName" HeaderText="File Name"
                                DataField="arcOrgName" UniqueName="arcOrgName" >
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="arcPageCount" HeaderText="Page Count"
                                DataField="arcPageCount" UniqueName="arcPageCount">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="arcFileSize" HeaderText="File Size"
                                DataField="arcFileSize" UniqueName="arcFileSize">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="arcCreatorUser" HeaderText="Creator"
                                DataField="arcCreatorUser" UniqueName="arcCreatorUser">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="arcCreationDate" HeaderText="Creation Date"
                                DataField="arcCreationDate" UniqueName="arcCreationDate">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
            </MasterTableView>
        </telerik:RadGrid>

so as you can see the parent Grid has no column, but the detailed Grid is statically defined.

now in the code behind i am trying to define the parent Grid columns programmatically using some map files.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Dictionary<string, string> MappingDefinition = new Dictionary<string, string>();
        MappingDefinition.Add("AAAA", "ColumnName_1");
        MappingDefinition.Add("BBBB", "ColumnName_2");
        MappingDefinition.Add("CCCC", "ColumnName_3");
        MappingDefinition.Add("DATE", "ColumnName_4");
        InitializeGrid(MappingDefinition);
        GrdDocuments.Rebind();
    }
}
 
protected void GrdDocuments_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    if (!e.IsFromDetailTable)
    {
        DataTable _DataTable = _Object.GetDataTable();
        GrdDocuments.DataSource = _DataTable;
    }
}
 
private void InitializeGrid(Dictionary<string, string> MappingDefinition)
{
    string ErrorMessage= "";
    try
    {
        foreach (KeyValuePair<string, string> pair in MappingDefinition)
        {
            if (pair.Key.ToLower().Contains("date"))
            {
                GridDateTimeColumn dateColumn;
                dateColumn = new GridDateTimeColumn();
                dateColumn.DataField = pair.Value;
                dateColumn.HeaderText = pair.Value;
                dateColumn.AllowFiltering = true;
                dateColumn.AllowSorting = true;
                GrdDocuments.MasterTableView.Columns.Add(dateColumn);
            }
            else
            {
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = pair.Value;
                boundColumn.HeaderText = pair.Value;
                boundColumn.AllowFiltering = true;
                boundColumn.AllowSorting = true;
                GrdDocuments.MasterTableView.Columns.Add(boundColumn);
            }
        }
    }
    catch (Exception ex)
    {
        ErrorMessage= ex.Message;
    }
}

it is all working fine until i expand any row, the parent grid header text is disapper, but the column remains, i don't know why this happen.
please review the attached images

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 20 Jan 2011, 12:10 PM
Hello Mohamed,

When creating RadGrid on Page_Load event, the columns or detail tables should be added to the corresponding collection first and then values for the properties of this instance should be set. Try the following approach to add columns to RadGrid.

C#:
GridBoundColumn boundColumn;
boundColumn = new GridBoundColumn();
GrdDocuments.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = pair.Value;
boundColumn.HeaderText = pair.Value;
boundColumn.AllowFiltering = true;
boundColumn.AllowSorting = true;

 I am referring the fololwing documentation. Please give a try with this.
Programmatic creation

Thanks,
Princy.
0
Mohamed
Top achievements
Rank 1
answered on 20 Jan 2011, 02:55 PM
Dear Princy,
Thanks for your help, but i have one question "How did you figure it out?" ;)
Tags
Grid
Asked by
Mohamed
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mohamed
Top achievements
Rank 1
Share this question
or