其他分享
首页 > 其他分享> > .net中对DataTable的操作总结

.net中对DataTable的操作总结

作者:互联网

原文链接:http://www.cnblogs.com/ckblogs/p/3708438.html

protected void Page_Load(object sender, EventArgs e)
    {
       //①Create a DataTable
          DataTable dt = new DataTable("Table_AX");

        //
②Add columns for DataTable
        //Method 1

          dt.Columns.Add("column0", System.Type.GetType("System.String"));
        //Method 2

          DataColumn dc = new DataColumn("column1", System.Type.GetType("System.Boolean"));
          dt.Columns.Add(dc);

        //
③Add rows for DataTable
        //★Initialize the row

          DataRow dr = dt.NewRow();
          dr["column0"] = "AX"
;
          dr["column1"] = true
;
          dt.Rows.Add(dr);
        //★Doesn't initialize the row

          DataRow dr1 = dt.NewRow();
          dt.Rows.Add(dr1);

        //
④Select row
        //Search the second row 如果没有赋值,则用is null来select

         DataRow[] drs = dt.Select("column1 is null");
          DataRow[] drss = dt.Select("column0 = 'AX'"
);


        //⑤Copy DataTable include data
          DataTable dtNew = dt.Copy();

        //⑥Copy DataTable only scheme
          DataTable dtOnlyScheme = dt.Clone();

        //⑦Operate one row
        //★都是对dt的操作
        //Method 1
          DataRow drOperate = dt.Rows[0];
          drOperate["column0"] = "AXzhz";
          drOperate["column1"] = false;
        //Method 2
          drOperate[0] = "AXzhz";
          drOperate[1] = false;
        //Method 3
          dt.Rows[0]["column0"] = "AXzhz";
          dt.Rows[0]["column1"] = false;
        //Method 4
          dt.Rows[0][0] = "AXzhz";
          dt.Rows[0][1] = false;

        //⑧Evaluate another DataTable's row to current Datatable
          dtOnlyScheme.Rows.Add(dt.Rows[0].ItemArray);

        //⑨Use Rowstate
        //■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //不知道怎么才能使Row的State变为DataRowState.Deleted
        //More further,怎样设置Row的State
        //■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        if (dt.Rows[0].RowState == DataRowState.Unchanged)
        {
            //Your logic
          }

        //⑩Convert to string
          System.IO.StringWriter sw = new System.IO.StringWriter();
          System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw);
          dt.WriteXml(xw);
        string s = sw.ToString();

        //ⅰ.string convert to DataTable【Doesn't achieve it】
        //■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //没有把string成功转换为DataTable         ■■已实现,参见追加■■
        //■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        //DataTable dtConvert = new DataTable();
        //System.IO.StringReader stream = new System.IO.StringReader(s);
        //System.Xml.XmlReader xtr = new System.Xml.XmlTextReader(stream);
        //dtConvert.ReadXml(xtr);

        //ⅱ.Filter DataTable
        //It's so strange that the second row has been filtered
        //the second row show in GridView never
        //It means null field will be filter always.
        //Filter the all conditions
          dt.DefaultView.RowFilter = "column1 <> true";
        //dt.DefaultView.RowFilter = "column1 = true";

          dt.DefaultView.RowStateFilter = DataViewRowState.Added;

        //ⅲ.Sort row
        //Stupid method
          DataRow[] drsss = dt.Select(String.Empty, "column0 DESC , column1 ASC");
        //Clever method
          dt.DefaultView.Sort = "column0 , column1 ASC";

        //ⅳ.Bind DataTable
        //绑定的其实是DefaultView
          gvTestDataTable.DataSource = dt;
          gvTestDataTable.DataBind();
      }

转载于:https://www.cnblogs.com/ckblogs/p/3708438.html

标签:总结,Rows,System,DataTable,net,column1,dt,row
来源: https://blog.csdn.net/weixin_30231093/article/details/97882512