WinForm] [Miscellany (6): C # the DataTable class (summary)

Table of contents

Attributes

  • Gets the number of ranks

method

  • Add Column
  • Add Row
  • Delete the ranks
  • Gets the column name
  • Gets the column type
  • Change the column name
  • Get Cell Value
  • Value is written into a cell

other

  • Transpose
  • Adding a column of a table to another table
  • Adding a row of a table to another table

 

Attributes

 Gets the number of ranks

int numOfRows = dt.Rows.Count;
int numOfCols = dt.Columns.Count;

  

method

Add Column

dt.Columns.Add("Name", typeof(string)).SetOrdinal(0);//to the first column
DataColumn dc1 = new DataColumn("Tol", typeof(string));
dt.Columns.Add(dc1);

Add Row

//method1
DataRow dr1 = dt.NewRow();
dt.Rows.Add(dr1);
//method2
dt.Rows.Add();

 Delete the ranks

dt.Columns.RemoveAt(index);//index is location of col
dt.Rows.RemoveAt(index);

  

Gets the column name

dt.Columns[i].ColumnName.ToString();

Gets the column type

Type dataType= dataDt.Columns["Name"].DataType

Change the column name

dt.Columns[0].ColumnName = "AAA";

  

Get Cell Value

var val = dt.Rows[i][j].toString();  

Value is written into a cell

dt.Rows[i][j]=val;//The type of val should be the same as the column, which the cell belongs to

  

The complete code

private void tableoperations()
{
    System.Data.DataTable dt = new System.Data.DataTable();
    int index = 0;
    int i = 0, j = 0;
          
    //add new column
    dt.Columns.Add("Name", typeof(string)).SetOrdinal(0);//to the first column
    DataColumn dc1 = new DataColumn("Tol", typeof(string));
    dt.Columns.Add(dc1);

    //add new row
    //method1
    DataRow dr1 = dt.NewRow();
    dt.Rows.Add(dr1);
    //method2
    dt.Rows.Add();

    //delet columns
    dt.Columns.RemoveAt(index);
    //delet rows
    dt.Rows.RemoveAt(index);

    //get the column name
    dt.Columns[i].ColumnName.ToString();
    //change columns name:
    dt.Columns[0].ColumnName = "AAA";

    //read/write the cell value
    dt.Rows[i][j] = 1;
    //get column type
    Type dataType = dt.Columns["Name"].DataType;

}

  

other

Transpose

private System.Data.DataTable transpositioin(System.Data.DataTable tb_org, int col_start = 0)//the original one has names of rows and columns
{
    System.Data.DataTable tb_trans = new System.Data.DataTable();
    tb_trans.Columns.Add("item", typeof(string));
    for (int i = 0; i < tb_org.Rows.Count; i++) {
        tb_trans.Columns.Add(tb_org.Rows[i][col_start].ToString(), typeof(string));//can change the type
    }
    for (int i = 0; i < tb_org.Columns.Count; i++) {//here i only need to start from the (start+1)th column
        tb_trans.Rows.Add();
        tb_trans.Rows[i][0] = tb_org.Columns[i].ColumnName.ToString();//add row name
        for (int j = 0; j < tb_org.Rows.Count; j++) {
            tb_trans.Rows[i][j + 1] = double.Parse(tb_org.Rows[j][i].ToString());//add value, the first column is the orinigal column name
        }
    }
    return tb_trans;
}

Adding a column to a table (empathy line added) another table

private System.Data.DataTable getcolumn(System.Data.DataTable tb_WithTarCol)
{
    System.Data.DataTable tb = new System.Data.DataTable();
    tb.Columns.Add(tb_WithTarCol.Columns[0].ColumnName.ToString(), typeof(string));//here, get the first column
    foreach (DataRow dr in tb_WithTarCol.Rows) {
        DataRow tmp = tb.NewRow();
        tmp[0] = dr[0];//get the first cell, which belongs to the first column
        tb.Rows.Add(tmp);
    }
    return tb;
}

  

 

Guess you like

Origin www.cnblogs.com/RicardoIsLearning/p/12116037.html