Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-12
10
Medium Priority
?
1,152 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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