387 lines
13 KiB
C#
387 lines
13 KiB
C#
using System;
|
||
using System.Data;
|
||
using System.Data.SqlTypes;
|
||
using System.Reflection;
|
||
using System.Windows.Forms;
|
||
using System.Xml;
|
||
|
||
namespace WinFormDataGridViewDemo
|
||
{
|
||
public partial class Form1 : Form
|
||
{
|
||
private static Boolean SaveFlag = true;
|
||
private int diffcount = 0;
|
||
//³õʼ»¯
|
||
public Form1()
|
||
{
|
||
|
||
InitializeComponent();
|
||
|
||
// ³õʼ»¯ DataGridView
|
||
dataGridView1.ColumnCount = 4;
|
||
dataGridView1.Columns[0].Name = "LayoutKey";
|
||
dataGridView1.Columns[1].Name = "LayoutValue";
|
||
dataGridView1.Columns[2].Name = "SecsKey";
|
||
dataGridView1.Columns[3].Name = "SecsValue";
|
||
dataGridView1.Columns[0].ReadOnly = true;
|
||
dataGridView1.Columns[1].ReadOnly = true;
|
||
dataGridView1.Columns[2].ReadOnly = true;
|
||
dataGridView1.Columns[3].ReadOnly = false;
|
||
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
|
||
dataGridView1.AllowUserToAddRows = false;
|
||
LoadDataGrid();
|
||
dataGridView1.Dock = DockStyle.Fill;
|
||
tableLayoutPanel1.Dock = DockStyle.Fill;
|
||
dataGridView1.RowPostPaint += dataGridView1_RowPostPaint;
|
||
dataGridView1.CellFormatting += dataGridView1_CellFormatting;
|
||
dataGridView1.AllowUserToAddRows = false;
|
||
//dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
|
||
|
||
int w = 0;
|
||
foreach (DataGridViewColumn item in dataGridView1.Columns)
|
||
{
|
||
w += item.Width;
|
||
}
|
||
dataGridView1.Width = w;
|
||
|
||
int borderWidth = this.Width - this.ClientSize.Width;
|
||
this.Width = dataGridView1.Width + borderWidth + 200;
|
||
}
|
||
//¼ÓÔØ
|
||
private void LoadDataGrid()
|
||
{
|
||
SaveFlag = true;
|
||
var alramLayout = @"D:\iWISEETEC\ABN12\\AlarmLayout_Eng.xml";
|
||
var alramSecsXml = @"D:\iWISEETEC\WSecsAgent\\SecsPar\\config\\yui.xml";
|
||
if (!string.Empty.Equals(LayoutPath.Text))
|
||
{
|
||
alramLayout = LayoutPath.Text;
|
||
}
|
||
if (!string.Empty.Equals(AlarmPath.Text))
|
||
{
|
||
alramSecsXml = AlarmPath.Text;
|
||
}
|
||
var layoutResult = LoadAlarmLayoutXML(alramLayout);
|
||
var secsResult = LoadAlarmSecsXML(alramSecsXml, out XmlDocument doc);
|
||
|
||
dataGridView1.Rows.Clear();
|
||
|
||
var mergedList = layoutResult.Select(kv => new
|
||
{
|
||
LayoutKey = kv.Key,
|
||
LayoutValue = kv.Value,
|
||
SecsKey = kv.Key,
|
||
SecsValue = secsResult.ContainsKey(kv.Key) ? secsResult[kv.Key] : ""
|
||
}).ToList();
|
||
|
||
foreach (var item in mergedList)
|
||
{
|
||
dataGridView1.Rows.Add(item.LayoutKey, item.LayoutValue, item.SecsKey, item.SecsValue);
|
||
}
|
||
diffcount = 0;
|
||
UpdateDiffCount();
|
||
|
||
}
|
||
|
||
// ¸²¸ÇÑ¡ÖÐÐÐ
|
||
private void btnCoverRow_Click(object sender, EventArgs e)
|
||
{
|
||
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
|
||
{
|
||
if (row.IsNewRow) continue;
|
||
var name = row.Cells[1].Value?.ToString();
|
||
row.Cells[3].Value = name;
|
||
}
|
||
if (dataGridView1.CurrentRow != null)
|
||
{
|
||
var rowIndex = dataGridView1.CurrentRow.Index;
|
||
var code = dataGridView1.Rows[rowIndex].Cells[0].Value?.ToString();
|
||
var name = dataGridView1.Rows[rowIndex].Cells[1].Value?.ToString();
|
||
|
||
dataGridView1.Rows[rowIndex].Cells[3].Value = name;
|
||
SaveFlag = false;
|
||
}
|
||
|
||
UpdateDiffCount();
|
||
}
|
||
|
||
// ¸²¸ÇÈ«²¿ÐÐ
|
||
private async void btnCoverAll_Click(object sender, EventArgs e)
|
||
{
|
||
var rowCount = dataGridView1.Rows.Count;
|
||
Cursor = Cursors.WaitCursor;
|
||
//progressBar1.Minimum = 0;
|
||
panel3.Enabled = false;
|
||
progressBar1.Value = 0;
|
||
progressBar1.Maximum = diffcount;
|
||
progressBar1.Visible = true;
|
||
var index = 0;
|
||
await Task.Run(() =>
|
||
{
|
||
foreach (DataGridViewRow row in dataGridView1.Rows)
|
||
{
|
||
var leftValue = row.Cells[1].Value.ToString();
|
||
if ("" == leftValue)
|
||
{
|
||
continue;
|
||
}
|
||
var rightValue = row.Cells[3].Value.ToString();
|
||
if (leftValue == rightValue)
|
||
{
|
||
continue;
|
||
}
|
||
Invoke(() =>
|
||
{
|
||
index++;
|
||
return row.Cells[3].Value = leftValue;
|
||
});
|
||
|
||
SaveFlag = false;
|
||
|
||
Invoke(() => {
|
||
progressBar1.Value += 1;
|
||
});
|
||
}
|
||
diffcount -= index;
|
||
Invoke(SetDiffCountText);
|
||
});
|
||
progressBar1.Value = 0;
|
||
panel3.Enabled = true;
|
||
this.Cursor = Cursors.Default;
|
||
MessageBox.Show($"ÒѸ²¸Ç {index} ÐÐ,Çëµã»÷¡¾±£´æ¡¿Ð´ÈëXML");
|
||
|
||
}
|
||
|
||
//ÐкÅ
|
||
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
|
||
{
|
||
string rowNumber = (e.RowIndex + 1).ToString();
|
||
|
||
var headerBounds = new Rectangle(
|
||
e.RowBounds.Left,
|
||
e.RowBounds.Top,
|
||
dataGridView1.RowHeadersWidth,
|
||
e.RowBounds.Height);
|
||
|
||
TextRenderer.DrawText(
|
||
e.Graphics,
|
||
rowNumber,
|
||
dataGridView1.Font,
|
||
headerBounds,
|
||
SystemColors.ControlText,
|
||
TextFormatFlags.VerticalCenter | TextFormatFlags.Right
|
||
);
|
||
}
|
||
//²îÒì¸ßÁÁ
|
||
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
|
||
{
|
||
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
|
||
{
|
||
if (dataGridView1.Rows[e.RowIndex].Cells[1].Value?.ToString() != dataGridView1.Rows[e.RowIndex].Cells[3].Value?.ToString())
|
||
{
|
||
//dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(255, 182, 193);
|
||
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.FromArgb(255, 200, 200);
|
||
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
|
||
}
|
||
else
|
||
{
|
||
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = dataGridView1.DefaultCellStyle.BackColor;
|
||
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = dataGridView1.DefaultCellStyle.ForeColor;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
//ͳ¼Æ²îÒì
|
||
private void UpdateDiffCount()
|
||
{
|
||
if (diffcount != 0) diffcount = 0;
|
||
var map = new Dictionary<string, int>();
|
||
foreach (DataGridViewRow row in dataGridView1.Rows)
|
||
{
|
||
if (row.IsNewRow) continue;
|
||
|
||
if (!(row.Cells[1].Value.ToString() == row.Cells[3].Value?.ToString()))
|
||
{
|
||
diffcount++;
|
||
}
|
||
}
|
||
SetDiffCountText();
|
||
}
|
||
//±£´æ
|
||
private void SaveAlarm(object sender, EventArgs e)
|
||
{
|
||
if (SaveFlag)
|
||
{
|
||
MessageBox.Show("δÐÞ¸ÄÎÞÐè±£´æ!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||
return;
|
||
}
|
||
string alramSecsXml = @"D:\iWISEETEC\WSecsAgent\SecsPar\config\yui.xml";
|
||
|
||
if (!File.Exists(alramSecsXml))
|
||
{
|
||
MessageBox.Show("Secs XML Îļþ²»´æÔÚ£¡");
|
||
return;
|
||
}
|
||
|
||
XmlDocument doc = new XmlDocument();
|
||
doc.Load(alramSecsXml);
|
||
|
||
XmlNode root = doc.DocumentElement;
|
||
if (root == null)
|
||
{
|
||
MessageBox.Show("XML ¸ù½Úµã²»´æÔÚ£¡");
|
||
return;
|
||
}
|
||
|
||
XmlNodeList nodes = doc.SelectNodes("//Item");
|
||
Dictionary<int, XmlNode> nodeDict = new Dictionary<int, XmlNode>();
|
||
|
||
if (nodes != null)
|
||
{
|
||
foreach (XmlNode node in nodes)
|
||
{
|
||
if (node.Attributes?["Code"] != null &&
|
||
int.TryParse(node.Attributes["Code"].Value, out int code))
|
||
{
|
||
nodeDict[code] = node;
|
||
}
|
||
}
|
||
}
|
||
|
||
foreach (DataGridViewRow row in dataGridView1.Rows)
|
||
{
|
||
if (row.IsNewRow) continue;
|
||
|
||
if (int.TryParse(row.Cells[2].Value?.ToString(), out int key))
|
||
{
|
||
string newValue = row.Cells[3].Value?.ToString() ?? "";
|
||
|
||
if (nodeDict.ContainsKey(key))
|
||
{
|
||
// ÐÞ¸Ä
|
||
nodeDict[key].Attributes["Name"].Value = newValue;
|
||
//Description¿ÉÄܲ»´æÔÚ
|
||
if (nodeDict[key].Attributes["Description"] != null)
|
||
{
|
||
nodeDict[key].Attributes["Description"].Value = newValue;
|
||
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
// ÐÂÔö
|
||
XmlElement newNode = doc.CreateElement("Item");
|
||
XmlAttribute codeAttr = doc.CreateAttribute("Code");
|
||
XmlAttribute UnitAttr = doc.CreateAttribute("Unit");
|
||
XmlAttribute LevelAttr = doc.CreateAttribute("Level");
|
||
XmlAttribute nameAttr = doc.CreateAttribute("Name");
|
||
XmlAttribute descriptionAttr = doc.CreateAttribute("Description");
|
||
XmlAttribute HowToAttr = doc.CreateAttribute("HowTo");
|
||
|
||
codeAttr.Value = key.ToString();
|
||
UnitAttr.Value = "00";
|
||
LevelAttr.Value = "W";
|
||
nameAttr.Value = newValue;
|
||
descriptionAttr.Value = newValue;
|
||
HowToAttr.Value = "";
|
||
|
||
newNode.Attributes.Append(codeAttr);
|
||
newNode.Attributes.Append(UnitAttr);
|
||
newNode.Attributes.Append(LevelAttr);
|
||
newNode.Attributes.Append(nameAttr);
|
||
newNode.Attributes.Append(descriptionAttr);
|
||
newNode.Attributes.Append(HowToAttr);
|
||
|
||
root.AppendChild(newNode);
|
||
}
|
||
}
|
||
}
|
||
|
||
doc.Save(alramSecsXml);
|
||
MessageBox.Show("±£´æ³É¹¦£¡");
|
||
LoadDataGrid();
|
||
}
|
||
//¼ÓÔØxml
|
||
private Dictionary<int, string> LoadAlarmLayoutXML(string path)
|
||
{
|
||
Dictionary<int, string> result = new Dictionary<int, string>();
|
||
string xmlContent = File.ReadAllText(path);
|
||
XmlDocument doc = new XmlDocument();
|
||
doc.LoadXml(xmlContent);
|
||
|
||
XmlNodeList nodes = doc.SelectNodes("//wAlarmLayout");
|
||
foreach (XmlNode node in nodes)
|
||
{
|
||
int erCode = int.Parse(node["erCode"].InnerText);
|
||
string msg = node["Msg"]?.InnerText ?? "";
|
||
|
||
result[erCode] = msg;
|
||
|
||
}
|
||
return result;
|
||
}
|
||
//¼ÓÔØxml
|
||
private Dictionary<int, string> LoadAlarmSecsXML(string path, out XmlDocument doc)
|
||
{
|
||
var result = new Dictionary<int, string>();
|
||
doc = new XmlDocument();
|
||
|
||
doc.Load(path);
|
||
|
||
XmlNodeList nodes = doc.SelectNodes("//Item");
|
||
if (nodes == null) return result;
|
||
|
||
foreach (XmlNode node in nodes)
|
||
{
|
||
if (node.Attributes == null) continue;
|
||
|
||
string codeStr = node.Attributes["Code"]?.Value;
|
||
string name = node.Attributes["Name"]?.Value ?? "";
|
||
|
||
if (int.TryParse(codeStr, out int code))
|
||
{
|
||
result[code] = name;
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
//ÖØÔØ reloadXml
|
||
private void reloadXml(object sender, EventArgs e)
|
||
{
|
||
LoadDataGrid();
|
||
MessageBox.Show($"ÖØÐ¼ÓÔØ³É¹¦£¡", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||
|
||
}
|
||
//¼ÓÔØLayoutXml·¾¶
|
||
private void OpenLayoutXml_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
|
||
{
|
||
this.LayoutPath.Text = this.openFileDialog1.FileName;
|
||
LoadDataGrid();
|
||
}
|
||
|
||
}
|
||
//¼ÓÔØSecsAlarm·¾¶
|
||
private void SecsAlarm_Click(object sender, EventArgs e)
|
||
{
|
||
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
|
||
{
|
||
this.AlarmPath.Text = this.openFileDialog1.FileName;
|
||
LoadDataGrid();
|
||
}
|
||
}
|
||
//ÉèÖÃlabÄÚÈÝ
|
||
private void SetDiffCountText()
|
||
{
|
||
this.DiffCountLab.Text = $"²îÒìÊýÁ¿:{diffcount}";
|
||
}
|
||
//Êó±êµã»÷ʼþ
|
||
|
||
|
||
}
|
||
}
|