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

How to remove a column in .cs ?

13 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bruno
Top achievements
Rank 1
Bruno asked on 25 Mar 2009, 02:38 PM
Hello

I programmatic create a Grid with AutoGenerateCollumns = true
after the Bind how can I remove some column ?

would be client or server side..

my PROC is returning these fields

cod,productdescription,value

and with autogenerate = true all fields will bind to the grid

can I remove "COD" after the grid is builded ?

NOTE : I Will Use the field COD as a relation field with the detail table.


theres a way to make it invisible?

thank you

13 Answers, 1 is accepted

Sort by
0
Bruno
Top achievements
Rank 1
answered on 25 Mar 2009, 03:23 PM
I tried with that

// Find the Grid 
RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
// Count The Collumns 
Response.Write(Grid1.Columns.Count.ToString()); 
 
//tried to remove 
Grid1.Columns.Remove("COD"); 
//or 
Grid1.Columns[0].visible = false
//or 
Grid1.Columns.FindByDataField("COD").visible = false

But the problem is that

the Grid1.Columns.Count is resturning no Columns, because the columns is being created at runtime

0
Bruno
Top achievements
Rank 1
answered on 25 Mar 2009, 09:13 PM
or some way to Hide a column and still have it accessable in client-side.
0
Princy
Top achievements
Rank 1
answered on 26 Mar 2009, 04:15 AM
Hello Bruno,

You can hide a specific column in the grid using the Display property, in the grids PreRender event and access it on client

aspx:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        RadGrid1.MasterTableView.GetColumn("COD").Display = false
    } 

Thanks
Princy.
0
Bruno
Top achievements
Rank 1
answered on 26 Mar 2009, 12:29 PM
I Dont have a RadGrid in my aspx.

in the aspx I have just a PlaceHolder

and I´m creating the RadGrid1 on the C#

how can I set the "OnPreRender" att ?
RadGrid RadGrid1 = new RadGrid(); 
 
        RadGrid1.ID = "RadGrid1"
        RadGrid1.DataSourceID = "SqlDataSource1"
 
        RadGrid1.MasterTableView.DataKeyNames = new string[] { VDataKey1 }; 
        RadGrid1.Width = Unit.Percentage(98); 
        RadGrid1.PageSize = 40; 
        RadGrid1.AllowPaging = true
        RadGrid1.AllowSorting = true
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric; 
        RadGrid1.PagerStyle.Position = GridPagerPosition.TopAndBottom; 
        RadGrid1.PagerStyle.NextPagesToolTip = "Próximas páginas"
        RadGrid1.PagerStyle.PrevPagesToolTip = "Páginas anteriores"
        RadGrid1.PagerStyle.NextPageText = "Próxima página"
        RadGrid1.PagerStyle.PrevPageToolTip = "Página Anterior"
        RadGrid1.AutoGenerateColumns = true
        RadGrid1.ShowStatusBar = true
        RadGrid1.ShowFooter = true
        RadGrid1.Skin = "Sunset"

then I have to put your code

 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
        grid.MasterTableView.GetColumn("nummes").Display = false
    }  

But my grid is AutoGenerated , and its not handling the event.

Thanxs



0
Andreas N.
Top achievements
Rank 2
answered on 26 Mar 2009, 01:15 PM
Hi,

just try to add the eventhandler in your code when creating the Grid like:

rGdEditor

 

.PreRender +=new EventHandler(rGdEditor_PreRender);

 


Then you can use the event.

Regards

Andreas
0
Bruno
Top achievements
Rank 1
answered on 26 Mar 2009, 02:03 PM
Ok Andreas that works well for my MasterTable:

protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
        grid.MasterTableView.GetColumn("nummes").Display = false
                            
    }  

The code above is working well, but now I need to do the same for the detail Tables as:

protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
        grid.MasterTableView.GetColumn("nummes").Display = false
         
        grid.MasterTableView.DetailTables[0].GetColumn("datakey").Visible = false
 
                    
    }  

and the last line of this code is returning an error,

 Cannot find a column with UniqueName 'datakey' 
Descrição: Ocorreu uma exceção não tratada durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para obter mais informações sobre o erro e onde foi originado no código. 
 
Detalhes da Exceção: Telerik.Web.UI.GridException: Cannot find a column with UniqueName 'datakey' 
 
Erro de Origem: 
 
Linha 46:         RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
Linha 47:         grid.MasterTableView.GetColumn("nummes").Display = false
Linha 48:         grid.MasterTableView.DetailTables[0].GetColumn("datakey").Visible = false
Linha 49:  
Linha 50:          
 
 
Arquivo de Origem: c:\Documents and Settings\SMA_01\Meus documentos\Visual Studio 2008\BI\Protegidos\Relatorios.aspx.cs    Linha: 48 
 

