Datasets and DataTable Explanations

Source: Internet
Author: User

A DataTable with the same structure as the user request data structure is built, then the user's request is populated into the built DataTable and the DataTable is added to the dataset.


Datatable,,datacolumn,datarow in-depth research

A DataTable is a relational data table in memory that can be created independently or used as a member of a DataSet!

How to use a DataTable as a member of a dataset:
Create a DataTable object first, and then add it to the Tables collection of the DataSet object by using the Add method
Example:
DataSet Dsclass = new DataSet (); Create a new empty class dataset
DataTable Dtclass = new DataTable ("Class"); Create a class table
DSCLASS.TABLES.ADD (Dtclass); Add a class table to a dataset
If you add a DataTable to a DataSet without specifying a DataTable name, the table gets a default table name that is incremented from "0"

The table you just started with does not have a table structure, and to define the structure of the table, you must create the DataColumn object and add it to the table's Columns collection. After a structure has been defined for a DataTable, the data is added to the Rows collection of the table through a DataRow object


DataColumn

DataColumn is the basis for creating a DataTable, defining the structure of a DataTable by adding one or more DataColumn objects to the DataTable. DataColumn has some common properties for restricting input data, such as: data type, data length, default value.

Common Properties of DataColumn
AllowDBNull whether null values are allowed
ColumnName stored data types
DataType stored data types
MaxLength Gets or sets the maximum length of a text column
Dafaultvalue Default Value
The name of the DataTable to which the Table belongs
Unique DataColumn is the only value

There are two ways to define DataColumn,
Method One:
Create Class Name column
DataColumn className = new DataColumn ();
Classname.columnname = "ClassName";
Classname.datatype = System.Type.GetType ("System.String");
Classname.maxlength = 50;

Create Class Name column
DataColumn className = new DataColumn ("ClassName", typeof (String));
Classname.maxlength = 50;

DataRow
The DataRow represents the actual data contained in the DataTable, and we can add the data to the DataTable defined in DataColumn with a DataRow.

Create Class Name column
DataColumn className = new DataColumn ("ClassName", typeof (String));
Classname.maxlength = 50;
Create a new row of data
DataRow Drclass = Dtclass.newrow ();
drclass["className"] = This.txtClassName.Text.Trim ();

How to customize a dataset
1. Create a DataSet object
2. Create a DataTable object
3. Create a DataColumn object build table Structure
4. Add the created table structure to the table
5. Create a DataRow object new data
6. Inserting data into a table
7. Adding a table to a dataset

Example:
//Create a new empty class DataSet
DataSet Dsclass = new DataSet ();
Create Class table
DataTable dtclass = new DataTable ("Class");
Create Class Name column
DataColumn dcclassname = new DataColumn ("ClassName", typeof (String));
Classname.maxlength = 50;
//Create Grade ID column
DataColumn dcgradeid = new DataColumn (Gradeid ", typeof (int));
Add a well-defined column to the class table
DtClass.Columns.Add (dcclassname);
DTCLASS.COLUMNS.ADD (Dcgradeid);
//Create a new data row
DataRow Drclass = Dtclass.newrow ();
drclass["className"] = This.txtClassName.Text.Trim ();
drclass["Gradeid"] = Objgrade.getgradeidbygradename (This.cboGrade.Text.Trim ());
//Insert a new data row into the class table
DtClass.Rows.Add (drclass);
Add the Class table to the DataSet
DsClass.Tables.Add (Dtclass);

How to get data from a dataset
There are two ways to get data from a dataset:
1. The first approach is to obtain data by specifying a row of a specific DataTable in the dataset.
Steps:
1. Get the specified DataTable from the dataset by the table name
2. Retrieve the specified DataRow from the DataTable by index
3. Get the data for the specified column from the DataRow by column name
Cases:
Get class Name
dsclass.table["Class"]. row[0]["ClassName"];
Get Grade ID
dsclass.tables["Class"]. row[0]["Gradeid"]


2. Another way is to bind the data in the dataset directly to the data presentation control.


We generally need to make some simple modifications to the data extracted from the dataset, such as hiding specific columns and sorting by a column. Quite simply,. NET provides us with a DataView object that can help us create different views of the data in the dataset, just like a view in a database. (a DataTable can dynamically generate multiple DataView)

DataView
DataView provides us with a dynamic view of the DataTable and the ability to sort, DA, and manipulate the data in a dynamically generated view, similar to a view in a database, except that it cannot provide a view of the associated DataTable, and it cannot exclude columns that exist in the original table. You cannot append a column that does not exist to the original table.

Example:
Filter out the user status to inactive learners
Post-filtered learner information sorted in descending order of learner names
DataSet dsstudent = new DataSet ();
DataView dvstudent = new DataView ();
dvstudent.table = dsstudent.tables["studenttable"];
Dvstudent.rowfilter = "UserState = ' activity '";
Dvstudent.sort = "Studentname DESC";

DataView a few common properties:
Table is used to get or set the source DataTable
Sort Gets or sets one or more DataView and sort order
RowFilter Gets or sets an expression that filters which rows are viewed in DataView
Count gets the number of rows in DataView after applying RowFilter

Experience:
In development, if you need to dynamically filter or sort the data presented by Datagrigview, we'd better use the DefaultView (default view) property of the DataTable to get a view of the DataTable. This allows us to reduce the process of instantiating the DataView object and getting the DataView object's original DataTable.
DataTable dtstudent = (DataTable) Datagridview.datasource;
dtstudent.tables["Studenttable"]. Defaultview.rowfilter = "userstate= ' Activity '";

Detailed dataset and DataTable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.