Solved

Example of binding a gridview to dataset

Posted on 2007-03-26
7
2,781 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grab and show facebook pages event calendars 6 65
ASP.NET e-commerce website 4 57
CheckboxList Object reference not set to an instance of an object. 16 33
Class object 2 26
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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 …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 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