Demonstrating the use of DataRow classes

Source: Internet
Author: User

(Excerpt from the C # function manual, metallurgical industry Press)

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;

Namespace ConsoleApplication1
{
Class Program
{
static void Main (string[] args)
{
DataTable Testdt = new DataTable ("Student");
DataColumn Testdc;
TESTDC = new DataColumn ();
Testdc.datatype = Type.GetType ("System.Int32");
Testdc.columnname = "ID";
TESTDT.COLUMNS.ADD (TESTDC);

TESTDC = new DataColumn ();
Testdc.datatype = Type.GetType ("System.String");
Testdc.columnname = "Name";
TESTDT.COLUMNS.ADD (TESTDC);

TESTDC = new DataColumn ();
Testdc.datatype = Type.GetType ("System.String");
Testdc.columnname = "School";
TESTDT.COLUMNS.ADD (TESTDC);

DataRow TESTDR = Testdt.newrow ();
testdr["ID"] = 1;
testdr["Name" = "Rose";
testdr["School"] = "scut";
TESTDT.ROWS.ADD (TESTDR);

TESTDR = Testdt.newrow ();
testdr["ID"] = 2;
testdr["Name"] = "Coke";
testdr["School"] = "SCNU";
TESTDT.ROWS.ADD (TESTDR);

TESTDR = Testdt.newrow ();
testdr["ID"] = 3;
testdr["Name" = "Tom";
testdr["School"] = "scut";
TESTDT.ROWS.ADD (TESTDR);

Console.WriteLine ("Information of the current table:");
Doprint (TESTDT);

Console.WriteLine ("Add one line of information:");
TESTDR = Testdt.newrow ();
testdr["ID"] = 4;
testdr["Name" = "Cat";
testdr["School"] = "ZSU";
TESTDT.ROWS.ADD (TESTDR);
Console.WriteLine ("Add new row status: {0}", testdr.rowstate);
Doprint (TESTDT);

Console.WriteLine ("Perform acceptchanges operation on newly added rows ...");
Testdr.acceptchanges ();
Console.WriteLine ("Perform acceptchanges operation the state of the row: {0}", testdr.rowstate);
Doprint (TESTDT);

Console.WriteLine ("Add another line of information:");
TESTDR = Testdt.newrow ();
testdr["ID"] = 5;
testdr["Name" = "Jim";
testdr["School"] = "ZSU";
TESTDT.ROWS.ADD (TESTDR);
Console.WriteLine ("Add row status: {0}", testdr.rowstate);
Doprint (TESTDT);

Console.WriteLine ("Perform rejectchanges operation on newly added rows ...");
Testdr.rejectchanges ();
Console.WriteLine ("Perform rejectchanges operation the state of the row: {0}", testdr.rowstate);
Doprint (TESTDT);

Console.WriteLine ("Start editing ...");
foreach (DataRow Dr in Testdt.rows)
{
Dr. BeginEdit ();
DR[0] = (int) dr[0] + 10;
Dr. EndEdit ();
}
Console.WriteLine ("Edit over!");
Doprint (TESTDT);

Console.WriteLine ("Set error message:");
String errstr = "This is a wrong ID number!";
TESTDR = Testdt.rows[0];
Testdr.setcolumnerror (Testdt.columns[0], errstr);

foreach (DataRow dre in Testdt.rows)
{
Datacolumn[] DcA;
if (Dre. HasErrors)//To determine whether a row has errors
{
DcA = dre.    Getcolumnsinerror (); Gets an array of columns that contain errors
for (int i = 0; i < dca.length; i++)
{
Console.WriteLine ("Column name with error message: + dca[i]." ColumnName);
}
Console.WriteLine ("Eliminate all errors in this row.");
Dre.    Clearerrors (); Use the Clearerrors method to clear the error of the row
}
}
Console.WriteLine ();
TESTDR = testdt.rows[3];
Testdr.delete (); To delete a Datrow object using the Dete method
Testdr.acceptchanges ();
Console.WriteLine ("Delete last line!");
Doprint (TESTDT);

DataTable TESTDTC = new DataTable ("Class");
TESTDC = new DataColumn ();
Testdc.datatype = Type.GetType ("System.Int32");
Testdc.columnname = "CID";
TESTDTC.COLUMNS.ADD (TESTDC);

TESTDC = new DataColumn ();
Testdc.datatype = Type.GetType ("System.String");
Testdc.columnname = "Grade";
TESTDTC.COLUMNS.ADD (TESTDC);

String[] Pstr = {"ID"};
String[] CStr = {"CID"};
DataRelation DR = new DataRelation ("EQ", "Student", "class", Pstr, CStr, true);
foreach (DataRelation drn in testdt.childrelations)
{
foreach (DataRow Dr in Testdt.rows)
{
Datarow[] DrA = Dr. GetChildRows (DRN);
for (int i = 0; i < dra.length; i++)
{
foreach (DataColumn dc in Testdt.columns)
{
Console.WriteLine (DRA[I][DC]);
}
}
}
}
TESTDR = Testdt.rows[0]; Console.WriteLine ("First column has null value?" + testdr.isnull ("ID"));
Console.ReadLine ();
}

private static void Doprint (DataTable TEMPDT)
{
Console.WriteLine ("{0} table data:", tempdt.tablename);
foreach (DataColumn dc in Tempdt.columns)
Console.Write (DC. ColumnName + "");
Console.WriteLine ();
foreach (DataRow Dr in Tempdt.rows)
{
foreach (DataColumn dc in Tempdt.columns)
{
Console.Write (DR[DC] + "");
}
Console.WriteLine ();
}
Console.WriteLine ("------------------------------/n");
}
}
}


*****************************************************************
The results of the implementation are as follows:

Information for the current table:
Student: {0} table data:
ID Name School

1 Rose scut

2 Coke SCNU

3 Tom Scut

------------------------------

Add one line of information:
Add a new row status: Added
Student: {0} table data:
ID Name School

1 Rose scut

2 Coke SCNU

3 Tom Scut

4 Cat ZSU

------------------------------

Perform a acceptchanges operation on the newly added row ...
Perform acceptchanges operation the status of the row: unchanged
Student: {0} table data:
ID Name School

1 Rose scut

2 Coke SCNU

3 Tom Scut

4 Cat ZSU

------------------------------

Add another line of information:
Add the status of a row: Added
Student: {0} table data:
ID Name School

1 Rose scut

2 Coke SCNU

3 Tom Scut

4 Cat ZSU

5 Jim ZSU

------------------------------

Perform a rejectchanges operation on the newly added row ...
Perform rejectchanges operation the status of the row: Detached
Student: {0} table data:
ID Name School

1 Rose scut

2 Coke SCNU

3 Tom Scut

4 Cat ZSU

------------------------------

Start editing ...
Edit over!
Student: {0} table data:
ID Name School

One Rose scut

Coke SCNU

Tom Scut

Cat ZSU

------------------------------

Set error message:
Column name with error message: ID
Resolves all errors in the row.

Delete the last line!
Student: {0} table data:
ID Name School

One Rose scut

Coke SCNU

Tom Scut

------------------------------

Does the first column have a null value? False

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.