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

[Solved] how to inherit from another grid?

11 Answers 277 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mac
Top achievements
Rank 1
mac asked on 29 Mar 2009, 09:28 PM
hello. I have already review the docs on how to programmatically design a radgrid in code behind but don't think it is supporting my scenario.
I have a grid desiged in basic source mode. <radgrid><mastertable><columns></columns> etc..
I have been requested to design three more grids in the same container page that are identical in nature but only have different embedded filter expressions. same DatasourceID, columns etc..
Is there any way I can just drop something like 

 

 

 

 

<telerik:RadGrid ID="RadGrid3" runat="server">  
</telerik:RadGrid> 
 

in my container and then run something like 

 

 If Not IsPostBack Then  
   For Each item As GridColumn In RadGrid1.MasterTableView.Columns  
      RadGrid3.MasterTableView.Columns.Add(item)  
   Next  
RadGrid3.MasterTableView.FilterExpression = "SomeCol= 'SomeValue'" 
RadGrid3.DataSourceID = RadGrid1.DataSourceID  
RadGrid3.DataBind()  
 End If  
 
This works at runtime but then on any rebind or paging change the grid returns with System.Data.DataRowView.
hoping there is an easy for this vs creating custom class file. or new object with a bunch of different properties.

 

 

11 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Mar 2009, 06:53 AM
Hi Mac,

In the above post you have mentioned that the all the other Grids are having the same Grid structure and using the same DataSourceID as RadGrid1. If so you just need to set the DataSourceID property of the Radgrid3 and set the AutoGenerateColumns property to true.

ASPX:

<
telerik:RadGrid ID="RadGrid2" AllowPaging="true"  AutoGenerateColumns="true" PageSize="5" AllowSorting="true" runat="server"
            </telerik:RadGrid> 

VB:
 
 
     Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) 
         If Not IsPostBack Then 
             RadGrid2.DataSourceID = RadGrid1.DataSourceID 
             
         End If 
     End Sub 
 


Thanks
Shinu
0
mac
Top achievements
Rank 1
answered on 30 Mar 2009, 03:22 PM
sorry no that would not work. I have template columns, custom headertext etc. I think I must have misrepresented my question.
Radgrid1 has all the columns defined as i wish with autogeneratecolumns = false.

I want to put three more grids on the page, all with the same structure and datasource, but with alternate filterexpressions. Is there a method to inherit the columns/settings from radgrid 1 into Radgrid2? As stated, I can set the DS and columns like I have, but lose it all when paging or searching against the datasource.
thanks
mac
0
Sebastian
Telerik team
answered on 01 Apr 2009, 02:55 PM
Hello mac,

I do not think that the inheritance you request is attainable. Rather you may consider duplicating the configuration in a separate grid instances to achieve your goal.

In addition, I recommend you use advanced binding with NeedDataSource handing for your grids instead of simple binding with DataBind() calls.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
mac
Top achievements
Rank 1
answered on 02 Apr 2009, 05:51 AM

