添加项目文件。
This commit is contained in:
386
WinFormsApp1/Form1.cs
Normal file
386
WinFormsApp1/Form1.cs
Normal file
@@ -0,0 +1,386 @@
|
||||
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;
|
||||
//<2F><>ʼ<EFBFBD><CABC>
|
||||
public Form1()
|
||||
{
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
// <20><>ʼ<EFBFBD><CABC> 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;
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>
|
||||
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();
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
|
||||
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($"<22>Ѹ<EFBFBD><D1B8><EFBFBD> {index} <20><>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>桿д<E6A1BF><D0B4>XML");
|
||||
|
||||
}
|
||||
|
||||
//<2F>к<EFBFBD>
|
||||
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
|
||||
);
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
//ͳ<>Ʋ<EFBFBD><C6B2><EFBFBD>
|
||||
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();
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>
|
||||
private void SaveAlarm(object sender, EventArgs e)
|
||||
{
|
||||
if (SaveFlag)
|
||||
{
|
||||
MessageBox.Show("δ<><EFBFBD><DEB8><EFBFBD><EFBFBD>豣<EFBFBD><E8B1A3>!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return;
|
||||
}
|
||||
string alramSecsXml = @"D:\iWISEETEC\WSecsAgent\SecsPar\config\yui.xml";
|
||||
|
||||
if (!File.Exists(alramSecsXml))
|
||||
{
|
||||
MessageBox.Show("Secs XML <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>");
|
||||
return;
|
||||
}
|
||||
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(alramSecsXml);
|
||||
|
||||
XmlNode root = doc.DocumentElement;
|
||||
if (root == null)
|
||||
{
|
||||
MessageBox.Show("XML <20><><EFBFBD>ڵ㲻<DAB5><E3B2BB><EFBFBD>ڣ<EFBFBD>");
|
||||
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))
|
||||
{
|
||||
// <20><EFBFBD>
|
||||
nodeDict[key].Attributes["Name"].Value = newValue;
|
||||
//Description<6F><6E><EFBFBD>ܲ<EFBFBD><DCB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (nodeDict[key].Attributes["Description"] != null)
|
||||
{
|
||||
nodeDict[key].Attributes["Description"].Value = newValue;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
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("<22><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>");
|
||||
LoadDataGrid();
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>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;
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>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;
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD> reloadXml
|
||||
private void reloadXml(object sender, EventArgs e)
|
||||
{
|
||||
LoadDataGrid();
|
||||
MessageBox.Show($"<22><><EFBFBD>¼<EFBFBD><C2BC>سɹ<D8B3><C9B9><EFBFBD>", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>LayoutXml·<6C><C2B7>
|
||||
private void OpenLayoutXml_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
this.LayoutPath.Text = this.openFileDialog1.FileName;
|
||||
LoadDataGrid();
|
||||
}
|
||||
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>SecsAlarm·<6D><C2B7>
|
||||
private void SecsAlarm_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
this.AlarmPath.Text = this.openFileDialog1.FileName;
|
||||
LoadDataGrid();
|
||||
}
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>lab<61><62><EFBFBD><EFBFBD>
|
||||
private void SetDiffCountText()
|
||||
{
|
||||
this.DiffCountLab.Text = $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:{diffcount}";
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user