Solved

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

Posted on 2014-02-12
10
1,052 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:murbro
  • 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:murbro
ID: 39854030
Hi. Thanks. I need to do this dynamically using VB.net code
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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:murbro
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:murbro
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:murbro
ID: 39855406
The following was stated clearly in my original question: "What VB.net code would I use to do this?"
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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