Creating A Gridview Programmatically in ASP.NETVB.NET
Step 1) Create WebPage .aspx
Step 2) Include following References
System.Data;
System.Data.SqlClient;
Step 3) Build DataSet using AdventureWorks Database.
Here I am using 2 tables
1.SalesPerson
2.Employee
Here I am using 2 tables
1.SalesPerson
2.Employee
Note for this exaple you can use any of the Table.
Private Sub BuildDataSet()
conn.Close()
End Sub
Step 4) Programatically Creating a GridView in ASP.NET using VB.NET
Dim conn As SqlConnection = New SqlConnection("server = .\sqlexpress2012
database=AdventureWorks2012
trusted_connection=yes")
database=AdventureWorks2012
trusted_connection=yes")
SqlDataAdapter ad = New SqlDataAdapter("select * from
(AdventureWorks2012).(Sales).(SalesPerson) as SalesPerson "+
"select * from (AdventureWorks2012).(HumanResources).
(Employee) as Employee",
conn)
ad.Fill(ds)(AdventureWorks2012).(Sales).(SalesPerson) as SalesPerson "+
"select * from (AdventureWorks2012).(HumanResources).
(Employee) as Employee",
conn)
conn.Close()
End Sub
Private Sub CreateGridView()
Dim view As GridView = New GridView()
view.EmptyDataText = "No data found"
view.ShowHeaderWhenEmpty = True
view.DataSource = ds.Tables(0)
view.DataBind()
view.AlternatingRowStyle.BackColor = System.Drawing.Color.Teal
view.AlternatingRowStyle.ForeColor = System.Drawing.Color.Wheat
form1.Controls.Add(view)
End Sub
Note: For Every PostBack Dynamic controls needs to be created.
Step 5) Run the Web page.
Tags:Creating A Gridview Programmatically in ASP.NET VB.NET,Dynamically create gridview in asp.net, Dynamic GridView in asp.net, dataBinding to GridView in asp.net,GridView and DataSet Binding,Multiple sql queries to database using ado.net
No comments:
Post a Comment