].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的列名
【追加】DataTable与XML的转换
System.IO.TextReader trSchema = new System.IO.StringReader(xml.Substring(xml.IndexOf("<?xml")));
DataTable dtReturn = new DataTable();
dtReturn.ReadXmlSchema(trSchema);
dtReturn.ReadXml(trDataTable);
return dtReturn;
}
【追加】排序的好方法
dt=dt.DefaultView.ToTable();
转载自:http://www.cnblogs.com/LiveStar/archive/2009/01/14/1375774.html