what's the Difference between DataView and DataTable?

Answer Posted / rajesh kumar chekuri

DataTable: Represents one table in-memory
DataView:: Represents a customized view of a DataTable for
sorting, filtering, searching, editing, and navigation.
See the following example u can get Better idea...

// Create a new DataTable.
System.Data.DataTable table = new
DataTable("Customers");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;

// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);


// Create second column.
// Add the column to the table.
table.Columns.Add("FirstName",
System.Type.GetType("System.String"));

// Add the column to the table.
table.Columns.Add("LastName",
System.Type.GetType("System.String"));

// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;


// add new datarow by adding the values
table.Rows.Add("C101", "Rajesh", "Kumar");
table.Rows.Add("C102", "Fareed", "sk");
table.Rows.Add("C103", "Rajesh", "Raji");

//Sort and display
DataView view = new DataView(table);
view.Sort = "LastName ASC, FirstName ASC";

// assing ur gridview to dataview
GridView1.DataSource = view;
GridView1.DataBind();

Is This Answer Correct ?    18 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explanation on Generic?

735


what is .NET framework architecture ??

1758


What is difference between an reference type and value type in C#?

796


what are windows services?

719


What is Global Assembly Cache (GAC) and what is the purpose of it? (How to make an assembly to public? Steps) How more than one version of an assembly can keep in same place?

785


What is instantiating a class in c#?

679


What is datareader c#?

671


What is the difference between parse and tryparse in c#?

698


How many parameters can a method have c#?

664


What are the types of attributes in c#?

678


Can abstract class have constructor?

669


Where static variables are stored?

656


Is cli same as the clr?

684


Where do we set the min and max pool size for connection pooling?

716


What is the benefit of dependency injection c#?

659