123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using OTSIncAReportApp.OTSSampleReportInfo;
- using OTSIncAReportApp.SysMgrTools;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- namespace OTSIncAReportGrids
- {
- /// <summary>
- /// 元素含量表
- /// </summary>
- public partial class ElementContentGrid : UserControl
- {
- #region 设置变量
- //设置模拟数据表
- public DataTable m_dt = new DataTable();
- //国际化
- Language lan;
- Hashtable table;
- OTSIncAReportApp.frmReportApp m_ReportApp;
- int gridwidth = 0;
- #endregion
- #region 自定义方法
- /// <summary>
- /// 绑定datagridview数据
- /// </summary>
- public void BindDataGridView(string path, int num, string filename)
- {
- OTSIncAReportApp.DataOperation.DataAccess.ParticleData particledata = new OTSIncAReportApp.DataOperation.DataAccess.ParticleData(path);
- OTSGridView gridView = new OTSGridView();
- List<string> colid = new List<string>() { "rowid", "ename", "percent" };
- List<string> colname = new List<string>() { table["str1"].ToString(), table["str8"].ToString(), table["str9"].ToString() };
- //创建Grid的列
- for (int i = 0; i < colid.Count; i++)
- {
- gridView.Columns.Add(colid[i], colname[i]);
- }
- //设置grid默认值的样式,防止图像为空时有红x
- for (int i = 0; i < gridView.Columns.Count; i++)
- {
- gridView.Columns[i].DefaultCellStyle.NullValue = null;
- }
- int sel = m_ReportApp.m_conditionChoose.m_SourceGridData.GetComboDownListIndexByItemName(OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
- string filedAndParticle = "";
- if (sel == 1)
- {
- List<OTSIncAReportApp.DataOperation.Model.Particle> selectParticles = m_ReportApp.GetSelectedParticles();
- foreach (var item in selectParticles)
- {
- filedAndParticle = filedAndParticle + "," + (item.FieldId.ToString() + "-" + item.ParticleId.ToString());
- }
- if (filedAndParticle != "")
- {
- filedAndParticle = filedAndParticle + ",";
- }
- if (filedAndParticle != "")
- {
- DataTable dt = particledata.GetElementForArea(filedAndParticle);
- double totalArea = 0;
- foreach (DataRow item in dt.Rows)
- {
- totalArea += Convert.ToDouble(item["earea"]);
- }
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- int add_rowindex = gridView.Rows.Add();
- gridView.Rows[i].Cells[0].Value = (i + 1).ToString();
- gridView.Rows[i].Cells[1].Value = dt.Rows[i]["name"];
- double d = Convert.ToDouble(dt.Rows[i]["earea"]) / totalArea;
- gridView.Rows[i].Cells[2].Value = Math.Round(d, 6) * 100;
- gridView.Rows[i].Cells[1].Style.BackColor = Color.Azure;
- gridView.Rows[i].Cells[2].Style.BackColor = Color.Azure;
- }
- }
- else
- {
- }
- }
- else
- {
- Hashtable hashtable = new Hashtable();
- DataTable dt = particledata.GetElementForArea(filedAndParticle);
- double totalArea = 0;
- foreach (DataRow item in dt.Rows)
- {
- totalArea += Convert.ToDouble(item["earea"]);
- hashtable.Add(item["name"].ToString(), Convert.ToDouble(item["earea"]));
- }
- DataTable dt1 = particledata.GetSmallElementForArea();
- foreach (DataRow item in dt1.Rows)
- {
- totalArea += Convert.ToDouble(item["earea"]);
- if (hashtable.Contains(item["name"].ToString()))
- {
- double temp = Convert.ToDouble(item["earea"]) + Convert.ToDouble(hashtable[item["name"].ToString()]);
- hashtable[item["name"].ToString()] = temp;
- }
- else
- {
- hashtable.Add(item["name"].ToString(), Convert.ToDouble(item["earea"]));
- }
- }
- foreach (DictionaryEntry de in hashtable) //ht为一个Hashtable实例
- {
- int add_rowindex = gridView.Rows.Add();
- gridView.Rows[add_rowindex].Cells[0].Value = (add_rowindex + 1).ToString();
- gridView.Rows[add_rowindex].Cells[1].Value = de.Key;
- double d = Convert.ToDouble(de.Value) / totalArea;
- gridView.Rows[add_rowindex].Cells[2].Value = Math.Round(d, 6) * 100;
- gridView.Rows[add_rowindex].Cells[1].Style.BackColor = Color.Azure;
- gridView.Rows[add_rowindex].Cells[2].Style.BackColor = Color.Azure;
- }
- }
- gridView.Name = "gridView" + num.ToString();
- SetDataGridViewStyle(gridView);
- panel1.Controls.Add(gridView);
- gridwidth = gridwidth + 1;
- }
- /// <summary>
- /// 设置DataGridView样式
- /// </summary>
- private void SetDataGridViewStyle(OTSGridView gridView)
- {
- gridView.AllowUserToAddRows = false;
- gridView.AllowUserToDeleteRows = false;
- gridView.AllowUserToResizeRows = false;
- gridView.BackgroundColor = System.Drawing.SystemColors.ButtonHighlight;
- gridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
- gridView.ContextMenuStrip = this.contextMenuStrip1;
- gridView.Dock = System.Windows.Forms.DockStyle.Fill;
- gridView.Location = new System.Drawing.Point(0, 0);
- gridView.Margin = new System.Windows.Forms.Padding(2);
- gridView.MergeColumnHeaderBackColor = System.Drawing.SystemColors.ButtonHighlight;
- gridView.Name = "Gview_gz";
- gridView.ReadOnly = true;
- gridView.RowHeadersVisible = false;
- gridView.RowHeadersWidth = 40;
- gridView.RowTemplate.Height = 30;
- gridView.Size = new System.Drawing.Size(667, 520);
- gridView.TabIndex = 0;
- gridView.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(this.Gview_gz_SortCompare);
- gridView.Sorted += new System.EventHandler(this.Gview_gz_Sorted);
- //用户不能调整标题的高度
- gridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- //用户不能调整 行高
- gridView.AllowUserToResizeRows = false;
- //改变行的高度;
- //gridView.RowTemplate.Height = 20;
- //点击选择整行
- gridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
- //居中显示
- System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
- dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
- gridView.DefaultCellStyle = dataGridViewCellStyle1;
- gridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
- //再次重覆禁用拖动表头高度,居然有效果了
- gridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- //设置grid可以复制
- gridView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
- //设置每列的宽度
- gridView.Columns[0].Width = 40;//第一列序号的宽度设置一下吧,要不太丑
- gridView.Columns[1].Width = 200;
- //设置序号列不排序
- gridView.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
- //设置序号列不可以设置宽度
- gridView.Columns[0].Resizable = DataGridViewTriState.False;
- }
- #endregion
- #region 窗体初始化及加载
- /// <summary>
- /// 元素含量表,构造函数
- /// </summary>
- /// <param name="in_sec">传入,委托类对象</param>
- /// <param name="in_Cotsreportprojfilemgrclr">传入,项目管理类对象</param>
- public ElementContentGrid(OTSIncAReportApp.frmReportApp ReportApp)
- {
- m_ReportApp = ReportApp;
- InitializeComponent();
- //国际化
- lan = new Language(this);
- table = lan.GetNameTable(this.Name);
- }
- private void ElementContentGrid_Load(object sender, EventArgs e)
- {
- //int idx = m_ReportApp.m_DataMgrFun.GetPropIndexByPropItemName(m_ReportApp.SourceGridData.ConditionItemList, OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE);
- string sou = m_ReportApp.m_conditionChoose.m_SourceGridData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE).ToString();
- if (sou.Contains("+"))
- {
- for (int i = 0; i < sou.Split('+').Length; i++)
- {
- OTSIncAReportApp.DataOperation.Model.ResultFile resultFile = m_ReportApp.m_rstDataMgr.ResultFilesList.Find(s => s.FileName == sou.Split('+')[i]);
- if (resultFile != null)
- {
- BindDataGridView(resultFile.FilePath, i, resultFile.FileName);
- }
- }
- }
- else
- {
- BindDataGridView(m_ReportApp.m_rstDataMgr.ResultFilesList[m_ReportApp.m_rstDataMgr.GetWorkingResult()].FilePath, 0, m_ReportApp.m_rstDataMgr.ResultFilesList[m_ReportApp.m_rstDataMgr.GetWorkingResult()].FileName);
- }
- }
- #endregion
- #region 相关事件
- private void ToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- //复制整个表
- CopyAll();
- }
- private void ToolStripMenuItem2_Click(object sender, EventArgs e)
- {
- //复制选择区域
- CopySelected();
- }
- /// <summary>
- /// 复制选择区域
- /// </summary>
- public void CopySelected()
- {
- foreach (var item in panel1.Controls)
- {
- if (item.GetType().ToString() == "OTSGridView")
- {
- //复制选择区域
- object oo = ((OTSGridView)item).GetClipboardContent();
- Clipboard.SetDataObject(((OTSGridView)item).GetClipboardContent());
- }
- }
- }
- /// <summary>
- /// 复制所有
- /// </summary>
- public void CopyAll()
- {
- foreach (var item in panel1.Controls)
- {
- if (item.GetType().ToString() == "OTSGridView")
- {
- ((OTSGridView)item).SelectAll();
- Clipboard.SetDataObject(((OTSGridView)item).GetClipboardContent());
- }
- }
- }
- //恢复至初始状态
- private void ToolStripMenuItem3_Click(object sender, EventArgs e)
- {
- foreach (var item in panel1.Controls)
- {
- if (item.GetType().ToString() == "OTSGridView")
- {
- panel1.Controls.Remove((OTSGridView)item);
- }
- }
- gridwidth = 0;
- //int idx = m_ReportApp.m_DataMgrFun.GetPropIndexByPropItemName(m_ReportApp.SourceGridData.ConditionItemList, OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE);
- string sou = m_ReportApp.m_conditionChoose.m_SourceGridData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE).ToString();
- if (sou.Contains("+"))
- {
- for (int i = 0; i < sou.Split('+').Length; i++)
- {
- OTSIncAReportApp.DataOperation.Model.ResultFile resultFile = m_ReportApp.m_rstDataMgr.ResultFilesList.Find(s => s.FileName == sou.Split('+')[i]);
- if (resultFile != null)
- {
- BindDataGridView(resultFile.FilePath, i, resultFile.FileName);
- }
- }
- }
- else
- {
- BindDataGridView(m_ReportApp.m_rstDataMgr.ResultFilesList[m_ReportApp.m_rstDataMgr.GetWorkingResult()].FilePath, 0, m_ReportApp.m_rstDataMgr.ResultFilesList[m_ReportApp.m_rstDataMgr.GetWorkingResult()].FileName);
- }
- }
- private void Gview_gz_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
- {
- //排序中
- if (((OTSGridView)sender).Rows[e.RowIndex1].Tag != null && ((OTSGridView)sender).Rows[e.RowIndex1].Tag.ToString() == "统计行")
- {
- //ROW1>ROW2
- e.SortResult = 1;
- if (((OTSGridView)sender).SortOrder == SortOrder.Descending)
- e.SortResult = -1;
- e.Handled = true;
- return;
- }
- if (((OTSGridView)sender).Rows[e.RowIndex2].Tag != null && ((OTSGridView)sender).Rows[e.RowIndex2].Tag.ToString() == "统计行")
- {
- //ROW1<ROW2
- e.SortResult = -1;
- if (((OTSGridView)sender).SortOrder == SortOrder.Descending)
- e.SortResult = 1;
- e.Handled = true;
- return;
- }
- }
- private void Gview_gz_Sorted(object sender, EventArgs e)
- {
- //排序完成,重新设置序号
- for (int i = 0; i < ((OTSGridView)sender).Rows.Count; i++)
- {
- if (((OTSGridView)sender).Rows[i].Tag != null && ((OTSGridView)sender).Rows[i].Tag.ToString() == "统计行")
- { }
- else
- {
- ((OTSGridView)sender).Rows[i].Cells[0].Value = i.ToString();
- }
- }
- }
- #endregion
- #region 获取向导出模块提供的DataTable和GridView对象
- /// <summary>
- /// 获取到该模块输出后形成的DataTable,和GridView
- /// </summary>
- /// <param name="out_dt"></param>
- /// <param name="out_dg"></param>
- public void GetDataTableAndGridView(out DataTable out_dt, out DataGridView out_dg)
- {
- out_dt = m_dt;
- out_dg = null;
- foreach (var item in panel1.Controls)
- {
- if (item.GetType().ToString() == "OTSGridView")
- {
- out_dg = ((OTSGridView)item);
- }
- }
- }
- #endregion
- }
- }
|