0
votes

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.

2
Can you show your GridView's aspx markup or are they completely dynamic? What is the datasource of them? - Tim Schmelter
I have added example of my GridView. They are not dynamic. Previosly application had 10 different aspx pages, but after refactoring all GridViews were copied to one aspx page and we need to show only one of them. (not only "to show one of them", but also "do some manipulations on one of them). - renathy

2 Answers

0
votes

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.

private string GridID 
{
        get
        {
            return ViewState["GridID "].ToString();
        }
        set
        {
            ViewState["GridID "] = value;
        }
}

And then

SqlDataSource1.SelectParameters["CommonParam1"].DefaultValue = this.commonParam1;       
if (reportname = "report1")  
{ 
    SqlDataSource1.SelectParameters["Param"].DefaultValue = this.param;       
    // //CopyGridViewStructure(GridView1, GridViewActive);  
    GridID = "GridView1";
} 
else if (reportname = "report2")  
{  
    SqlDataSource1.SelectParameters["AnotherParam"].DefaultValue = this.anotherParam; 
    CopyGridViewStructure(GridView2, GridViewActive);  
    GridID = "GridView2";
} 
etc.

And when I need to use gridView, I am using FindControl:

GridView grd = (GridView) this.FindControl(GridID);

// actually for my page I am using masterpage and FindControl is a bit more complicated:
GridView grd = (GridView)((ContentPlaceHolder)(this.Controls[0].FindControl("mainContent"))).FindControl(GridID);
0
votes

I found some solution:

In Source aspx after grid databind:

Session["gridtoexcel"] =  yourgrid;

In destination aspx

 var grid = ((GridView)Session["gridtoexcel"]);

            gridToExcel.Columns.Clear();
            foreach (DataControlField col in grid.Columns)
                  gridToExcel.Columns.Add(col);
            
            gridToExcel.DataSource = grid.DataSource;
            gridToExcel.DataBind();

this way i can 'clone' exact grid to another page. if you need some css style don't forget of add them in destination page