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

GroudBoundColumn duplicates on every server command used

4 Answers 31 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RJ
Top achievements
Rank 1
RJ asked on 07 Jan 2010, 03:35 PM
Hi, Im having problem with my gridBoundColumn. It duplicates whenever there some server command thats been executed. The only column thats not duplicating is my gridTemplateColumn. Im populating my columns purely in code behind. Radgrid was defined on aspx with several properties but without any column. The funny thing about it is that I use to count my columns before it adds columns to the grid. Before adding any columns value is 0 and after adding all the columns value is just 8. But if Im gonna count all of the columns (1 template column which theres no duplication) and there are 5 sets of same 7 columns  if I it executes some server codes or post back 5x.. Its like at first load it shows correct 8 columns, and when I do change my dropdown it will have some postback and then my grid count becomes (1 templatecolumn)  and 2 sets of 7 so total columns is 15. and when I do change again my dropdown it will have (1 template column) and 3 sets of 7 columns Its like this:
Chkbox Tempcol|Fname|Lname|TelNo|Add|City|State|Zip|Fname|Lname|TelNo|Add|City|State|Zip|Fname|Lname|TelNo|Add|City|State|Zip

Heres my code
<rad:radgrid id="RadGrid1" runat="server" Height="200px" AllowMultiRowSelection="True" ForeColor="White" AllowMultiRowEdit="True" Skin="outlook" width="985px" ItemStyle-BackColor="WhiteSmoke" AlternatingItemStyle-BackColor="White" BorderStyle="None" GridLines="Both">     
    
<AlternatingItemStyle Font-Size="Small" Font-Names="Arial Narrow" HorizontalAlign="Left" VerticalAlign="Middle" BorderStyle="None" CssClass="AlternatingItemRow" BackColor="White"></AlternatingItemStyle>    
                             
<MasterTableView DataKeyNames="ID" AllowAutomaticInserts="True" EditMode="InPlace" CommandItemDisplay="Top" AutoGenerateColumns="False">     
    
<CommandItemStyle BackColor="#FFE39A"></CommandItemStyle>    
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>    
   
    
   
   
                                 
<CommandItemTemplate>    
    <table class="MyCommandItemClass" width="100%">     
    <tr>    
                                        <td align="left" style="width: 75%; padding-top:4px">     
                                            <asp:Label ID="lblPN" runat="server" Text="PlanName" CssClass="basicfontbold" ForeColor="black" Font-Size="12px" Visible="true"></asp:Label>    
                                                 
                                                 
                                        </td>    
                                              
                                        <td align="right">     
                                        <asp:Button id="btnAdd" Runat="server" CommandName="InitInsert"  ToolTip="Add New Record" Text="Add" CssClass="ButtonGray" Visible='<%# Not RadGrid1.MasterTableView.IsItemInserted and showInsert %>' />    
                                                 
                                            <asp:Button ID="PerformInsert" runat="server" CommandName="PerformInsert"  ToolTip="Save" CssClass="ButtonRed" Text="Save" Visible='<%# RadGrid1.MasterTableView.IsItemInserted and showInsert %>'/>     
                                            <asp:Button ID="CancelInsert" runat="server" CommandName="CancelInsert" ToolTip="Cancel" CssClass="ButtonGray" Text="Cancel" Visible='<%# RadGrid1.MasterTableView.IsItemInserted and showInsert %>'/>     
                                                 
                                           <asp:Button id="Linkbutton1" Runat="server" CommandName="DeleteChecked" Text="Delete" CssClass="ButtonGray" Visible='<%# Not RadGrid1.MasterTableView.IsItemInserted and showDelete %>' Enabled='<%# showDelete %>'/>     
                                             
                                             
                                                 
                                        </td>    
                                                                            </tr>    
                                </table>    
                            </CommandItemTemplate>    
                            <ExpandCollapseColumn>    
                                <HeaderStyle Width="20px"></HeaderStyle>    
                            </ExpandCollapseColumn>    
                            <FooterStyle BorderStyle="None" BackColor="#FFE39A"></FooterStyle>    
                        </MasterTableView>    
                             
                        <HeaderStyle Font-Size="Small" Font-Names="Arial Narrow" Font-Bold="True" ForeColor="White" ></HeaderStyle>     
                        <SelectedItemStyle CssClass="RadGridSelectedItem"></SelectedItemStyle>    
                        <ItemStyle Font-Size="Small" Font-Names="Arial Narrow" HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>    
                        <ClientSettings>    
                            <Selecting AllowRowSelect="True" EnableDragToSelectRows="False"></Selecting>    
                            <Resizing ResizeGridOnColumnResize="True" AllowColumnResize="True"></Resizing>    
                            <ClientEvents OnRowDeselected="RowDeselected" OnRowSelected="RowSelected" OnRowCreated="RowCreated"></ClientEvents>    
                            <Scrolling ScrollHeight="202px" AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="5"></Scrolling>    
                        </ClientSettings>    
                    </rad:radgrid>    
 
