C#datatable some ways to use

Source: Internet
Author: User

C#datatable some ways to use

1, using a DataTable must refer to System.Data.

2, define a DataTable

DataTable dt=new DataTable ();

 3, create a column for the DataTable

1. Create an empty column DataColumn dc = new DataColumn ();d T. Columns.Add (DC);//2. Create a column with column name and type name dt. Columns.Add ("Column0", typeof (String));//3. Add columns By column schema DataColumn dc = new DataColumn ("Column1", typeof (String));d T. Columns.Add (DC);

4, create a row for the DataTable

1. Create the empty line DataRow dr = dt. NewRow ();d T. Rows.Add (DR);//2. Create a blank line dt. Rows.Add ();//3. Create and assign the DT through the row frame. Rows.Add ("Xiao Ming",//add); The data order of the parameters inside the parameter corresponds to the order of the columns in the DT//4. Create a dt by copying a row from the DT2 table. Rows.Add (DT2. Rows[i]. ItemArray);

Value and assignment of 5,datatable

The assignment of the new row is the DataRow dr = dt. NewRow ();d r[0] = "xiaoming";//By index assignment dr["Column1"] = DateTime.Now; Assigns a value by name//to the table already has the row to assign the value dt. Rows[0][0] = "Xiao Ming"; The index is assigned by DT. rows[0]["Column1"] = datetime.now;//is assigned by name//value string Name=dt. Rows[0][0]. ToString (); string Time=dt. rows[0]["Column1"]. ToString ();

6,datatable Filter rows and delete rows

Select usage
Filter the collection of rows with "small" in the Name column value (fuzzy query), the second sentence can be added in descending order by age datarow[] dr = dt. Select ("Name like ' small% '");D atarow[] drs = dt. Select ("Name like ' small% '", "Age DESC");
 //Compute用法Object result = Dt.compute ("sum (score)", "Age >16 and name like ' small * '"); Result is calculated as the first parameter of the COMPUTE function is generally an aggregate function, and the latter parameter is the filter condition//find usage Dt.primarykey = new datacolumn[] {dt. columns["study number"]}; DataRow DR1 = dt. Rows.find ("004");//find usage requires a DataTable to have a primary key, which is a way to search a row of data by primary key//delete row  //使用DataTable.Rows.Remove(DataRow)方法  dt.Rows.Remove(dt.Rows[0]);  //使用DataTable.Rows.RemoveAt(index)方法  dt.Rows.RemoveAt(0);  //使用DataRow.Delete()方法  dt.Row[0].Delete();  dt.AcceptChanges();  //-----区别和注意点-----  //Remove()和RemoveAt()方法是直接删除  //Delete()方法只是将该行标记为deleted,但是还存在,还可DataTable.RejectChanges()回滚,使该行取消删除。  //用Rows.Count来获取行数时,还是删除之前的行数,需要使用DataTable.AcceptChanges()方法来提交修改。  //如果要删除DataTable中的多行,应该采用倒序循环DataTable.Rows,而且不能用foreach进行循环删除,因为正序删除时索引会发生变化,程式发生异常,很难预料后果。  for ( int i = dt.Rows.Count - 1; i >= 0; i--)  {   dt.Rows.RemoveAt(i);  }

 Copying and sorting of 7,datatable

Copy table, copy table structure and table data datatable dtnew = new DataTable ();d tnew = dt. Copy ();//clone table, just copy table structure, not including data datatable dtnew = new DataTable ();d tnew = dt. Clone ();//Sort DataView dv = dt. defaultview;//Gets the table view DV. sort = "id DESC";//Sort dv.totable () in reverse order of ID;//Convert to Table

  

 

C#datatable some ways to use

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.