using Newtonsoft.Json; using System; using System.ComponentModel; using System.Data; using System.Linq; using System.Windows.Forms; namespace GeoSigma.UCDraw.WellAndSection { public partial class FrmRockSampleDataEdit : Form { class CRockSample { public string name { get; set; } public string top { get; set; } public string bottom { get; set; } public BindingList samples; } class CSampleData { public string position { get; set; } public string sampleNo { get; set; } public string porosity { get; set; } public string permeability { get; set; } public string saturation { get; set; } public string caco3 { get; set; } } DataGridView mSelectdDataGrid = null; BindingList mRockSampes = new BindingList(); public string mStrDataJson = ""; void RefreshSamplesGrid() { if (this.dataGridRockSample.CurrentRow == null) { this.dataGridSamples.DataSource = null; return; } if (this.dataGridRockSample.CurrentRow.IsNewRow || this.dataGridRockSample.CurrentRow.DataBoundItem == null) { this.dataGridSamples.DataSource = null; return; } CRockSample selectRock = (CRockSample)this.dataGridRockSample.CurrentRow.DataBoundItem; this.dataGridSamples.DataSource = selectRock.samples; dataGridSamples.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); } public FrmRockSampleDataEdit() { InitializeComponent(); this.dataGridRockSample.AutoGenerateColumns = false; this.dataGridRockSample.Columns.AddRange( new DataGridViewTextBoxColumn { DataPropertyName = "name", HeaderText = "名称" }, new DataGridViewTextBoxColumn { DataPropertyName = "top", HeaderText = "顶深" }, new DataGridViewTextBoxColumn { DataPropertyName = "bottom", HeaderText = "底深" } ); this.dataGridSamples.AutoGenerateColumns = false; this.dataGridSamples.Columns.AddRange( new DataGridViewTextBoxColumn { DataPropertyName = "position", HeaderText = "位置" }, new DataGridViewTextBoxColumn { DataPropertyName = "sampleNo", HeaderText = "样品号" }, new DataGridViewTextBoxColumn { DataPropertyName = "porosity", HeaderText = "孔隙度" }, new DataGridViewTextBoxColumn { DataPropertyName = "permeability", HeaderText = "渗透率" }, new DataGridViewTextBoxColumn { DataPropertyName = "saturation", HeaderText = "饱和度" }, new DataGridViewTextBoxColumn { DataPropertyName = "caco3", HeaderText = "碳酸钙" } ); this.dataGridRockSample.SelectionChanged += (s, e) => RefreshSamplesGrid(); //选择主表后刷新从表 } private void btnOK_Click(object sender, EventArgs e) { this.dataGridRockSample.EndEdit(); this.dataGridSamples.EndEdit(); mStrDataJson = JsonConvert.SerializeObject(mRockSampes, Formatting.None); } private void FrmRockSampleDataEdit_Shown(object sender, EventArgs e) { } public void SetRockSampleData(string strJsonData) { mRockSampes = JsonConvert.DeserializeObject>(strJsonData); dataGridRockSample.DataSource = mRockSampes; if (mRockSampes.Count > 0) { dataGridRockSample.Rows[0].Selected = true; } } private void btnAddRockSample_Click(object sender, EventArgs e) { CRockSample newRockSample = new CRockSample(); newRockSample.samples = new BindingList(); mRockSampes.Add(newRockSample); //dataGridSamples.DataSource = newRockSample.samples; } private void btnDelRockSample_Click(object sender, EventArgs e) { if (this.dataGridRockSample.CurrentRow == null || this.dataGridRockSample.CurrentRow.IsNewRow) { return; } this.dataGridRockSample.Rows.Remove(this.dataGridRockSample.CurrentRow); } private void btnAddSample_Click(object sender, EventArgs e) { try { if (dataGridRockSample.CurrentRow == null || dataGridRockSample.CurrentRow.IsNewRow) { return; } var rockSample = dataGridRockSample.CurrentRow.DataBoundItem as CRockSample; if (rockSample != null) { CSampleData tsample = new CSampleData(); rockSample.samples.Add(tsample); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void btnDelSample_Click(object sender, EventArgs e) { if (this.dataGridSamples.CurrentRow == null || this.dataGridSamples.CurrentRow.IsNewRow) { return; } this.dataGridSamples.Rows.Remove(this.dataGridSamples.CurrentRow); } private void btnCancel_Click(object sender, EventArgs e) { } private void dataGridRockSample_SelectionChanged(object sender, EventArgs e) { if (dataGridRockSample.SelectedRows.Count > 0 && dataGridRockSample.SelectedRows[0].DataBoundItem != null) { var rockSample = dataGridRockSample.SelectedRows[0].DataBoundItem as CRockSample; if (rockSample != null) { dataGridSamples.DataSource = rockSample.samples; } } } private void CopyMenuItem_Click(object sender, EventArgs e) { if (this.mSelectdDataGrid.SelectedRows == null || mSelectdDataGrid.SelectedRows.Count == 0) { MessageBox.Show("请先选择数据行"); return; } try { Clipboard.SetDataObject(mSelectdDataGrid.GetClipboardContent()); } catch (Exception ex) { } } public int GetMaxSelectedRowIndex(DataGridView dgv) { // 如果没有选中任何行或单元格 if (dgv.SelectedRows.Count == 0 && dgv.SelectedCells.Count == 0) { return -1; // 返回-1表示未选中 } // 获取选中行和选中单元格中的最大行号 int maxFromRows = dgv.SelectedRows.Count > 0 ? dgv.SelectedRows.Cast().Max(r => r.Index) : -1; int maxFromCells = dgv.SelectedCells.Count > 0 ? dgv.SelectedCells.Cast().Max(c => c.RowIndex) : -1; // 返回两者中的较大值 return Math.Max(maxFromRows, maxFromCells); } private void PasteMenuItem_Click(object sender, EventArgs e) { try { string clipText = Clipboard.GetText(); if (string.IsNullOrEmpty(clipText)) return; string[] rows = clipText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (mSelectdDataGrid == this.dataGridRockSample) { int maxselId = GetMaxSelectedRowIndex(this.dataGridRockSample); for (int i = 0; i < rows.Length; i++) { string tstr = rows[i]; tstr = tstr.TrimStart(); string[] values = tstr.Split(new[] { "\t", " ", ",", ";" }, StringSplitOptions.None); int rcol = 3; if (rcol > values.Length) rcol = values.Length; CRockSample newRockSample = new CRockSample(); newRockSample.samples = new BindingList(); if (rcol > 0) newRockSample.name = values[0]; if (rcol > 1) newRockSample.top = values[1]; if (rcol > 2) newRockSample.bottom = values[2]; //mRockSampes.Add(newRockSample); mRockSampes.Insert(maxselId, newRockSample); } } else { int maxselId = GetMaxSelectedRowIndex(this.dataGridSamples); if (dataGridRockSample.CurrentRow == null || dataGridRockSample.CurrentRow.IsNewRow) { return; } if (maxselId < 0) { MessageBox.Show("选择样品表中插入位置"); return; } var rockSample = dataGridRockSample.CurrentRow.DataBoundItem as CRockSample; if (rockSample == null) return; for (int i = 0; i < rows.Length; i++) { string tstr = rows[i]; tstr = tstr.TrimStart(); string[] values = tstr.Split(new[] { "\t", " ", ",", ";" }, StringSplitOptions.None); int rcol = 6; if (rcol > values.Length) rcol = values.Length; CSampleData tsample = new CSampleData(); if (rcol > 0) tsample.position = values[0]; if (rcol > 1) tsample.sampleNo = values[1]; if (rcol > 2) tsample.permeability = values[2]; if (rcol > 3) tsample.saturation = values[3]; if (rcol > 4) tsample.porosity = values[4]; if (rcol > 5) tsample.caco3 = values[5]; rockSample.samples.Insert(maxselId, tsample); } dataGridSamples.DataSource = rockSample.samples; } } catch (Exception ex) { } } private void DelMenuItem_Click(object sender, EventArgs e) { if (mSelectdDataGrid == this.dataGridRockSample) { btnDelRockSample_Click(sender, e); } else { btnDelSample_Click(sender, e); } } private void ClearMenuItem_Click(object sender, EventArgs e) { if (mSelectdDataGrid == this.dataGridRockSample) { this.mRockSampes.Clear(); this.dataGridSamples.DataSource = null; } else { this.dataGridSamples.Rows.Clear(); } } private void dataGridRockSample_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.mSelectdDataGrid = this.dataGridRockSample; this.dataGridRockSample.ContextMenuStrip = this.ctmRockSampleDlg; } } private void dataGridSamples_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.mSelectdDataGrid = this.dataGridSamples; this.dataGridSamples.ContextMenuStrip = this.ctmRockSampleDlg; } } } }