Solved

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

Posted on 2014-02-12
10
1,038 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now