ElementContentGrid.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using OTSIncAReportApp.SysMgrTools;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace OTSIncAReportGrids
  9. {
  10. /// <summary>
  11. /// 元素含量表
  12. /// </summary>
  13. public partial class ElementContentGrid : UserControl
  14. {
  15. #region 设置变量
  16. //设置模拟数据表
  17. public DataTable m_dt = new DataTable();
  18. //国际化
  19. Language lan;
  20. Hashtable table;
  21. OTSIncAReportApp.frmReportApp m_ReportApp;
  22. int gridwidth = 0;
  23. #endregion
  24. #region 自定义方法
  25. /// <summary>
  26. /// 绑定datagridview数据
  27. /// </summary>
  28. public void BindDataGridView(string path, int num, string filename)
  29. {
  30. OTSIncAReportApp.DataOperation.DataAccess.ParticleData particledata = new OTSIncAReportApp.DataOperation.DataAccess.ParticleData(path);
  31. OTSGridView gridView = new OTSGridView();
  32. List<string> colid = new List<string>() { "rowid", "ename", "percent" };
  33. List<string> colname = new List<string>() { table["str1"].ToString(), table["str8"].ToString(), table["str9"].ToString() };
  34. //创建Grid的列
  35. for (int i = 0; i < colid.Count; i++)
  36. {
  37. gridView.Columns.Add(colid[i], colname[i]);
  38. }
  39. //设置grid默认值的样式,防止图像为空时有红x
  40. for (int i = 0; i < gridView.Columns.Count; i++)
  41. {
  42. gridView.Columns[i].DefaultCellStyle.NullValue = null;
  43. }
  44. int idx = m_ReportApp.m_DataMgrFun.GetSampleIndexByPropItemName(m_ReportApp.SourceGridData.SampleDataList, OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
  45. int sel = m_ReportApp.SourceGridData.SampleDataList[idx].comboDownList.IndexOf(m_ReportApp.SourceGridData.SampleDataList[idx].itemVal.ToString());
  46. string filedAndParticl = "";
  47. if (sel == 1)
  48. {
  49. List<OTSIncAReportApp.DataOperation.Model.Particle> selectParticles = m_ReportApp.SelectedParticles;
  50. foreach (var item in selectParticles)
  51. {
  52. filedAndParticl = filedAndParticl + "," + (item.FieldId.ToString() + "-" + item.ParticleId.ToString());
  53. }
  54. if (filedAndParticl != "")
  55. {
  56. filedAndParticl = filedAndParticl + ",";
  57. }
  58. }
  59. if (filedAndParticl != "")
  60. {
  61. DataTable dt = particledata.GetElementForArea(filedAndParticl);
  62. double totalArea = 0;
  63. foreach (DataRow item in dt.Rows)
  64. {
  65. totalArea += Convert.ToDouble(item["earea"]);
  66. }
  67. for (int i = 0; i < dt.Rows.Count; i++)
  68. {
  69. int add_rowindex = gridView.Rows.Add();
  70. gridView.Rows[i].Cells[0].Value = (i + 1).ToString();
  71. gridView.Rows[i].Cells[1].Value = dt.Rows[i]["name"];
  72. double d = Convert.ToDouble(dt.Rows[i]["earea"]) / totalArea;
  73. gridView.Rows[i].Cells[2].Value = Math.Round(d, 6) * 100;
  74. gridView.Rows[i].Cells[1].Style.BackColor = Color.Azure;
  75. gridView.Rows[i].Cells[2].Style.BackColor = Color.Azure;
  76. }
  77. }
  78. else
  79. {
  80. Hashtable hashtable = new Hashtable();
  81. DataTable dt = particledata.GetElementForArea(filedAndParticl);
  82. double totalArea = 0;
  83. foreach (DataRow item in dt.Rows)
  84. {
  85. totalArea += Convert.ToDouble(item["earea"]);
  86. hashtable.Add(item["name"].ToString(), Convert.ToDouble(item["earea"]));
  87. }
  88. DataTable dt1 = particledata.GetSmallElementForArea();
  89. foreach (DataRow item in dt1.Rows)
  90. {
  91. totalArea += Convert.ToDouble(item["earea"]);
  92. if (hashtable.Contains(item["name"].ToString()))
  93. {
  94. double temp = Convert.ToDouble(item["earea"]) + Convert.ToDouble(hashtable[item["name"].ToString()]);
  95. hashtable[item["name"].ToString()] = temp;
  96. }
  97. else
  98. {
  99. hashtable.Add(item["name"].ToString(), Convert.ToDouble(item["earea"]));
  100. }
  101. }
  102. foreach (DictionaryEntry de in hashtable) //ht为一个Hashtable实例
  103. {
  104. int add_rowindex = gridView.Rows.Add();
  105. gridView.Rows[add_rowindex].Cells[0].Value = (add_rowindex + 1).ToString();
  106. gridView.Rows[add_rowindex].Cells[1].Value = de.Key;
  107. double d = Convert.ToDouble(de.Value) / totalArea;
  108. gridView.Rows[add_rowindex].Cells[2].Value = Math.Round(d, 6) * 100;
  109. gridView.Rows[add_rowindex].Cells[1].Style.BackColor = Color.Azure;
  110. gridView.Rows[add_rowindex].Cells[2].Style.BackColor = Color.Azure;
  111. }
  112. }
  113. gridView.Name = "gridView" + num.ToString();
  114. SetDataGridViewStyle(gridView);
  115. panel1.Controls.Add(gridView);
  116. gridwidth = gridwidth + 1;
  117. }
  118. /// <summary>
  119. /// 设置DataGridView样式
  120. /// </summary>
  121. private void SetDataGridViewStyle(OTSGridView gridView)
  122. {
  123. gridView.AllowUserToAddRows = false;
  124. gridView.AllowUserToDeleteRows = false;
  125. gridView.AllowUserToResizeRows = false;
  126. gridView.BackgroundColor = System.Drawing.SystemColors.ButtonHighlight;
  127. gridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  128. gridView.ContextMenuStrip = this.contextMenuStrip1;
  129. gridView.Dock = System.Windows.Forms.DockStyle.Fill;
  130. gridView.Location = new System.Drawing.Point(0, 0);
  131. gridView.Margin = new System.Windows.Forms.Padding(2);
  132. gridView.MergeColumnHeaderBackColor = System.Drawing.SystemColors.ButtonHighlight;
  133. gridView.Name = "Gview_gz";
  134. gridView.ReadOnly = true;
  135. gridView.RowHeadersVisible = false;
  136. gridView.RowHeadersWidth = 40;
  137. gridView.RowTemplate.Height = 30;
  138. gridView.Size = new System.Drawing.Size(667, 520);
  139. gridView.TabIndex = 0;
  140. gridView.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(this.Gview_gz_SortCompare);
  141. gridView.Sorted += new System.EventHandler(this.Gview_gz_Sorted);
  142. //用户不能调整标题的高度
  143. gridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  144. //用户不能调整 行高
  145. gridView.AllowUserToResizeRows = false;
  146. //改变行的高度;
  147. //gridView.RowTemplate.Height = 20;
  148. //点击选择整行
  149. gridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  150. //居中显示
  151. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  152. dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
  153. gridView.DefaultCellStyle = dataGridViewCellStyle1;
  154. gridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  155. //再次重覆禁用拖动表头高度,居然有效果了
  156. gridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  157. //设置grid可以复制
  158. gridView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  159. //设置每列的宽度
  160. gridView.Columns[0].Width = 40;//第一列序号的宽度设置一下吧,要不太丑
  161. gridView.Columns[1].Width = 200;
  162. //设置序号列不排序
  163. gridView.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
  164. //设置序号列不可以设置宽度
  165. gridView.Columns[0].Resizable = DataGridViewTriState.False;
  166. }
  167. #endregion
  168. #region 窗体初始化及加载
  169. /// <summary>
  170. /// 元素含量表,构造函数
  171. /// </summary>
  172. /// <param name="in_sec">传入,委托类对象</param>
  173. /// <param name="in_Cotsreportprojfilemgrclr">传入,项目管理类对象</param>
  174. public ElementContentGrid(OTSIncAReportApp.frmReportApp ReportApp)
  175. {
  176. m_ReportApp = ReportApp;
  177. InitializeComponent();
  178. //国际化
  179. lan = new Language(this);
  180. table = lan.GetNameTable(this.Name);
  181. }
  182. private void ElementContentGrid_Load(object sender, EventArgs e)
  183. {
  184. int idx = m_ReportApp.m_DataMgrFun.GetSampleIndexByPropItemName(m_ReportApp.SourceGridData.SampleDataList, OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE);
  185. string sou = m_ReportApp.SourceGridData.SampleDataList[idx].itemVal.ToString();
  186. if (sou.Contains("+"))
  187. {
  188. for (int i = 0; i < sou.Split('+').Length; i++)
  189. {
  190. OTSIncAReportApp.DataOperation.Model.ResultFile resultFile = m_ReportApp.resultFilesList.Find(s => s.FileName == sou.Split('+')[i]);
  191. if (resultFile != null)
  192. {
  193. BindDataGridView(resultFile.FilePath, i, resultFile.FileName);
  194. }
  195. }
  196. }
  197. else
  198. {
  199. BindDataGridView(m_ReportApp.resultFilesList[m_ReportApp.WorkingResult].FilePath, 0, m_ReportApp.resultFilesList[m_ReportApp.WorkingResult].FileName);
  200. }
  201. }
  202. #endregion
  203. #region 相关事件
  204. private void ToolStripMenuItem1_Click(object sender, EventArgs e)
  205. {
  206. //复制整个表
  207. CopyAll();
  208. }
  209. private void ToolStripMenuItem2_Click(object sender, EventArgs e)
  210. {
  211. //复制选择区域
  212. CopySelected();
  213. }
  214. /// <summary>
  215. /// 复制选择区域
  216. /// </summary>
  217. public void CopySelected()
  218. {
  219. foreach (var item in panel1.Controls)
  220. {
  221. if (item.GetType().ToString() == "OTSGridView")
  222. {
  223. //复制选择区域
  224. object oo = ((OTSGridView)item).GetClipboardContent();
  225. Clipboard.SetDataObject(((OTSGridView)item).GetClipboardContent());
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 复制所有
  231. /// </summary>
  232. public void CopyAll()
  233. {
  234. foreach (var item in panel1.Controls)
  235. {
  236. if (item.GetType().ToString() == "OTSGridView")
  237. {
  238. ((OTSGridView)item).SelectAll();
  239. Clipboard.SetDataObject(((OTSGridView)item).GetClipboardContent());
  240. }
  241. }
  242. }
  243. //恢复至初始状态
  244. private void ToolStripMenuItem3_Click(object sender, EventArgs e)
  245. {
  246. foreach (var item in panel1.Controls)
  247. {
  248. if (item.GetType().ToString() == "OTSGridView")
  249. {
  250. panel1.Controls.Remove((OTSGridView)item);
  251. }
  252. }
  253. gridwidth = 0;
  254. int idx = m_ReportApp.m_DataMgrFun.GetSampleIndexByPropItemName(m_ReportApp.SourceGridData.SampleDataList, OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE);
  255. string sou = m_ReportApp.SourceGridData.SampleDataList[idx].itemVal.ToString();
  256. if (sou.Contains("+"))
  257. {
  258. for (int i = 0; i < sou.Split('+').Length; i++)
  259. {
  260. OTSIncAReportApp.DataOperation.Model.ResultFile resultFile = m_ReportApp.resultFilesList.Find(s => s.FileName == sou.Split('+')[i]);
  261. if (resultFile != null)
  262. {
  263. BindDataGridView(resultFile.FilePath, i, resultFile.FileName);
  264. }
  265. }
  266. }
  267. else
  268. {
  269. BindDataGridView(m_ReportApp.resultFilesList[m_ReportApp.WorkingResult].FilePath, 0, m_ReportApp.resultFilesList[m_ReportApp.WorkingResult].FileName);
  270. }
  271. }
  272. private void Gview_gz_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
  273. {
  274. //排序中
  275. if (((OTSGridView)sender).Rows[e.RowIndex1].Tag != null && ((OTSGridView)sender).Rows[e.RowIndex1].Tag.ToString() == "统计行")
  276. {
  277. //ROW1>ROW2
  278. e.SortResult = 1;
  279. if (((OTSGridView)sender).SortOrder == SortOrder.Descending)
  280. e.SortResult = -1;
  281. e.Handled = true;
  282. return;
  283. }
  284. if (((OTSGridView)sender).Rows[e.RowIndex2].Tag != null && ((OTSGridView)sender).Rows[e.RowIndex2].Tag.ToString() == "统计行")
  285. {
  286. //ROW1<ROW2
  287. e.SortResult = -1;
  288. if (((OTSGridView)sender).SortOrder == SortOrder.Descending)
  289. e.SortResult = 1;
  290. e.Handled = true;
  291. return;
  292. }
  293. }
  294. private void Gview_gz_Sorted(object sender, EventArgs e)
  295. {
  296. //排序完成,重新设置序号
  297. for (int i = 0; i < ((OTSGridView)sender).Rows.Count; i++)
  298. {
  299. if (((OTSGridView)sender).Rows[i].Tag != null && ((OTSGridView)sender).Rows[i].Tag.ToString() == "统计行")
  300. { }
  301. else
  302. {
  303. ((OTSGridView)sender).Rows[i].Cells[0].Value = i.ToString();
  304. }
  305. }
  306. }
  307. #endregion
  308. #region 获取向导出模块提供的DataTable和GridView对象
  309. /// <summary>
  310. /// 获取到该模块输出后形成的DataTable,和GridView
  311. /// </summary>
  312. /// <param name="out_dt"></param>
  313. /// <param name="out_dg"></param>
  314. public void GetDataTableAndGridView(out DataTable out_dt, out DataGridView out_dg)
  315. {
  316. out_dt = m_dt;
  317. out_dg = null;
  318. foreach (var item in panel1.Controls)
  319. {
  320. if (item.GetType().ToString() == "OTSGridView")
  321. {
  322. out_dg = ((OTSGridView)item);
  323. }
  324. }
  325. }
  326. #endregion
  327. }
  328. }