ResultGrid.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using OTSIncAReportApp;
  2. using OTSIncAReportApp.DataOperation.DataAccess;
  3. using OTSIncAReportApp.OTSSampleReportInfo;
  4. using OTSIncAReportApp.SysMgrTools;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Windows.Forms;
  10. using OTSIncAReportApp.OTSRstMgrFunction;
  11. namespace OTSIncAReportGrids
  12. {
  13. public partial class ResultGrid : UserControl
  14. {
  15. #region 变量定义
  16. //测量结果
  17. frmReportApp m_frmReportApp = null;
  18. //国际化
  19. Language lan;
  20. public Hashtable table;
  21. frmReportConditionChoose m_condition;
  22. #endregion
  23. #region 窗体加载及构造函数
  24. public ResultGrid(frmReportApp frmReportApp)
  25. {
  26. m_frmReportApp = frmReportApp;
  27. m_condition = frmReportApp.m_conditionChoose;
  28. InitializeComponent();
  29. //国际化
  30. lan = new Language(this);
  31. table = lan.GetNameTable(this.Name);
  32. }
  33. private void ResultGrid_Load(object sender, EventArgs e)
  34. {
  35. BindGrid();
  36. SetDataGridViewStyleIncLib();
  37. }
  38. #endregion
  39. #region 绑定数据
  40. private void BindGrid()
  41. {
  42. string str1 = table["str1"].ToString();
  43. string str8 = table["str8"].ToString();
  44. Gview_gz.Rows.Clear();
  45. Gview_gz.Columns.Clear();
  46. //创建列
  47. Gview_gz.Columns.Add(str1, str1);
  48. Gview_gz.Columns.Add(str8, str8);
  49. //自适应列宽
  50. if (this.Width > 0)
  51. {
  52. //再对不同的列宽度进行设置
  53. Gview_gz.Columns[0].Width = this.Width / 2;
  54. Gview_gz.Columns[1].Width = this.Width / 2;
  55. }
  56. //先设置一下头的高度,否则会太矮不好看
  57. Gview_gz.ColumnHeadersVisible = false;
  58. Gview_gz.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  59. Gview_gz.ColumnHeadersHeight = 30;
  60. string sou = m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemDisplayVal.ToString();
  61. if (sou.Contains("+"))
  62. {
  63. for (int i = 0; i < sou.Split('+').Length; i++)
  64. {
  65. ResultFile resultFile = m_frmReportApp.m_rstDataMgr.ResultFilesList.Find(s => s.anotherFileName == sou.Split('+')[i]);
  66. if (resultFile != null)
  67. {
  68. addGrid(resultFile);
  69. }
  70. }
  71. }
  72. else
  73. {
  74. for (int i=0;i< m_frmReportApp.m_rstDataMgr.ResultFilesList.Count;i++)
  75. {
  76. if(sou== m_frmReportApp.m_rstDataMgr.ResultFilesList[i].anotherFileName.ToString())
  77. addGrid(m_frmReportApp.m_rstDataMgr.ResultFilesList[i]);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 获取测量结果概况信息数据
  83. /// </summary>
  84. /// <returns></returns>
  85. public Dictionary<string, string> GetData_ResultGrid(ResultFile resultFile)
  86. {
  87. ResultFile resfile = resultFile;
  88. Dictionary<string, string> keyValues = new Dictionary<string, string>() { };
  89. keyValues.Add(table["col1"].ToString(), resfile.anotherFileName);
  90. string path = resfile.FilePath;
  91. FieldData fielddata = new FieldData(path);
  92. DataTable dt = fielddata.GetGeneralInfo();
  93. DateTime star = new DateTime();
  94. DateTime end = new DateTime();
  95. System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
  96. dtFormat.ShortDatePattern = "dd/MM/yyyy hh:mm:ss";
  97. if (dt.Rows.Count > 0)
  98. {
  99. for (int i = 0; i < dt.Rows.Count; i++)
  100. {
  101. if (dt.Rows[i]["name"].ToString() == "TimeStart")
  102. {
  103. star = Convert.ToDateTime(dt.Rows[i]["value"], dtFormat);
  104. }
  105. if (dt.Rows[i]["name"].ToString() == "TimeEnd")
  106. {
  107. end = Convert.ToDateTime(dt.Rows[i]["value"], dtFormat);
  108. }
  109. }
  110. }
  111. TimeSpan timeSpan = end - star;
  112. int filedCount = fielddata.GetFiledCount();
  113. double scanFieldSize = resfile.GetScanFieldSizeX();
  114. double wide = resfile.GetImageWidth();
  115. double high = resfile.GetImageHeight();
  116. double filedsArea= resfile.GetTotalArea();
  117. if (filedsArea == -1)
  118. {
  119. filedsArea = scanFieldSize * (scanFieldSize * high / wide) * filedCount;
  120. }
  121. double particleArea = fielddata.GetparticleArea();
  122. double Magnification= resfile.GetMeasurementMagnification();
  123. //报告参数文件
  124. string xmlpath = Application.StartupPath + RptConfigFile.m_ReportMgrParamFile;
  125. DataSet ds = XMLoperate.GetXmlData(xmlpath, "XMLData");
  126. DataTable dt1 = ds.Tables["Member"];
  127. string sizestr = "1000";
  128. for (int i = 0; i < dt1.Rows.Count; i++)
  129. {
  130. if (dt1.Rows[i]["RegName"].ToString() == "Scale")
  131. {
  132. sizestr = dt1.Rows[i]["strValue"].ToString();
  133. break;
  134. }
  135. }
  136. double ratio = Math.Round(particleArea / filedsArea*double.Parse(sizestr),3);
  137. keyValues.Add(table["col2"].ToString(), star.ToString("yyyy-MM-dd HH:mm:ss"));
  138. keyValues.Add(table["col3"].ToString(), end.ToString("yyyy-MM-dd HH:mm:ss"));
  139. keyValues.Add(table["col4"].ToString(), Math.Round(timeSpan.TotalMinutes, 2).ToString() + "mins");
  140. keyValues.Add(table["col5"].ToString(), filedCount.ToString());
  141. keyValues.Add(table["col12"].ToString(), Math.Round(Magnification, 2).ToString());
  142. string LibraryName = "";
  143. string rstSTD = resfile.GetSTDName();
  144. if ("NoSTDDB"== rstSTD ||
  145. "NoSTDDB.db" == rstSTD)
  146. {
  147. LibraryName = "OTSIncASysSTD";
  148. }
  149. else
  150. {
  151. LibraryName = rstSTD;
  152. }
  153. keyValues.Add(table["col6"].ToString(), LibraryName);
  154. keyValues.Add(table["col7"].ToString(), fielddata.GetparticleCount().ToString());
  155. keyValues.Add(table["col8"].ToString() + "\x00B2)",Math.Round(filedsArea,2).ToString());
  156. keyValues.Add(table["col9"].ToString()+ "\x00B2)", String.Format("{0:f2}", particleArea));
  157. keyValues.Add(table["col10"].ToString(), ratio.ToString());
  158. //keyValues.Add(table["col11"].ToString(), sizestr);
  159. return keyValues;
  160. }
  161. public Dictionary<string, string> GetData_ResultGrid_Report()
  162. {
  163. ResultFile resfile = m_frmReportApp.m_rstDataMgr.ResultFilesList[m_frmReportApp.m_rstDataMgr.GetWorkingResultId()];
  164. Dictionary<string, string> keyValues = new Dictionary<string, string>() { };
  165. keyValues.Add(table["col1"].ToString(), resfile.anotherFileName);
  166. string path = resfile.FilePath;
  167. FieldData fielddata = new FieldData(path);
  168. DataTable dt = fielddata.GetGeneralInfo();
  169. DateTime star = new DateTime();
  170. DateTime end = new DateTime();
  171. System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
  172. dtFormat.ShortDatePattern = "dd/MM/yyyy hh:mm:ss";
  173. if (dt.Rows.Count > 0)
  174. {
  175. for (int i = 0; i < dt.Rows.Count; i++)
  176. {
  177. if (dt.Rows[i]["name"].ToString() == "TimeStart")
  178. {
  179. star = Convert.ToDateTime(dt.Rows[i]["value"], dtFormat);
  180. }
  181. if (dt.Rows[i]["name"].ToString() == "TimeEnd")
  182. {
  183. end = Convert.ToDateTime(dt.Rows[i]["value"], dtFormat);
  184. }
  185. }
  186. }
  187. TimeSpan timeSpan = end - star;
  188. double Magnification = resfile.GetMeasurementMagnification();
  189. int filedCount = fielddata.GetFiledCount();
  190. double wide = resfile.GetImageWidth();
  191. double high = resfile.GetImageHeight();
  192. double pixelSize = resfile.GetPixelSize();
  193. //double filedsArea = Math.Round(high * wide * pixelSize * pixelSize * filedCount, 2);
  194. double particleArea = fielddata.GetparticleArea();
  195. double filedsArea = resfile.GetTotalArea();
  196. //double ratio = particleArea / filedsArea;
  197. //报告参数文件
  198. string xmlpath = Application.StartupPath + RptConfigFile.m_ReportMgrParamFile;
  199. DataSet ds = XMLoperate.GetXmlData(xmlpath, "XMLData");
  200. DataTable dt1 = ds.Tables["Member"];
  201. string sizestr = "1000";
  202. for (int i = 0; i < dt1.Rows.Count; i++)
  203. {
  204. if (dt1.Rows[i]["RegName"].ToString() == "Scale")
  205. {
  206. sizestr = dt1.Rows[i]["strValue"].ToString();
  207. break;
  208. }
  209. }
  210. double ratio = Math.Round(particleArea / filedsArea * double.Parse(sizestr), 3);
  211. keyValues.Add(table["col2"].ToString(), star.ToString("yyyy-MM-dd HH:mm:ss"));
  212. keyValues.Add(table["col3"].ToString(), end.ToString("yyyy-MM-dd HH:mm:ss"));
  213. keyValues.Add(table["col4"].ToString(), Math.Round(timeSpan.TotalMinutes, 2).ToString() + "mins");
  214. keyValues.Add(table["col5"].ToString(), filedCount.ToString());
  215. string LibraryName = "";
  216. string rstSTD = resfile.GetSTDName();
  217. if ("NoSTDDB" == rstSTD||
  218. "NoSTDDB.db" == rstSTD)
  219. {
  220. LibraryName = "OTSIncASysSTD";
  221. }
  222. else
  223. {
  224. LibraryName = rstSTD;
  225. }
  226. keyValues.Add(table["col6"].ToString(), LibraryName);
  227. keyValues.Add(table["col7"].ToString(), fielddata.GetparticleCount().ToString());
  228. keyValues.Add(table["col8"].ToString(), Math.Round(filedsArea, 2).ToString());
  229. keyValues.Add(table["col9"].ToString(), particleArea.ToString());
  230. keyValues.Add(table["col10"].ToString(), ratio.ToString());
  231. keyValues.Add(table["col11"].ToString(), sizestr);
  232. keyValues.Add("Magnification", Math.Round(Magnification,2).ToString());
  233. return keyValues;
  234. }
  235. /// <summary>
  236. /// 绑定GridView显示
  237. /// </summary>
  238. /// <param name="resultFile"></param>
  239. private void addGrid(ResultFile resultFile)
  240. {
  241. Dictionary<string, string> keyValues = GetData_ResultGrid(resultFile);
  242. Dictionary<string, string>.Enumerator en = keyValues.GetEnumerator();
  243. for (int irow = 0; irow < keyValues.Count; irow++)
  244. {
  245. if (en.MoveNext())
  246. {
  247. //add row
  248. int add_rowindex = 0;
  249. add_rowindex = Gview_gz.Rows.Add();
  250. if (irow == 0)
  251. {
  252. //set title style
  253. Gview_gz.Rows[add_rowindex].Cells[0].Style.BackColor = System.Drawing.Color.PowderBlue;
  254. Gview_gz.Rows[add_rowindex].Cells[1].Style.BackColor = System.Drawing.Color.PowderBlue;
  255. }
  256. else
  257. {
  258. //set row style
  259. Gview_gz.Rows[add_rowindex].Cells[0].Style.BackColor = System.Drawing.Color.LightCyan;
  260. }
  261. //set value
  262. Gview_gz.Rows[add_rowindex].Cells[0].Value = en.Current.Key;
  263. Gview_gz.Rows[add_rowindex].Cells[1].Value = en.Current.Value;
  264. }
  265. }
  266. //将左侧第一列,全部设置成System.Drawing.Color.PowderBlue; 当标题头不错
  267. for (int i = 0; i < Gview_gz.Rows.Count; i++)
  268. {
  269. Gview_gz.Rows[i].Cells[0].Style.Font = new System.Drawing.Font("黑体", 10, System.Drawing.FontStyle.Bold);
  270. Gview_gz.Rows[i].Cells[1].Style.Font = new System.Drawing.Font("黑体", 10, System.Drawing.FontStyle.Bold);
  271. }
  272. }
  273. #endregion
  274. #region 设置Grid样式
  275. /// <summary>
  276. /// 设置夹杂物列表DataGridView样式
  277. /// </summary>
  278. private void SetDataGridViewStyleIncLib()
  279. {
  280. //用户不能调整标题的高度
  281. Gview_gz.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  282. //用户不能调整 行高
  283. Gview_gz.AllowUserToResizeRows = false;
  284. //再次重覆禁用拖动表头高度,居然有效果了
  285. Gview_gz.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  286. //设置grid可以复制
  287. Gview_gz.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  288. //设置每列的宽度
  289. // dgV_IncALib.Columns[1].Width = 40;
  290. Gview_gz.Columns[0].Width = 300;
  291. //设置序号列不排序
  292. Gview_gz.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
  293. //设置序号列不可以设置宽度
  294. Gview_gz.Columns[0].Resizable = DataGridViewTriState.False;
  295. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  296. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
  297. dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
  298. Gview_gz.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
  299. Gview_gz.BackgroundColor = System.Drawing.Color.White;
  300. Gview_gz.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  301. Gview_gz.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
  302. dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
  303. dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
  304. dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  305. dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
  306. dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
  307. dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
  308. Gview_gz.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
  309. Gview_gz.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  310. Gview_gz.EnableHeadersVisualStyles = false;
  311. Gview_gz.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
  312. Gview_gz.ReadOnly = true;
  313. Gview_gz.RowHeadersVisible = false; //建议改为true;为了以后显示序号。
  314. Gview_gz.RowTemplate.Height = 23;
  315. Gview_gz.RowTemplate.ReadOnly = true;
  316. //居中
  317. Gview_gz.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
  318. }
  319. #endregion
  320. #region 得到向word中插入Table所需要使用的,该模块结合后的对象数组
  321. /// <summary>
  322. /// 获取到该模块输出后形成的DataTable,和GridView
  323. /// </summary>
  324. /// <param name="out_dt"></param>
  325. /// <param name="out_dg"></param>
  326. public void GetDataTableAndGridView(out DataTable out_dt, out DataGridView out_dg)
  327. {
  328. out_dt = null;
  329. out_dg = this.Gview_gz;
  330. }
  331. #endregion
  332. #region 相关事件
  333. /// <summary>
  334. /// 复制选择区域
  335. /// </summary>
  336. public void CopySelected()
  337. {
  338. //复制选择区域
  339. object oo = Gview_gz.GetClipboardContent();
  340. Clipboard.SetDataObject(Gview_gz.GetClipboardContent());
  341. }
  342. /// <summary>
  343. /// 复制所有
  344. /// </summary>
  345. public void CopyAll()
  346. {
  347. Gview_gz.SelectAll();
  348. Clipboard.SetDataObject(Gview_gz.GetClipboardContent());
  349. }
  350. //复制整个表
  351. private void ToolStripMenuItem1_Click(object sender, EventArgs e)
  352. {
  353. CopyAll();
  354. }
  355. //复制选择区域
  356. private void ToolStripMenuItem2_Click(object sender, EventArgs e)
  357. {
  358. CopySelected();
  359. }
  360. //恢复初始状态
  361. private void ToolStripMenuItem3_Click(object sender, EventArgs e)
  362. {
  363. }
  364. #endregion
  365. }
  366. }