Code behind

 Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init  
 
Dim boundColumn As GridBoundColumn    
 Dim colcount as int16 = RadGrid1.MasterTableView.Columns.Count   
Try     
                  
                boundColumn = New GridBoundColumn()     
                RadGrid1.MasterTableView.Columns.Add(boundColumn)     
                boundColumn.HeaderText = "ID"    
                boundColumn.DataField = "ID"    
                boundColumn.UniqueName = "ID"    
                boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left     
                boundColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle     
                boundColumn.ReadOnly = True    
                boundColumn.Visible = False    
            Catch ex As Exception     
    
            End Try     
    
            Try     
                Dim templateColumnName As String = "Delete"    
                Dim templateColumn As New GridTemplateColumn()     
                templateColumn.ItemTemplate = New GridCheckboxTemplate(templateColumnName)     
                templateColumn.HeaderImageUrl = "images/delete.gif"    
                templateColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center     
                templateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left     
                templateColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle     
                templateColumn.HeaderStyle.Width = Unit.Pixel(23)     
                templateColumn.ItemStyle.Width = Unit.Pixel(35)     
                RadGrid1.MasterTableView.Columns.Add(templateColumn)     
            Catch ex As Exception     
    
            End Try    
 
 Try  
                                    boundColumn = New GridBoundColumn()  
                                     
                                    boundColumn.HeaderText = "Fname" 
                                    boundColumn.DataField = "Fname" 
                                    boundColumn.UniqueName = "Fname" 
                                    boundColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left  
                                    boundColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle  
                                    boundColumn.HeaderStyle.Width = Unit.Pixel(100)  
                                    boundColumn.ItemStyle.Width = Unit.Pixel(100)  
 RadGrid1.MasterTableView.Columns.Add(boundColumn)  
                                Catch ex As Exception  
                                End Try  
 
            Several more columns defined here.....  
 
 
 colcount = RadGrid1.MasterTableView.Columns.Count  
            RadGrid1 Code Binding here.......  
end Sub
 

Any feedback would be appreciated.

Thanks in advance,
RJ

4 Answers, 1 is accepted

Sort by
0
Hus Damen
Top achievements
Rank 1
answered on 07 Jan 2010, 03:52 PM
Hi RJ,

You can check the Adding Columns Programmatically section of the following document:

http://www.telerik.com/help/aspnet-ajax/grdusingcolumns.html

Thanks
Hus


0
RJ
Top achievements
Rank 1
answered on 07 Jan 2010, 04:05 PM
Hi Hus thanks for your quick response but I already used that as my reference as well as this link.
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
Actually all are working fine. I was able to make my checkbox template to delete all checked items, Add new items, and edit items.
The only problem is the grid adding set of items, even when I do check for the count (Radgrid1.Mastertableview.Columns.Count). It is only 8. but it shows several sets of same columns based on how many severside execution I made. I think this a bug or something. Coz before on my static columns which is defined in my aspx, I never had this kind of problem. The reason why I use dynamic creation for I wanted to sort columns based on users setup. Which I cant do using static columns defined on aspx.
0
Princy
Top achievements
Rank 1
answered on 08 Jan 2010, 12:25 PM
Hi,

To define the structure of a RadGrid control that is declared in the ASPX page, use the Page_Load event handler. Columns and detail table should be added to the corresponding collection first, before the values for their properties are set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection

Be sure to check that IsPostBack is false. Otherwise, you will end up adding the same objects to the RadGrid multiple times.

Thanks,
Princy


0
RJ
Top achievements
Rank 1
answered on 08 Jan 2010, 12:59 PM
Hi Princy,

On my previous post I have my column creation defined on Page_load which gave me a problem for it makes my GridTemplateColumn Checkbox disappears when ever theres some postback. Another problem with this is I cant attach event or addhandler to it for it said on item created "Object is Null" or something like that. You told me to define ti to Page_init which have made the fix.
http://www.telerik.com/community/forums/aspnet-ajax/grid/gridtemplatecolumn-checkbox-disappears-after-clicking-delete-command-button.aspx
And I was able to use my checkbox to delete checked items as well. But then it made my grid columns duplicate as stated above. But the only thing that im wondering is that when ever I do some Code Count (radgrid1,mastertableview.columns.count). It says "8", but looking on the grid, it has more than that.

Thanks again,
RJ  
Tags
Grid
Asked by
RJ
Top achievements
Rank 1
Answers by
Hus Damen
Top achievements
Rank 1
RJ
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or