Column disappearing from RadGrid

3 posts, 0 answers
  1. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 01 Apr 2009 Link to this post

    Hi,

    I am programmaticaly creating RadGrid columns. This works fine and the data is right, but when I change the page of the RadGrid my columns disappear. Please advise either what I am doing wrong, or what the problem is.

    public partial class ListAdvertisements : System.Web.UI.Page 
        { 
            static DataSet ds; 
            static DataSet ds2; 
            static DataSet ds3; 
            static string UserID; 
     
            protected void Page_Load(object sender, EventArgs e) 
            { 
                if (!IsPostBack) 
                { 
                    UserID = Request.QueryString["userid"]; 
     
                    SubSonic.SqlQuery sql = new SubSonic.Select("ProfileID"
                                                            "Title as Profile"
                                                        .From(Tables.Ma_Profiles) 
                                                        .Where(Ma_Profiles.InactiveColumn) 
                                                        .IsEqualTo(0); 
                    ds = sql.ExecuteDataSet(); 
     
                    GridBoundColumn bound; 
                    bound = new GridBoundColumn(); 
                    grdAdvertisements.MasterTableView.Columns.Add(bound); 
                    bound.DataField = "ProfileID"
                    bound.Visible = false
                    bound = new GridBoundColumn(); 
                    grdAdvertisements.MasterTableView.Columns.Add(bound); 
                    bound.DataField = "Profile"
                    bound.Visible = true
                    bound.HeaderText = "Profile"
     
                    SubSonic.SqlQuery sql2 = new SubSonic.Select("ID"
                                                             "Description"
                                                    .From(Tables.Vacancy_Advert_Types) 
                                                    .Where(Vacancy_Advert_Types.InactiveDateColumn) 
                                                    .IsNull() 
                                                    .Or(Vacancy_Advert_Types.InactiveDateColumn) 
                                                    .IsGreaterThan(DateTime.Now); 
                    ds2 = sql2.ExecuteDataSet();                 
     
                    foreach (DataRow dr in ds2.Tables[0].Rows) 
                    { 
                        DataColumn dc = new DataColumn(); 
                        bound = new GridBoundColumn(); 
                        grdAdvertisements.MasterTableView.Columns.Add(bound); 
                        bound.DataField = dr[1].ToString() + "id"
                        bound.Visible = false
                        dc.ColumnName = dr[1].ToString() + "id"
                        ds.Tables[0].Columns.Add(dc); 
     
                        DataColumn dc2 = new DataColumn(); 
                        dc2.ColumnName = dr[1].ToString(); 
                        ds.Tables[0].Columns.Add(dc2); 
                        string colname = "Advert"
                        GridTemplateColumn templateColumn = new GridTemplateColumn(); 
                        templateColumn.ItemTemplate = new MyTemplate(colname, UserID); 
                        templateColumn.HeaderText = colname; 
                        grdAdvertisements.MasterTableView.Columns.Add(templateColumn); 
                    } 
     
                    GridButtonColumn button = new GridButtonColumn(); 
                    grdAdvertisements.MasterTableView.Columns.Add(button); 
                    button.ButtonType = GridButtonColumnType.ImageButton; 
                    button.CommandName = "Edit"
                    button.Visible = true
                    button.HeaderText = "View all"
                    button.ImageUrl = "~/Images/Buttons/S03edit32.gif"
                } 
            } 
     
            protected void FillGrid() 
            { 
     
                SubSonic.SqlQuery sql = new SubSonic.Select("ProfileID"
                                                            "Title as Profile"
                                                        .From(Tables.Ma_Profiles) 
                                                        .Where(Ma_Profiles.InactiveColumn) 
                                                        .IsEqualTo(0); 
                ds = sql.ExecuteDataSet(); 
     
                SubSonic.SqlQuery sql2 = new SubSonic.Select("ID"
                                                             "Description"
                                                    .From(Tables.Vacancy_Advert_Types) 
                                                    .Where(Vacancy_Advert_Types.InactiveDateColumn) 
                                                    .IsNull() 
                                                    .Or(Vacancy_Advert_Types.InactiveDateColumn) 
                                                    .IsGreaterThan(DateTime.Now); 
                ds2 = sql2.ExecuteDataSet(); 
     
                foreach (DataRow dr in ds2.Tables[0].Rows) 
                { 
                    DataColumn dc = new DataColumn(); 
                    dc.ColumnName = dr[1].ToString() + "id"
                    ds.Tables[0].Columns.Add(dc); 
     
                    DataColumn dc2 = new DataColumn(); 
                    dc2.ColumnName = dr[1].ToString(); 
                    ds.Tables[0].Columns.Add(dc2); 
                } 
     
                foreach (DataRow dr in ds.Tables[0].Rows) 
                { 
                    string sql3 = "select a.adverttype, c.description from vacancy_advert a " + 
                                  "inner join vacancy_advert_to_profiles b on a.id = b.advertid " + 
                                  "inner join vacancy_advert_types c on a.adverttype = c.id " + 
                                  "where b.profileid = @profileid"
     
                    QueryCommand qc = new QueryCommand(sql3, "SSBASE"); 
     
                    qc.AddParameter("profileid", Convert.ToInt32(dr[0]), DbType.Int32, ParameterDirection.Input); 
     
                    DataSet ds4 = DataService.GetDataSet(qc); 
     
                    if (ds4.Tables[0].Rows.Count > 0) 
                    { 
                        int j = 0; 
                        for (int i = 0; i < ds4.Tables[0].Rows.Count * 2; i = i + 2) 
                        { 
                            dr[2 + i] = ds4.Tables[0].Rows[j][0].ToString(); 
                            dr[3 + i] = ds4.Tables[0].Rows[j][1].ToString(); 
                            j++; 
                        } 
                    } 
                } 
     
                grdAdvertisements.DataSource = ds.Tables[0]; 
            } 
     
            protected void grdAdvertisements_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
            { 
                FillGrid(); 
            } 
     
            protected void grdAdvertisements_PreRender(object sender, EventArgs e) 
            { 
                 
                GridSortExpression sortExpr = new GridSortExpression(); 
                sortExpr.FieldName = "ProfileID"
                sortExpr.SortOrder = GridSortOrder.Ascending; 
                grdAdvertisements.MasterTableView.SortExpressions.AddSortExpression(sortExpr); 
            } 
     
            protected void btnNewAdvert_Click(object sender, ImageClickEventArgs e) 
            { 
                Response.Redirect("~/Recruitment/Advertisements/CreateAdvert.aspx?userid=" + UserID); 
            } 
     
            protected void grdAdvertisements_ItemCommand(object source, GridCommandEventArgs e) 
            { 
                if (e.CommandName == "Edit"
                { 
                    Response.Redirect("~/Recruitment/Advertisements/EditAdverts.aspx?userid=" + UserID + "&profileid=" + e.Item.Cells[2].Text); 
                } 
            } 
     
            protected void grdAdvertisements_ItemDataBound(object sender, GridItemEventArgs e) 
            { 
                if (e.Item.DataItem != null
                { 
                    for (int i = 0; i < e.Item.Cells.Count; i++) 
                    { 
                        if (e.Item.Cells[i].Controls.Count == 1) 
                        { 
                            if (e.Item.Cells[i].Controls[0] is ImageButton) 
                            { 
                                if (((ImageButton)e.Item.Cells[i].Controls[0]).ID == "imgAdvert"
                                { 
                                    if (e.Item.Cells[i - 1].Text == "&nbsp;"
                                    {                                     
                                        ((ImageButton)e.Item.Cells[i].Controls[0]).ImageUrl = "~/Images/Buttons/S12edit32.gif"
                                        ((ImageButton)e.Item.Cells[i].Controls[0]).Enabled = false
                                    } 
                                } 
                            } 
                        } 
                    } 
                } 
            } 
     
            protected void grdAdvertisements_ItemCreated(object sender, GridItemEventArgs e) 
            { 
     
            } 
        } 
     
        public partial class MyTemplate : ITemplate 
        { 
            protected ImageButton img; 
            private string colname; 
            static string UserID2; 
     
            public MyTemplate(string cName, string UserID) 
            { 
                colname = cName; 
                UserID2 = UserID; 
            } 
     
            public void InstantiateIn(System.Web.UI.Control container) 
            { 
                img = new ImageButton(); 
                img.Click += new ImageClickEventHandler(img_Click); 
                img.ID = "imgAdvert"
                img.ImageUrl = "~/Images/Buttons/S06preview32.gif"
                img.Enabled = true
                container.Controls.Add(img); 
            } 
     
            void img_Click(object sender, ImageClickEventArgs e) 
            { 
     
            } 
        } 

  2. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 03 Apr 2009 Link to this post

    Hello Marcel,

    I followed your scenario in order to replicate the issue but to no avail. Please find attached a small runnable application with the implementation request by you. Give it a try and see if it works for you or if I am leaving something out.

    I hope this helps.

    Greetings,
    Pavlina
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 06 Apr 2009 Link to this post

    Thank you for your reply, I will have a look and let you know.
Back to Top