During PreRender the DetailTable already exists ??





0
Andreas N.
Top achievements
Rank 2
answered on 26 Mar 2009, 06:19 PM
Hi,

on prerender, the creation of the objects has finished, please have a look here:

http://www.telerik.com/help/aspnet-ajax/grdeventsequence.htm

My Assumption is, that you don't have a column named datakey in your detail table (as the exception says), If you do have a column with that name, try to use the correct upper/lower casing of the column name, as sometimes, the grid is sensitive to that.

regards

Andreas
0
Shinu
Top achievements
Rank 1
answered on 27 Mar 2009, 07:58 AM
Hi Bruno,

The above given code must be working fine. You are getting this may be because the column named 'datakey' may not be present in the Detail table's column collection as Andreas said or may be you are trying to access the wrong Detail table. I would suggest you to send the C# code where you are creating the Detail table.

Regards
Shinu
0
Bruno
Top achievements
Rank 1
answered on 27 Mar 2009, 12:18 PM
 private void DefineGridStructure(string VQuery1,string VDataKey1,string VQuery2,string VDataKey2,string VType2,string VQuery3,string VDataKey3) 
    { 
 
        string connString = ConfigurationManager.ConnectionStrings["coelbaConnectionString"].ConnectionString; 
                 
        SqlDataSource DataSource1 = new SqlDataSource(connString, VQuery1.ToString()); 
        DataSource1.ID = "SqlDataSource1"
         
        this.PlaceHolder1.Controls.Add(DataSource1); 
         
        RadGrid RadGrid1 = new RadGrid(); 
 
        RadGrid1.ID = "RadGrid1"
        RadGrid1.DataSourceID = "SqlDataSource1"
 
        RadGrid1.MasterTableView.DataKeyNames = new string[] { VDataKey1 }; 
        RadGrid1.Width = Unit.Percentage(98); 
        RadGrid1.PageSize = 40; 
        RadGrid1.AllowPaging = true
        RadGrid1.AllowSorting = true
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric; 
        RadGrid1.PagerStyle.Position = GridPagerPosition.TopAndBottom; 
        RadGrid1.PagerStyle.NextPagesToolTip = "Próximas páginas"
        RadGrid1.PagerStyle.PrevPagesToolTip = "Páginas anteriores"
        RadGrid1.PagerStyle.NextPageText = "Próxima página"
        RadGrid1.PagerStyle.PrevPageToolTip = "Página Anterior"
        RadGrid1.AutoGenerateColumns = true
        RadGrid1.ShowStatusBar = true
        RadGrid1.ShowFooter = true
        RadGrid1.Skin = "Sunset"
        RadGrid1.PreRender += new EventHandler(RadGrid1_PreRender); 
 
        RadGrid1.MasterTableView.PageSize = 40; 
        RadGrid1.MasterTableView.AllowMultiColumnSorting = true
 
        //Ckecking if is necessary to cretae a second level grid 
        //The Detail Table Index [0] 
        if (VQuery2 != ""
        { 
 
            SqlDataSource DataSource2 = new SqlDataSource(connString, VQuery2); 
            DataSource2.ID = "SqlDataSource2"
 
            SessionParameter SesParam1 = new SessionParameter(VDataKey1, VDataKey1); 
            DataSource2.SelectParameters.Add(SesParam1); 
            this.PlaceHolder1.Controls.Add(DataSource2); 
 
             
            //Detail table - Orders (II in hierarchy level) 
            GridTableView tableViewOrders = new GridTableView(RadGrid1); 
            tableViewOrders.DataSourceID = "SqlDataSource2"
            tableViewOrders.Width = Unit.Percentage(100); 
 
            tableViewOrders.DataKeyNames = new string[] { VDataKey2 }; 
            tableViewOrders.AutoGenerateColumns = true
 
            GridRelationFields relationFields = new GridRelationFields(); 
            relationFields.MasterKeyField = VDataKey1.ToString(); 
            relationFields.DetailKeyField = VDataKey1.ToString(); 
            tableViewOrders.ParentTableRelation.Add(relationFields); 
 
            tableViewOrders.PreRender += new EventHandler(tableViewOrders_PreRender); 
             
            RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders); 
 
 
 
            //the detail table index[1] 
            // III Level of the grid 
            if (VQuery3 != ""
            { 
 
                SqlDataSource DataSource3 = new SqlDataSource(connString, VQuery3); 
                DataSource3.ID = "SqlDataSource3"
 
                 
                if (VType2 == "DateTime"
                { 
                     SessionParameter SessParam2 = new SessionParameter(VDataKey2, System.Data.DbType.DateTime, VDataKey2); 
                     DataSource3.SelectParameters.Add(SessParam2); 
                     this.PlaceHolder1.Controls.Add(DataSource3); 
                } 
                if (VType2 == "String"
                { 
                    SessionParameter SessParam2 = new SessionParameter(VDataKey2, System.Data.DbType.String, VDataKey2); 
                    DataSource3.SelectParameters.Add(SessParam2); 
                    this.PlaceHolder1.Controls.Add(DataSource3); 
                } 
 
                 
 
                //grid 
 
                GridTableView tableViewOrderDetails = new GridTableView(RadGrid1); 
                tableViewOrderDetails.DataSourceID = "SqlDataSource3"
                tableViewOrderDetails.Width = Unit.Percentage(100); 
 
                //tableViewOrders.DataKeyNames = new string[] { VDataKey3 }; 
 
                GridRelationFields relationFields2 = new GridRelationFields(); 
                relationFields2.MasterKeyField = VDataKey2; 
                relationFields2.DetailKeyField = VDataKey2; 
                tableViewOrderDetails.ParentTableRelation.Add(relationFields2); 
                tableViewOrderDetails.AutoGenerateColumns = true
                //tableViewOrderDetails.AllowFilteringByColumn = true; 
 
 
                tableViewOrders.DetailTables.Add(tableViewOrderDetails); 
 
                 
            } 
 
 
        } 
         
 
        //Add the RadGrid instance to the controls 
        this.PlaceHolder1.Controls.Add(RadGrid1); 
 
         
    } 
 
 
    private void CarregaGrid(string RelId) 
    { 
        //start switch statement 
        switch (RelId) 
        { 
            case "34"
                DefineGridStructure("sp_totalAnoMes""nummes""exec sp_relPorData @nummes = @nummes""data","DateTime""select * from vwds where data = @data order by codatividade""");       
             break
            case "35"
                DefineGridStructure("select distinct(matrinspetor),nomeinspetor from vwds""matrinspetor""select * from vwds where matrinspetor = @matrinspetor ""codatividade","String""select * from vwds where codatividade = @codatividade order by codatividade""");       
            break
            
            default
                DefineGridStructure("""""""""","","");       
 
                break
        }//end switch statement 
 
 
    }
0
Bruno
Top achievements
Rank 1
answered on 27 Mar 2009, 12:22 PM
I Want to remove from MASterTable the column "nummes" and it is working fine with

protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
        switch (RelId) 
        { 
            case "34": 
                grid.MasterTableView.GetColumn("nummes").Display = false;  
                break; 
             
        } 

Now I Need to Remove the "datakey" column from my first level Detail Table I tought detailtable[0]

But it isn´nt working

protected void tableViewOrders_PreRender(object sender, EventArgs e) 
    { 
        RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1"); 
        grid.MasterTableView.DetailTables[0].GetColumn("data").Visible = false
 
    } 

All my Grids is being creted in C# I have just a PLACEHOLDER inside my aspx

My procedures is retuning the datafields and all fields are AutoGererated

 


0
Accepted
Princy
Top achievements
Rank 1
answered on 27 Mar 2009, 12:52 PM
Hello Bruno,

You can use the name property of the detail table  as shown in the following code and see if it helps:
aspx:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" > 
        <MasterTableView DataSourceID="SqlDataSource1"
           <DetailTables > 
             <telerik:GridTableView DataSourceID="SqlDataSource2" Name="Detail" runat="server" > 

cs:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    {  
      RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1");  
        foreach (GridDataItem _item in grid.Items) 
        { 
            if (_item.OwnerTableView.Name == "Detail"
            { 
                _item.OwnerTableView.GetColumn("data").Visible = false
            }            
        } 
    } 

Thanks
Princy.
0
Bruno
Top achievements
Rank 1
answered on 27 Mar 2009, 01:11 PM
I dont have the RadGrid in my aspx

how can I set the  Name="Detail" inside the .cs ?


0
Bruno
Top achievements
Rank 1
answered on 27 Mar 2009, 01:12 PM
OK I Found

  tableViewOrders.Name = "Detail";

Now It is working perfect!!!

Thank you all!!



Tags
Grid
Asked by
Bruno
Top achievements
Rank 1
Answers by
Bruno
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Andreas N.
Top achievements
Rank 2
Shinu
Top achievements
Rank 1
Share this question
or