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
//common params
SqlDataSource1.SelectParameters["CommonParam1"].DefaultValue = this.commonParam1;
if (reportname = "report1")
{
SqlDataSource1.SelectParameters["Param"].DefaultValue = this.param;
CopyGridViewStructure(GridView1, GridViewActive);
}
else if (reportname = "report2")
{
SqlDataSource1.SelectParameters["AnotherParam"].DefaultValue = this.anotherParam;
CopyGridViewStructure(GridView2, GridViewActive);
}
etc.
// DO OTHER STAFF WITH GRIDVIEWATIVE HERE AND IN OTHER METHODS
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):
//this block doesn't work for now, because grvActive doesn't have any structure
this.grvActive.DataSource = SqlDataSource1;
this.grvActive.DataBind();
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:
<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>
Other GridView are very similar, but with different columnnames and datafields. Some of them contains different TemplateField, some doesn’t have TemplateFields at all.
I have not found a sollution for copying GridView structure, however, I have found a solution for my problem. Instead of Cloning structure, I am saving GridView ID and then I am using FindControl.
And then
And when I need to use gridView, I am using FindControl: