Solved

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

Posted on 2014-02-12
10
1,008 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

20 Experts available now in Live!

Get 1:1 Help Now