Solved

ASP.net VB.net Code to recreate/copy an existing GridView

Posted on 2014-02-12
10
1,090 Views
Last Modified: 2014-02-12
Hi

I have a GridView on an ASP.net page that I need to copy a few times, and add different data sources to the copies. What VB.net code would I use to do this?
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39853949
Copy and paste the aspx code and change the datasource properties?
0
 
LVL 7

Expert Comment

by:XGIS
ID: 39853965
Hello murbro,

If you are using a SQLDatasource and declaring this data on your ASPX page the following should be possible.  No need for "code-behind".  Not 100% professional but it works fine.

GridView1
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT * FROM AccountsTable">
</asp:SqlDataSource>  
<asp:GridView ID="GridView1"  DataSourceID="SqlDataSource1"  
AutoGenerateColumns="true" runat="server">
</asp:GridView>

Open in new window


GridView2
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
SelectCommand="SELECT * FROM Customers">
</asp:SqlDataSource>  
<asp:GridView ID="GridView2"  DataSourceID="SqlDataSource2"  
AutoGenerateColumns="true" runat="server">
</asp:GridView>

Open in new window

0
 

Author Comment

by:Murray Brown
ID: 39854030
Hi. Thanks. I need to do this dynamically using VB.net code
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39854218
Gridview does not have a Clone or Copy method so it would have to be done by creating the GridView control, defining its columns and setting its datasource and then adding it to the page.

Partial example here

http://www.highoncoding.com/Articles/88_Dynamically_Creating_Bound_and_Template_Columns_in_GridView.aspx
0
 
LVL 7

Expert Comment

by:XGIS
ID: 39854297
Hello Murbro.. pls note your ? has now changed
otherwise you would only need to change the
"SELECT * FROM TableName" to make copies with different data displaying.

eg "SELECT Name, Address FROM TableName"
eg "SELECT ID, Postcode FROM TableName"

The grids will only display what you ask them to, and the fields generate dynamically

The alternative to get different displays by defining filter fields in the gridview, which again could be done without any code.. vb or c#

The only difference now based on your new requirement is to remove the data source from the front page and place it in code behind which will mostly increase code complexity and cause learning downtime, but yield a faster more secure result.

A button based event may be a good implementation as each button could reference a different datasource variant. eg Show Names or Show Postcodes

private void button1_Click(object sender, EventArgs e)
        {
            DataGridView grd = new DataGridView();
            Point pt = new Point(0, 0);
            grd.RowCount = 2;
            grd.ColumnCount = 2;
            grd.Location = pt;
            grd.Visible = true;
            this.Controls.Add(grd);
        }

Open in new window


otherwise this example may help

else this approach may be what you are after
0
 

Author Comment

by:Murray Brown
ID: 39854428
If I have a style for a GridView in my style sheet can I programatically apply this style to a GridView that I have added programatically
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 39854598
Yes you can set the CssClass property

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.cssclass(v=vs.110).aspx


Is this style at the top level of the gridview?
0
 

Author Closing Comment

by:Murray Brown
ID: 39855379
Thanks very much for the help
0
 
LVL 7

Expert Comment

by:XGIS
ID: 39855400
? Seriously... There is no VB .net code referred to in this page? let alone the 1st suggestion that solves the question and the 2nd suggestion shows demonstrated examples.. as for the CSS question it is unrelated to the original question... just saying thats all.
0
 

Author Comment

by:Murray Brown
ID: 39855406
The following was stated clearly in my original question: "What VB.net code would I use to do this?"
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question