Copy GridView struture to another GridView

dj2fine

New Member
I have about 11 GridViews on my page: GridView1, GridView2, ... GridView10 and GridViewActive. GridView1 to GridView10 are invisible, but GridViewActive is Visible. Based on different conditions I need to copy structure of one GridView to GridViewActive.I need to do something like \[code\]//common paramsSqlDataSource1.SelectParameters["CommonParam1"].DefaultValue = http://stackoverflow.com/questions/12723649/this.commonParam1; if (reportname ="report1") { SqlDataSource1.SelectParameters["Param"].DefaultValue = http://stackoverflow.com/questions/12723649/this.param; CopyGridViewStructure(GridView1, GridViewActive); }else if (reportname ="report2") { SqlDataSource1.SelectParameters["AnotherParam"].DefaultValue = http://stackoverflow.com/questions/12723649/this.anotherParam; CopyGridViewStructure(GridView2, GridViewActive); }etc.// DO OTHER STAFF WITH GRIDVIEWATIVE HERE AND IN OTHER METHODS\[/code\]And on button click (actually it is a bit more complicated than simple ButtonClick, as it is Ajax which calls Button click, and button is actually invisible for user):\[code\]//this block doesn't work for now, because grvActive doesn't have any structurethis.grvActive.DataSource = SqlDataSource1; this.grvActive.DataBind();\[/code\]I need to copy only Columns. Have not found sollution for this. GridView doesn't contain Clone method.EDIT:GridView1 to GridView10 and GridViewActive are not dynamically created.Example of GridView:\[code\]<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Visible="false"> <Columns> <asp:BoundField DataField="username" HeaderText="First Name"> <ItemStyle Width="110px"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="usersurname" HeaderText="Last Name"> <ItemStyle Width="110"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="usertype" HeaderText="User Type"> <ItemStyle Width="100px"></ItemStyle> </asp:BoundField> <asp:TemplateField HeaderText="Login"> <ItemTemplate> <asp:Label runat="server" ID="lblLastModifyDate" Text='<%# MyUtility.MyCommon.FormatDate(Eval("logindate").ToString()) %>' /> </ItemTemplate> <ItemStyle Width="177px"></ItemStyle> </asp:TemplateField> </Columns> </asp:GridView>\[/code\]Other GridView are very similar, but with different columnnames and datafields. Some of them contains different TemplateField, some doesn't have TemplateFields at all.
 
Back
Top