|
2017/06/13(Tue) 18:52:32 編集(投稿者)
ターゲットフレームは4です。
ターゲットフレームワーク [.Net Framework4] プラットフォームターゲット [x86]
で作成しています。
DataTable.XMLの中身は以下のようになっています。
==================================================
<?xml version="1.0" standalone="yes"?> <NewDataSet> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="DISTINCT" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="DISTINCT"> <xs:complexType> <xs:sequence> <xs:element name="NAME" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> <xs:unique name="Constraint1" msdata:PrimaryKey="true"> <xs:selector xpath=".//DISTINCT" /> <xs:field xpath="NAME" /> </xs:unique> </xs:element> </xs:schema> <DISTINCT> <NAME>CG</NAME> </DISTINCT> <DISTINCT> <NAME>IR</NAME> </DISTINCT> <DISTINCT> <NAME>RY</NAME> </DISTINCT> <DISTINCT> <NAME>SR</NAME> </DISTINCT> <DISTINCT> <NAME>VR</NAME> </DISTINCT> <DISTINCT> <NAME>GN</NAME> </DISTINCT> <DISTINCT> <NAME>F2</NAME> </DISTINCT> <DISTINCT> <NAME>GI</NAME> </DISTINCT> <DISTINCT> <NAME>CJ</NAME> </DISTINCT> <DISTINCT> <NAME>MX</NAME> </DISTINCT> <DISTINCT> <NAME>C2</NAME> </DISTINCT> </NewDataSet>
==================================================
プログラムの中身は以下になります。
rtb は RichTextBox です。
================================================== namespace test { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void SetDataRow(DataTable dt, int dy, int id, string name, int count) { DataRow drw = dt.NewRow(); drw["DY"] = dy; drw["ID"] = id; drw["NAME"] = name; drw["COUNT"] = count; dt.Rows.Add(drw); }
public static DataTable Distinct(DataTable dtsource, params string[] colnames) { #if false DataTable dtdest = dtsource.Clone(); #else DataTable dtdest = new DataTable("DISTINCT"); for(int icnum = 0; icnum < dtsource.Columns.Count; icnum++) { dtdest.Columns.Add(dtsource.Columns[icnum].ColumnName, dtsource.Columns[icnum].DataType); } #endif DataColumn[] dc = new DataColumn[colnames.Length]; for (int icolnamenum = 0; icolnamenum < colnames.Length; icolnamenum++) dc[icolnamenum] = dtdest.Columns[colnames[icolnamenum]]; dtdest.PrimaryKey = dc;
foreach (DataRow drsource in dtsource.Rows) { object[] keys = new object[colnames.Length]; for (int ikeynum = 0; ikeynum < colnames.Length; ikeynum++) keys[ikeynum] = drsource[colnames[ikeynum]];
DataRow drdest = dtdest.Rows.Find(keys); if (null == drdest) dtdest.Rows.Add(drsource.ItemArray); } for (int icolnum = 0; icolnum < dtdest.Columns.Count;) { if (colnames.Contains(dtdest.Columns[icolnum].ColumnName)) { icolnum++; continue; } dtdest.Columns.Remove(dtdest.Columns[icolnum].ColumnName); } return (dtdest); }
private void button2_Click(object sender, EventArgs e) { rtb.Clear();
DataTable dtwork = new DataTable(); dtwork.Columns.Add("DY", typeof(int)); dtwork.Columns.Add("ID", typeof(int)); dtwork.Columns.Add("NAME", typeof(string)); dtwork.Columns.Add("COUNT", typeof(int));
SetDataRow(dtwork, 1, 1, "CG", 1); SetDataRow(dtwork, 1, 1, "IR", 1); SetDataRow(dtwork, 1, 1, "RY", 1); SetDataRow(dtwork, 1, 1, "SR", 3); SetDataRow(dtwork, 1, 1, "VR", 30); SetDataRow(dtwork, 1, 1, "VR", 3); SetDataRow(dtwork, 1, 1, "GN", 1); SetDataRow(dtwork, 1, 2, "F2", 1); SetDataRow(dtwork, 1, 3, "GI", 4); SetDataRow(dtwork, 1, 4, "CJ", 1); SetDataRow(dtwork, 7, 1, "MX", 1); SetDataRow(dtwork, 7, 1, "C2", 1); SetDataRow(dtwork, 7, 1, "IR", 1); SetDataRow(dtwork, 7, 1, "RY", 1); SetDataRow(dtwork, 7, 1, "SR", 2);
// 行データ取得 rtb.AppendText("機種一覧取得(重複削除)\r\n"); DataTable dttemp = Distinct(dtwork, "NAME");
DataRow[] arryrow = null; try { dttemp.TableName = "DISTINCT";
string folder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); if (dttemp.DataSet != null) { string file = System.IO.Path.Combine(folder, "DataSet.XML"); dttemp.DataSet.WriteXml(file, XmlWriteMode.WriteSchema); } else { string file = System.IO.Path.Combine(folder, "DataTable.XML"); dttemp.WriteXml(file, XmlWriteMode.WriteSchema); }
rtb.AppendText("ソート\r\n"); arryrow = dttemp.Select("", "NAME"); if (arryrow.Length <= 0) { rtb.AppendText("arryrow.length <= 0\r\n"); return; } rtb.AppendText("成功\r\n"); } catch (Exception ex) { rtb.AppendText(ex.Message + "\r\n"); } } } } ==================================================
|