Solved

Example of binding a gridview to dataset

Posted on 2007-03-26
7
2,783 Views
Last Modified: 2011-10-03
I found this example of binding a gridview to a dataset but how about if I want to use a TableAdapter?

http://www.codersource.net/asp_net_grid_view_whidbey.aspx

0
Comment
Question by:Camillia
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:zeroxp
ID: 18797094
after you constructed your strong typed DataSets and TableAdapters, you can use them as normal datasets or data adapters. the default adapter method name will be GetData() or Fill():

   protected void Page_Load(object sender, EventArgs e)
   {
      LoadData();
   }
   private void LoadData()
   {
      DataSet_NorthwindTableAdapters.CustomersTableAdapter tblCustomer = new DataSet_NorthwindTableAdapters.CustomersTableAdapter();
      GridView1.DataSource = tblCustomer.GetData();
      GridView1.DataBind();
      DataSet_Northwind ds = new DataSet_Northwind();
      tblCustomer.Fill(ds.Customers);
      GridView2.DataSource = ds.Customers;
      GridView2.DataBind();
   }
0
 
LVL 5

Expert Comment

by:asp_lha
ID: 18797097
maybe you can check this :

Data Binding Web Pages with a Visual Studio Data Component
http://msdn2.microsoft.com/en-us/library/3h7eexxe(VS.80).aspx

0
 
LVL 7

Author Comment

by:Camillia
ID: 18797153

Thank you both. Is there a performace benefit to using a tableadapter? Why would i want to use it??
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Author Comment

by:Camillia
ID: 18797163
for example, what's the difference between the example i posted and zeroxp's example?
0
 
LVL 5

Expert Comment

by:zeroxp
ID: 18797238
i can see any performance benefit with strongly typed datasets. i'm not really sure about the performance difference. i imagine the strongly typed one perform more mapping process.
but the main benefit is you have easy and direct access to table structure. once you have the dataset configured, you can use it as a compiled class with get/update methods.
but if you just want use a simple query to access or update some record, it is still easy to use normal command, datareader, dataadapter and dataset directly.
0
 
LVL 7

Author Comment

by:Camillia
ID: 18797251
i call the stored proc, grab top 20 rows, then just want to bind to a girdview. No updatng or anything of data...
0
 
LVL 5

Accepted Solution

by:
zeroxp earned 500 total points
ID: 18797357
this is easier to me, because no need to make a dataset in the project.

      System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("SELECT TOP 20 Customers.* FROM Customers");
      using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
      {
         command.Connection = conn;
         conn.Open();
         GridView1.DataSource = command.ExecuteReader();
         GridView1.DataBind();
         conn.Close();
      }

use can also easily change to use dataadapter and dataset as your example.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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 …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

776 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