1 DataColumn datatype can be any data type, including self-defined classes and structs.
Datatable dt = new datatable ("Product"); dt. Columns.Add ("ProductId", typeof (int)); Dt. Columns.Add ("Productproperty", typeof (productproperty)); datarow r1 = dt. NewRow (); r1["ProductId"] = 1 ; r1["ProductProperty"] = new productproperty () { ProductionDate = DateTime.Now, Desc = "Milk sugar" }; dt. Rows.Add (R1); foreach (DATAROW DR in dt. Rows) { console.writeline ("productid {0}; Productiondate:{1};D esc:{2}; ", dr["ProductId"]. ToString (), (dr["Productproperty"] as productproperty). Productiondate.tostring (), (dr["Productproperty"] as productproperty). DESC); }
2 A DataColumn that has a unique attribute marked true does not allow null values.
3 Determine if a column in the DataRow has been modified
Onerow.datarowstate=datarowstate.changed, otherwise there may be no original version if (Onerow.hasversion (datarowversion.proposed)) {if ( onerow["Salary", datarowversion.original]!=onerow["Salary", datarowversion.proposed]) {//salary field has changed }}
4 using Datarow.beginedit () Datarow.endedit () to delay data validation while reducing the number of rowchanged event firings
Onerow.beginedit (); onerow["ProductName"]= "Colaverygood";//productname MaxLength for 10onerow["UnitPrice"]=12.4;// The UnitPrice maximum value is 10.0onerow.endedit ();//until the data validation exception occurs, and if there is no exception, the RowChanged event occurs without triggering two events.
5 Validation-based errors
With datacolumnchanging,datarowchanging, the data can be modified to verify that data that does not conform to the validator is entered into the column field or the data is not valid.
private void App_columnchanging (Object Sender,datacolumnchangeeventargs e) {if (e.column.columnname== "age") { if ((int) e.proposedvalue>120) e.row.setcolumnerror (E.column, "Human can ' t live that long"); } }
6 column-Level errors
public void Showfirstrowerror (DataRow row) {string errorText = "No error!"; datacolumn[] Errorcolumns = row. Getcolumnsinerror (); if (errorcolumns.count>0) {errortext= row. Getcolumnerror (Errorcolumns[0]); } else if (row. rowerror.length>0) {errorText = row. RowError; }//Show ErrorText}
7 Search rows by primary key
DataRow Matchrow = SomeTable.Rows.Find (searchvalue);//single-part keydatarow matchrow = someTable.Rows.Find (New object []{value1,value2}]//multi-part key;//If no row is found, returns null
8 Selecting rows and sorting with search criteria
Match note string case sensitivity//sometable.casesensitive=true;//default case insensitive datarow[] matchrows = Sometable.select (FilterCriteria, Sortrules);//filtercriteria filter Expression Reference datacolumn.expression very powerful//sortrules OrderDate desc,customername ASC,ASC is the default sort, can be omitted.
This article is from the "Smart Barrier World" blog, please make sure to keep this source http://chenwan.blog.51cto.com/2754591/1567286
Ado. NET Knowledge Point notes