hi
I threw  a few more hours at this and found that it is feasible to a degree.. would love to know if there is another better method however.
it seems that if you loop through the columns in radgrid1, you can easily do a radgrid2.mastertable.columns.add(column) in the loop.
if you integrate with the needsdatasource, then indeed the grid is perfect in paging and data. one really weird thing that i found though was that no matter where i stuck it, if i needed to hide say a command button column. it would hide on both grids.
ie in radgrid2.prerender
radgrid2.mastertableview.getcolumn("col1").display = false ' would cause both grid col 1's to disappear. odd.
I also play with iterating through the items collection just using a DataItem("col1").visible=false and that made the columnar data go away but not the header :( . So i went old school.
here is my code.

VB  
  Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        For Each col As Telerik.Web.UI.GridColumn In RadGrid1.MasterTableView.Columns  
            If col.UniqueName <> "col1" Then 'this effectively does kill the col1 in the next grid  
                RadGrid2.MasterTableView.Columns.Add(col)  
            End If  
 
        Next  
        'When enabled causes both grids to lose col1   RadGrid2.MasterTableView.GetColumn("col1").Display = False 
        RadGrid1.DataSource = fns.getdata("select * from test")  
    End Sub  
 
    Protected Sub RadGrid2_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid2.Init  
        RadGrid2.Width = RadGrid1.Width  
    End Sub  
 
    Protected Sub RadGrid2_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource  
        RadGrid2.DataSource = fns.getdata("Select top 4* from test")  
    End Sub  
 
source  
 
source  
       <telerik:RadGrid ID="RadGrid1" Width="500" runat="server" GridLines="None">  
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="keyid">  
                <Columns> 
                    <telerik:GridBoundColumn DataField="keyid" DataType="System.Guid" HeaderText="keyid" 
                        ReadOnly="True" SortExpression="keyid" UniqueName="keyid">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="col1" HeaderText="col1" SortExpression="col1" 
                        UniqueName="col1">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="col2" HeaderText="col2" SortExpression="col2" 
                        UniqueName="col2">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="col3" HeaderText="col3" SortExpression="col3" 
                        UniqueName="col3">  
                    </telerik:GridBoundColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
        <telerik:RadGrid ID="RadGrid2" runat="server" GridLines="None">  
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="keyid">  
                
            </MasterTableView> 
        </telerik:RadGrid> 
 
0
Sebastian
Telerik team
answered on 02 Apr 2009, 08:55 AM
Hello mac,

It is odd that changing the visibility of a particular column in radgrid2 affects the other grid instance as well. However, I noticed that you add columns to the second grid dynamically from the NeedDataSource handler of the first one. Note that this is not a proper approach and you will need to modify your logic to do that inside the PageInit or PageLoad handler of the page as explained here.
 
Thus the viewstate of the grid/its columns will remain consistent.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
mac
Top achievements
Rank 1
answered on 02 Apr 2009, 02:42 PM
yeah i had pasted incorrectly somehow.. this is what I am using.. you will note that I have now changed the script slightly with a
col.uniqueName = "RG2"  & col.uniquename... this says to me that now radgrid2 has completely differently named columns... however the problem remains.
 Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        RadGrid1.DataSource = fns.getdata("select * from test")  
    End Sub  
 
    Protected Sub RadGrid2_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid2.Init  
        For Each col As Telerik.Web.UI.GridColumn In RadGrid1.MasterTableView.Columns  
            ' If col.UniqueName <> "col1" Then  
            col.UniqueName = "RG2" & col.UniqueName  
            RadGrid2.MasterTableView.Columns.Add(col)  
            '  End If  
 
        Next  
         
    End Sub  
 
    Protected Sub RadGrid2_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource  
        RadGrid2.DataSource = fns.getdata("Select top 4* from test")  
    End Sub  
 
 
    Protected Sub RadGrid2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid2.PreRender  
           RadGrid2.MasterTableView.GetColumn("RG2col1").Display = False 
    End Sub 
0
Sebastian
Telerik team
answered on 03 Apr 2009, 01:31 PM
Hello Mac,

When you have your grids defined in the aspx of the page, the proper means to add columns programmatically is inside !Page.IsPostBack condition inside the PageLoad handler of the page as explained in the help article. Another approach would be to instantiate both grids entirely programmatically inside the PageInit handler.

Thus you will keep their viewstate consistent and their event lifecycle intact.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
mac
Top achievements
Rank 1
answered on 03 Apr 2009, 03:13 PM
In this help article http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
it clearly indicates

The VB.NET language gives another option (unavailable in C#). You can have the grid instance created on the page and use the RadGrid.Init event to dynamically create its structure.

You can also note the sample does not indicate to use ispostback when using the .init event. IF you use ispostback, the grid falls apart on paging. 
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  DefineGridStructure() ' does not have postback in the sub
End
Sub
I'm pretty confident that I am building the grid columns in the correct location. What I am not confident or understanding is how to give the second grid uniquenames different from the first.
my sample does work if tested. what does not work is the columns getting a unique name. I would really appreciate if we could get this working as I have grids that use tons of customization and it would be so much easier if I could just change one on the page and have the other 3 inherit those changes. converting to custom control is an option but not one I am wanting to use.
this code fully works other than the weird column naming issue. I am pretty confident that if I can get the columns to be unique, then it will work.
Alternatively  a working sample would also be appreciated..in vb.. .

 

    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        RadGrid1.DataSource = _data.dt("Select * from account")  
    End Sub  
 
    Protected Sub Radgrid2_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles Radgrid2.NeedDataSource  
        Radgrid2.DataSource = _data.dt("Select * from account")  
    End Sub  
 
    Protected Sub Radgrid2_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Radgrid2.Init  
        For Each col As GridColumn In RadGrid1.MasterTableView.Columns  
            Dim bc As GridColumn  
            bc = col 
            bc.UniqueName = "RG2" & col.UniqueName  
            Radgrid2.MasterTableView.Columns.Add(bc)  
        Next  
        Radgrid2.MasterTableView.GetColumn("RG2id").Display = False 
    End Sub  
 
source  
 
  <telerik:RadGrid  ID="RadGrid1" AllowPaging="true" AutoGenerateColumns="false" runat="server">  
              <MasterTableView PageSize="10">  
              <Columns> 
              <telerik:GridBoundColumn DataField="id" HeaderText="ID" /> 
              <telerik:GridBoundColumn HeaderText="Name" DataField="Name" /> 
              <telerik:GridBoundColumn HeaderText="Account" DataField="AccountNo" /> 
              <telerik:GridBoundColumn HeaderText="Balance" DataField="Balance" /> 
              </Columns> 
              </MasterTableView> 
              </telerik:RadGrid> 
                
              <telerik:RadGrid ID="Radgrid2" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" runat="server">  
              </telerik:RadGrid> 
 


 

0
Sebastian
Telerik team
answered on 07 Apr 2009, 12:37 PM
Hello mac,

For your convenience I prepared a sample project which illustrates how to create two grid instances programmatically. The grids are added statically on the page and their column structure is defined inside the OnInit handler. The columns in the second grid are copies of those generated in the first and added dynamically to the Columns collection of the second grid.

An important detail to keep the viewstate of the grids consistent is to set MasterTableView -> EnableColumnsViewState to false as shown in the demo.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
mac
Top achievements
Rank 1
answered on 07 Apr 2009, 02:38 PM
Hi Sebastian
Thanks for the sample. Like I have been saying though.. the problem is NOT with having to create the columns at runtime. My sample scripts are doing the same thing. The problem is the uniqueName of columns in Radgrid2.
Take your sample and put this in anywhere , init, prerender, load etc.. no matter where you put it..it hide the Address column in radgrid1 too.

RadGrid2.MasterTableView.GetColumn("Address").Display = False

 

My assumption is that I am targeting Radgrid2..
however this affects both grids every time. At least..that is what happens for me :(

 

 

0
Accepted
Sebastian
Telerik team
answered on 10 Apr 2009, 02:27 PM
Hello Mac,

Indeed you are right. This happens because the reference to the first column is retained and when you apply settings for it they are reflected in the two grid instances.

To avoid the unexpected result you need to create a clone of the relevant column and add the cloned copy to the second grid after customizing its appearance, namely:

    Protected Sub PopulateGrid2OnPageInit()  
        For Each column As GridColumn In RadGrid1.MasterTableView.Columns  
            If column.UniqueName = "Address" Then 
                Dim clonedColumn As GridColumn = column.Clone()  
                clonedColumn.Display = False 
                RadGrid2.MasterTableView.Columns.Add(clonedColumn)  
            Else 
                RadGrid2.MasterTableView.Columns.Add(column)  
            End If 
        Next 
    End Sub 

I am attaching the updated version of the project to this message for further reference.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
mac
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
mac
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or