You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
11 KiB
C#

1 month ago
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace GeoSigma.UCDraw.WellAndSection
{
public partial class FrmWellDataEdit : CResizeForm
{
public string[] mEnColNames; //英文列名称
public string[] mColsName;
public DataTable mDatas;
public DataTable originDatas;
public string strJsonData = "";
public bool m_IsModified = false;
public FrmWellDataEdit()
{
InitializeComponent();
_designSize = new Size(this.Size.Width, this.Size.Height);
StoreOriginalStates(this.Controls);
mColsName = new string[0];
mDatas = new DataTable();
WellDataGridView.EnableHeadersVisualStyles = false;
WellDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.AliceBlue;
WellDataGridView.RowHeadersDefaultCellStyle.BackColor = Color.Azure;
WellDataGridView.ContextMenuStrip = this.ctmMenuWellTrackData;
}
void CurrentDataTable_DeleteRow(object sender, DataRowChangeEventArgs e)
{
m_IsModified = true;
}
void CurrentDataTable_RowChanged(object sender, DataRowChangeEventArgs e)
{
if (e.Action == DataRowAction.Change)
{
m_IsModified = true;
}
}
void CurrentDataTable_RowModified(object sender, DataRowChangeEventArgs e)
{
m_IsModified = true;
}
void CurrentDataTable_TableNewRow(object sender, DataTableNewRowEventArgs e)
{
m_IsModified = true;
}
public void SetData(string[] colNames, string[] colNamesCH, string[,] datas)
{
mDatas.Clear();
mEnColNames = new string[colNames.Length];
for (int i = 0; i < colNames.Length; i++)
{
mEnColNames[i] = colNames[i];
}
for (int i = 0; i < colNamesCH.Length; i++)
{
DataColumn col = new DataColumn(colNamesCH[i], typeof(string));
mDatas.Columns.Add(col);
}
for (int i = 0; i < datas.GetLength(0); i++)
{
DataRow dr = mDatas.NewRow();
for (int j = 0; j < datas.GetLength(1); j++)
{
dr[j] = datas[i, j];
}
mDatas.Rows.Add(dr);
}
mDatas.RowChanged += CurrentDataTable_RowChanged;
mDatas.RowDeleted += CurrentDataTable_RowModified;
mDatas.TableNewRow += CurrentDataTable_TableNewRow;
mDatas.RowDeleted += CurrentDataTable_DeleteRow;
originDatas = mDatas;
}
private void FrmWellDataEdit_Shown(object sender, EventArgs e)
{
WellDataGridView.DataSource = mDatas.DefaultView;
}
private void FrmWellDataEdit_Load(object sender, EventArgs e)
{
}
private void FrmWellDataEdit_FormClosed(object sender, FormClosedEventArgs e)
{
//DataTable changes = mDatas.GetChanges();
//if(changes != null)
//{
// //foreach( DataRow row in changes.Rows)
// //{
// // if(row.RowState == DataRowState.Deleted)
// // {
// // m_IsModified = true;
// // }
// // else if( row.RowState == DataRowState.Modified)
// // {
// // m_IsModified = true;
// // }
// // else if( row.RowState == DataRowState.Added)
// // {
// // m_IsModified = true;
// // }
// //}
//}
}
private void FrmWellDataEdit_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void BtnAddRow_Click(object sender, EventArgs e)
{
DataRow newRow = mDatas.NewRow();
mDatas.Rows.Add(newRow);
WellDataGridView.Refresh();
int lastRow = WellDataGridView.RowCount - 1;
WellDataGridView.FirstDisplayedScrollingRowIndex = lastRow;
}
private void BtnDelRow_Click(object sender, EventArgs e)
{
if (WellDataGridView.SelectedRows == null || WellDataGridView.SelectedRows.Count == 0)
{
MessageBox.Show("请先选择需要删除的数据行");
}
else
{
List<DataRow> rowsToDelete = new List<DataRow>();
foreach (DataGridViewRow row in WellDataGridView.SelectedRows)
{
if (row.IsNewRow)
continue;
DataRowView rowView = (DataRowView)row.DataBoundItem;
rowsToDelete.Add(rowView.Row);
}
foreach (DataRow row in rowsToDelete)
{
mDatas.Rows.Remove(row);
}
WellDataGridView.Refresh();
// m_IsModified = true;
}
}
private void RemoveEmptyRows(DataTable dataTable)
{
List<DataRow> rowsToRemove = new List<DataRow>();
foreach (DataRow row in dataTable.Rows)
{
bool isEmpty = true;
foreach (DataColumn col in dataTable.Columns)
{
// 跳过自动生成的列(例如表达式列或自增列)可选
// if (col.AutoIncrement || col.Expression != null) continue;
object value = row[col];
if (value != null && value != DBNull.Value && !string.IsNullOrWhiteSpace(value.ToString()))
{
isEmpty = false;
break;
}
}
if (isEmpty)
{
rowsToRemove.Add(row);
}
}
// 从 DataTable 中移除空行(必须在循环外删除,防止枚举修改异常)
foreach (DataRow row in rowsToRemove)
{
row.Delete(); // 使用 Delete 而不是 Remove以正确维护状态尤其用于DataAdapter更新
}
// 提交更改(可选,但建议)
dataTable.AcceptChanges();
}
private void BtnOk_Click(object sender, EventArgs e)
{
RemoveEmptyRows(mDatas);
JObject tjson = new JObject();
JArray jsonColName = new JArray();
for (int ii = 0; ii < mEnColNames.Length; ii++)
{
jsonColName.Add(mEnColNames[ii]);
}
tjson["columnName"] = jsonColName;
JArray jsonData = new JArray();
for (int ii = 0; ii < mDatas.Rows.Count; ii++)
{
DataRow rw = mDatas.Rows[ii];
JArray jRow = new JArray();
for (int jj = 0; jj < mEnColNames.Length; jj++)
{
jRow.Add(rw[jj].ToString());
}
jsonData.Add(jRow);
}
tjson["data"] = jsonData;
strJsonData = JsonConvert.SerializeObject(tjson);
}
private void PasteMenuItemWellDataDlg_Click(object sender, EventArgs e)
{
try
{
string clipText = Clipboard.GetText();
if (string.IsNullOrEmpty(clipText))
return;
int maxselId = GetMaxSelectedRowIndex(WellDataGridView);
if (maxselId == -1)
maxselId = 0;
string[] rows = clipText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
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 = mDatas.Columns.Count;
if (rcol > values.Length)
rcol = values.Length;
DataRow newRow = mDatas.NewRow();
for (int j = 0; j < rcol; j++)
newRow[j] = values[j];
mDatas.Rows.InsertAt(newRow, maxselId);
maxselId++;
}
m_IsModified = true;
WellDataGridView.Refresh();
}
catch (Exception ex)
{
MessageBox.Show($"粘贴失败:{ex.Message}");
}
}
private void CopyMenuItemWellDataDlg_Click(object sender, EventArgs e)
{
if (WellDataGridView.SelectedRows == null || WellDataGridView.SelectedRows.Count == 0)
{
MessageBox.Show("请先选择数据行");
return;
}
try
{
Clipboard.SetDataObject(WellDataGridView.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<DataGridViewRow>().Max(r => r.Index) : -1;
int maxFromCells = dgv.SelectedCells.Count > 0 ?
dgv.SelectedCells.Cast<DataGridViewCell>().Max(c => c.RowIndex) : -1;
// 返回两者中的较大值
return Math.Max(maxFromRows, maxFromCells);
}
private void DelMenuItemWellDataDlg_Click(object sender, EventArgs e)
{
if (WellDataGridView.SelectedRows == null || WellDataGridView.SelectedRows.Count == 0)
{
MessageBox.Show("请先选择需要删除的数据行");
}
else
{
List<DataRow> rowsToDelete = new List<DataRow>();
foreach (DataGridViewRow row in WellDataGridView.SelectedRows)
{
if (row.IsNewRow)
continue;
DataRowView rowView = (DataRowView)row.DataBoundItem;
rowsToDelete.Add(rowView.Row);
}
foreach (DataRow row in rowsToDelete)
{
mDatas.Rows.Remove(row);
}
WellDataGridView.Refresh();
// m_IsModified = true;
}
}
private void ClearMenuItemWellDataDlg_Click(object sender, EventArgs e)
{
mDatas.Clear();
m_IsModified = true;
this.WellDataGridView.Refresh();
}
private void WellDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
}
}
private void FrmWellDataEdit_Resize(object sender, EventArgs e)
{
FrmResize(sender, e);
}
}
}