Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi All,

I want to know ,when we use

DataGrid1.datasource=datatable

and when

DataGrid1.datasource=dataset;



what is the difference between them.

Please help me.

Thanks in Advance.
Posted
Updated 27-Jun-11 18:39pm
v2
Comments
[no name] 28-Jun-11 0:39am    
Edited for Code Block.

A DataSet can contain several DataTables. It's also possible to lay logical relationships between these datatables in the dataset. So to keep it simple: A dataset can be seen as a container that holds datatables.
 
Share this answer
 
Comments
[no name] 28-Jun-11 0:42am    
Good Call Amit. My 5 too.
Wild-Programmer 28-Jun-11 0:56am    
Thank you Koushik :)
Agree with Amit. Adding to what he said: when you assign dataset as datasource to a datagrid( or any other UI control), the first table( of the dataset) will act as datasource.

Hope that helps!
 
Share this answer
 
DataSet and DataTable are the key components in ADO.NET programming. This mean that DataTable represents an in memory representation of the database. We can load a single Table from the database into a DataTable and manipulate the data in memory. DataTable can be used as a DataSource and used it if we are going to fetch data in a single database table only. While DataSet on the other hand can define DataRelations - which define the relationship between DataTables, much like a foreign key relationship can be set up between tables in a database. DataSets, themselves DOES NOT Contain any Data. DataSets contain DataTables (which is where any data actually resides),DataRelations, etc, but no data.

SqlDataAdapter a = new SqlDataAdapter;
DataSet s = new DataSet();
a.Fill(s);
foreach (DataRow dr in s.Tables[0].Rows)
  {
    Console.WriteLine(dr[0].ToString());
  }
 
Share this answer
 
Comments
[no name] 28-Jun-11 0:43am    
Nice Reply. My 5 too.
CS1401 28-Jun-11 0:54am    
Thanks buddy..
It really depends on the sort of data you're bringing back. Since a DataSet is (in effect) just a collection of DataTable objects, you can return multiple distinct sets of data into a single, and therefore more manageable, object.
 
Share this answer
 
we can use "DataGrid1.datasource=datatable" when we have to bind single datatable to a datagrid. we can also use datatable.defaultview to bind as a Datasource. It helps to filter & sort using dataview.

but when we have realtionship exists in datatables in Dataset, then we can bind Dataset to show related data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900