DataTable常用操作總結
引言】專案中經常使用DataTable,發現有好多東西,需要整理下.
【追加】判斷一個字串是否為DataTable的列名
【追加】DataTable與XML的轉換
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();
}
{
//①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();
}
【追加】判斷一個字串是否為DataTable的列名
dtInfo.Columns.Contains("AX");
【追加】DataTable與XML的轉換
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt_AX = new DataTable();
//dt_AX.Columns.Add("Sex", typeof(System.Boolean));
//DataRow dr = dt_AX.NewRow();
//dr["Sex"] = true;
//dt_AX.Rows.Add(dr);
string xml=ConvertBetweenDataTableAndXML_AX(dt_AX);
DataTable dt = ConvertBetweenDataTableAndXML_AX(xml);
}
public string ConvertBetweenDataTableAndXML_AX(DataTable dtNeedCoveret)
{
System.IO.TextWriter tw = new System.IO.StringWriter();
//if TableName is empty, WriteXml() will throw Exception.
dtNeedCoveret.TableName=dtNeedCoveret.TableName.Length==0?"Table_AX":dtNeedCoveret.TableName;
dtNeedCoveret.WriteXml(tw);
dtNeedCoveret.WriteXmlSchema(tw);
return tw.ToString();
}
public DataTable ConvertBetweenDataTableAndXML_AX(string xml)
{
System.IO.TextReader trDataTable = new System.IO.StringReader(xml.Substring(0, xml.IndexOf("
{
DataTable dt_AX = new DataTable();
//dt_AX.Columns.Add("Sex", typeof(System.Boolean));
//DataRow dr = dt_AX.NewRow();
//dr["Sex"] = true;
//dt_AX.Rows.Add(dr);
string xml=ConvertBetweenDataTableAndXML_AX(dt_AX);
DataTable dt = ConvertBetweenDataTableAndXML_AX(xml);
}
public string ConvertBetweenDataTableAndXML_AX(DataTable dtNeedCoveret)
{
System.IO.TextWriter tw = new System.IO.StringWriter();
//if TableName is empty, WriteXml() will throw Exception.
dtNeedCoveret.TableName=dtNeedCoveret.TableName.Length==0?"Table_AX":dtNeedCoveret.TableName;
dtNeedCoveret.WriteXml(tw);
dtNeedCoveret.WriteXmlSchema(tw);
return tw.ToString();
}
public DataTable ConvertBetweenDataTableAndXML_AX(string xml)
{
System.IO.TextReader trDataTable = new System.IO.StringReader(xml.Substring(0, xml.IndexOf("
相關文章
- ?Git 常用操作總結Git
- javascript中字串常用操作總結JavaScript字串
- Python時間模組常用操作總結Python
- 常用基礎Linux操作命令總結與hadoop基礎操作命令LinuxHadoop
- Git 常用操作彙總Git
- My SQL常用操作彙總SQL
- 常用TS總結
- numpy 常用總結
- 【vim】常用總結
- C++中list的使用方法及常用list操作總結C++
- MySQL新增列、刪除列,建立主鍵等常用操作總結MySql
- 常用js方法總結:JS
- 常用kubectl命令總結
- github常用指令總結Github
- java常用Api總結JavaAPI
- phaser常用API總結API
- 常用git命令總結Git
- 常用PDF庫總結
- Rails常用gem總結AI
- css常用大總結CSS
- openssl常用使用總結
- Dapper常用方法總結APP
- Python集合操作總結Python
- FutureTask基本操作總結
- dataTable.js使用總結(包含序號生成,懸浮框使用等)JS
- MySql常用操作SQL語句彙總MySql
- Object物件常用方法總結Object物件
- 常用Java集合類總結Java
- Python字串常用方法總結Python字串
- Vue專案常用總結Vue
- Javascript Object常用方法總結JavaScriptObject
- 【個人總結】常用技巧
- jQuery常用方法總結【三】jQuery
- PHP常用函式總結PHP函式
- JS中DOM操作總結JS
- 前端資料操作總結前端
- RMAN命令LIST操作總結
- Android鍵盤操作總結Android
- android IO流操作總結Android