C # DataSet, DataTable, DataRow, DataColumn DataSet application Encyclopedia

Source: Internet
Author: User
Tags first row

Turn from: http://www.cnblogs.com/szytwo/archive/2012/03/21/2409536.html

First, the basic application of data sets

1. Table New Record

Way One: Use the BindingSource AddNew

New record, recommended for use, cursor position in current new record, and processing edit state
DataRow Thisrow = ((DataRowView) usersbindingsource.addnew ()). Row;
thisrow["OID"] = 5;
thisrow["CNAME"] = "New user";
thisrow["sex"] = "M";

Mode two: Using the NewRow of the DataTable

New records (not recommended, because this way is rows.add when not in edit state is constrained, and the cursor does not automatically move the record when new) 
DataRow Thisrow = userdataset.tables["Users". NewRow ();
thisrow["OID"] = 5;
thisrow["CNAME"] = "New user";
thisrow["sex"] = "M";
userdataset.tables["Users". Rows.Add (Thisrow);

2. Table deletion record

Way One: Use the BindingSource removecurrent

if (usersbindingsource.current!= null)
   //delete current record, recommend using
   usersbindingsource.removecurrent ();

Mode two: Using the remove of DataRowCollection

Deleting the current record, which is not recommended, is not recorded in RowState and will not be updated at
DataRow thisrow = Getcurrentdatarow (usersbindingsource);
if (thisrow!= null)
   userdataset.tables["Users". Rows.remove (Thisrow);

Mode three: Using DataRow's Delete

Delete the current record, not recommended, BindingSource can be more concise
DataRow Thisrow = Getcurrentdatarow (usersbindingsource);
if (thisrow!= null)
   thisrow.delete ();

3. Table Modification Record

Mode one: Use datarowobject[column name] directly modify

DataRow Thisrow = Getcurrentdatarow (usersbindingsource);

if (thisrow!= null)
{
   thisrow.beginedit ();
   thisrow["CNAME"] = "modified name";
   Thisrow.endedit ();
}

4. Form lookup and filter records

Way one: Use DataRowCollection.Find to find

Datacolumn[] keys = new datacolumn[1];
Keys[0] = userdataset.tables["Users". columns["OID"];
userdataset.tables["Users". PrimaryKey = keys;
DataRow FindRow = userdataset.tables["Users". Rows.find ("1");
if (FindRow = = null)
{
   MessageBox.Show ("not Found");
}
else
{
   MessageBox.Show ("successfully found, CNAME =" + findrow["CNAME"]);

Mode two: Using Bindingsource.find to find

int i = Usersbindingsource.find ("OID", "1");

if (i >= 0)
   MessageBox.Show ("successfully found, CNAME =" +
   userdataset.tables["users"]. rows[i]["CNAME"]);

Mode three: Using DataTable.Select to get DataRow array

datarow[] Arydr = userdataset.tables["Users". Select ("OID > 1");

for (int i = 0; i < arydr.length i++)
{
    DataRow dr = Arydr[i];
    MessageBox.Show (convert.tostring (int) dr["OID"));
}

5. Movement of table Records

Mode one: Adopt BindingSource method or position attribute.

Specifies which row to locate, Position does not change with table column ordering, 0 is not necessarily the first row of the table

usersbindingsource.position = 0;

To move to the previous one, after sorting the table columns, the previous one may be the interface to display the previous usersbindingsource.moveprevious of the Table

();

Usersbindingsource.movenext ()//move to Next

Usersbindingsource.movefirst ();

Usersbindingsource.movelast ();

6. Filtering of Forms

Method One: Using BindingSource filter to achieve

Usersbindingsource.filter = "OID > 1";

7. Data set Empty

Way One: Use Datatable.clear (), note that this will not retain the deletion state, save will not really delete

userdataset.tables["Users". Clear ();

Mode two: Use DataTable.Rows.Clear Delete, note that this will not retain the deletion state, save will not really delete

userdataset.tables["Users". Rows.clear ();

Mode three: Use bindingsource.removecurrent loop to delete all records, this will retain the deletion state.

while (usersbindingsource.current!= null)
      usersbindingsource.removecurrent ();

8. Replication of dataset data and structure

Mode one: Replication of the entire dataset

DataSet copyds = Userdataset.copy ();

Mode two: Copy only a single table

DataSet copyds = new DataSet ();
COPYDS.TABLES.ADD (userdataset.tables["users"). Copy ());

Mode three: Copy only the structure of the dataset

Copyds = Userdataset.clone ();
MessageBox.Show (copyds.tables["users"). Rows.Count.ToString ());

9. Get Dirty Data

Way one: dirty data for the entire dataset

Copyds = Userdataset.getchanges ();

Method Two: Get dirty data for a single table

DataTable dt = userdataset.tables["users"]. GetChanges ();

10. Data merge for datasets

Mode one: DataSet.Merge merging of the entire dataset

Ds. Merge (Userdataset);

Mode two: Datatable.merge Merging of single table

Ds. Merge (userdataset.tables["Users"
Related Article

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.