FrmPollList.cs
14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*-------------------------------------------------------------------------------------------
* 选举名单窗体
* 创建:2011-11-30
* 修改:杨斌 2012-03-01 Esc退出(设置窗体CancelButton),其他窗体类似
* ----------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralLib;
using System.IO;
namespace SunVoteARSPPT
{
public partial class FrmPollList : Form
{
TagSet mTagSet = null;
public TagSet TagSet
{
get { return mTagSet; }
set
{
mTagSet = value;
}
}
/// <summary>
/// 记录原候选人ID
/// </summary>
private string oldCandidateID = "";
private bool SaveOK = true;
bool CanEdit = true;
public FrmPollList(TagSet tagSet, bool canEdit)
{
InitializeComponent();
TagSet = tagSet;
CanEdit = canEdit;
btnImport.Enabled = CanEdit;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (!CheckPollList()) { return; }
SavePollList();
SaveOK = true;
//MessageBox.Show("保存成功", "ARS2011");
}
private void FrmPollList_Load(object sender, EventArgs e)
{
GlobalInfo.SysLanguage.SetLanguage(this.Name, this);
lblExportSuccess.Text = "";
LoadPollList();
}
/// <summary>
/// 加载候选人列表
/// </summary>
private void LoadPollList()
{
string CandidateID = "";
string CandidateName = "";
int PollCount = TagSet.LoadValue(TagKey.Poll_CandidatesCount, 0).ToInt;
if (PollCount > 0)
dgvVoterList.RowCount = PollCount;
else
dgvVoterList.RowCount = 0;//杨斌 2012-03-06
for (int i = 0; i < PollCount; i++)
{
CandidateID = TagSet.LoadValue(TagKey.Poll_CandidatesID_, i, "").Value.ToString();
CandidateName = TagSet.LoadValue(TagKey.Poll_CandidatesName_, i, "").Value.ToString();
dgvVoterList.Rows[i].Cells["colPollID"].Value = CandidateID;
dgvVoterList.Rows[i].Cells["colPollName"].Value = CandidateName;
}
ControlOper.SetGridRowH(dgvVoterList);//杨斌 2016-03-04
}
/// <summary>
/// 保存候选人列表
/// </summary>
private void SavePollList()
{
string CandidateID = "";
string CandidateName = "";
int PollCount = 0;
for (int i = 0; i < dgvVoterList.Rows.Count; i++)
{
if (dgvVoterList.Rows[i].Cells["colPollID"].Value == null) { continue; }
CandidateID = dgvVoterList.Rows[i].Cells["colPollID"].Value.ToString().Trim();
CandidateName = (dgvVoterList.Rows[i].Cells["colPollName"].Value != null) ? dgvVoterList.Rows[i].Cells["colPollName"].Value.ToString().Trim() : "";
TagSet.SetValue(TagKey.Poll_CandidatesID_, i, CandidateID);
TagSet.SetValue(TagKey.Poll_CandidatesName_, i, CandidateName);
PollCount += 1;
}
TagSet.SetValue(TagKey.Poll_CandidatesCount, PollCount);
//MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name,"SaveSuccess" ,"保存成功"), "ARS2011");
}
private void FrmPollList_FormClosed(object sender, FormClosedEventArgs e)
{
if (!SaveOK)
{
//if (MessageBox.Show("候选名单未保存,是否保存?", "ARS2011", MessageBoxButtons.YesNo,
// MessageBoxIcon.Question) == DialogResult.Yes)
//{
//SavePollList();
//}
}
}
private void dgvVoterList_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.RowIndex >= 0)
{
if (dgvVoterList.Columns[e.ColumnIndex].Name == "colPollID")
{
oldCandidateID = (dgvVoterList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) ? dgvVoterList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() : "";
}
}
}
private void dgvVoterList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//string newCandidateID = "";
//string candidateID="";
//if (e.RowIndex >= 0)
//{
// if (dgvVoterList.Columns[e.ColumnIndex].Name == "colPollID")
// {
// newCandidateID = (dgvVoterList.Rows[e.RowIndex].Cells["colPollID"].Value != null) ? dgvVoterList.Rows[e.RowIndex].Cells["colPollID"].Value.ToString() : "";
// for (int i = 0; i < dgvVoterList.Rows.Count; i++)
// {
// //已存在的候选人编号
// candidateID = (dgvVoterList.Rows[i].Cells["colPollID"].Value != null) ? dgvVoterList.Rows[i].Cells["colPollID"].Value.ToString() : "";
// if ((i != e.RowIndex) && (newCandidateID ==candidateID))
// {
// dgvVoterList.Rows[e.RowIndex].Cells["colPollID"].Value =oldCandidateID;
// MessageBox.Show("该编号已经存在","ARS2011");
// break;
// }
// }
// }
//}
SaveOK = false;
}
private void btnImport_Click(object sender, EventArgs e)
{
//ExcelOper exl = new ExcelOper();
lblExportSuccess.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "ImportData", "数据导入");
string fileName = "";
string[,] PollList = null;
//数据开始行
int StarRowIndex = 1;
//候选人编号列
int IDColIndex = 0;
//候选人名称列
int NameColIndex = 1;
//杨斌 2014-10-21
string path = new DirectoryInfo(GlobalInfo.GetAppWorkDir()).Parent.FullName;
path += @"\Resources\PollList\";
openFileDialog.InitialDirectory = path;
//openFileDialog.Filter = "Execl files (*.xls)|*.xls|Excel files (*.xlsx)|*.xlsx";
//openFileDialog.Filter = "Excel(*.xls;*.xlsx)|*.xls;*.xlsx";//杨斌 2012-03-05
//openFileDialog.Filter = "Excel(*.xls;*.xlsx)|*.xls;*.xlsx|CSV(*.csv)|*.csv";//杨斌 2017-10-30
openFileDialog.Filter = "Excel(*.xlsx)|*.xlsx|CSV(*.csv)|*.csv";//杨斌 2018-12-18。不兼容2003格式
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog.FileName;
bool isCSV = (Path.GetExtension(fileName).ToLower() == ".csv");//杨斌 2018-05-14
if (isCSV)
{
PollList = ExcelOper.ImportCSV(fileName);
}
else
{
ExcelOper exl = new ExcelOper();
exl.OpenExcel(fileName);
PollList = exl.ImportToSheetBySheetNum(1);//杨斌 2018-05-14
exl.CloseExcel();
}
if (PollList != null)
{
int iRowCount = PollList.GetLength(0);
int iColCount = PollList.GetLength(1);
dgvVoterList.RowCount = iRowCount - 1;
for (int i = 0; i < iRowCount - 1; i++)
{
dgvVoterList.Rows[i].Cells[0].Value = PollList[i + StarRowIndex, IDColIndex];
dgvVoterList.Rows[i].Cells[1].Value = PollList[i + StarRowIndex, NameColIndex];
}
ControlOper.SetGridRowH(dgvVoterList);//杨斌 2016-03-04
}
}
else
{
//取消操作不是失败。杨斌 2012-02-29,另外窗体界面控件停靠属性和位置改了
//lblExportSuccess.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "ImportFail", "导入失败");
return;
}
if (!CheckPollList())
{
dgvVoterList.Rows.Clear();
lblExportSuccess.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "ImportFail", "导入失败");
return;
}
SavePollList();
SaveOK = true;
lblExportSuccess.Text = GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "ImportSuccess", "导入成功");
//new FrmImportCandidate(dgvVoterList).ShowDialog();
}
private void btnInsert_Click(object sender, EventArgs e)
{
dgvVoterList.Rows.Insert(dgvVoterList.CurrentRow.Index, 1);
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (dgvVoterList.CurrentRow.Index != dgvVoterList.Rows.Count - 1)
{
dgvVoterList.Rows.RemoveAt(dgvVoterList.CurrentRow.Index);
SaveOK = false;
}
}
/// <summary>
/// 检测候选人列表
/// 修改:杨斌 2012-05-16
/// </summary>
/// <returns></returns>
private bool CheckPollList()
{
bool bValue = true;
string newCandidateID = "";
string candidateID = "";
int CandidateCount = TagSet.LoadValue(TagKey.Poll_CandidatesCount, 0).ToInt;
//修改标志 赵丽 2012-06-06 导入的候选人为空时,提示失败
if ((dgvVoterList.Rows.Count == 0) && (CandidateCount == 0)) { return false; }
if (dgvVoterList.Rows.Count == 0) { TagSet.SetValue(TagKey.Poll_CandidatesCount, 0); return bValue; }
dgvVoterList.CurrentCell.Selected = false;
for (int i = 0; i < dgvVoterList.Rows.Count - 1; i++)
{
newCandidateID = (dgvVoterList.Rows[i].Cells["colPollID"].Value != null) ? dgvVoterList.Rows[i].Cells["colPollID"].Value.ToString() : "";
if (!PublicFunction.IsMatchWith(newCandidateID, @"^\d+$"))
{
MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "CheckMessage1", "候选人编号只允许输入数字,,请修改候选名单列表"), GlobalInfo.GetAppName());
bValue = false;
dgvVoterList.Rows[i].Cells["colPollID"].Selected = true;
break;
}
if (newCandidateID == "")
{
MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "CheckMessage2", "候选人编号不能为空,,请修改候选名单列表"), GlobalInfo.GetAppName());
bValue = false;
dgvVoterList.Rows[i].Cells["colPollID"].Selected = true;
break;
}
for (int j = i + 1; j < dgvVoterList.Rows.Count; j++)
{
candidateID = (dgvVoterList.Rows[j].Cells["colPollID"].Value != null) ? dgvVoterList.Rows[j].Cells["colPollID"].Value.ToString() : "";
if (newCandidateID == candidateID)
{
dgvVoterList.Rows[j].Cells["colPollID"].Selected = true;
MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "CheckMessage3", "候选人编号有重复,,请修改候选名单列表"), GlobalInfo.GetAppName()); bValue = false;
break;
}
}
}
return bValue;
}
private void FrmPollList_FormClosing(object sender, FormClosingEventArgs e)
{
//有未保存的数据
//int CandidateCount = TagSet.LoadValue(TagKey.Poll_CandidatesCount, 0).ToInt;
//if ((dgvVoterList.Rows.Count == 1) && (CandidateCount==0)) { return; }
//if (!SaveOK)
//{
// if (MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name,"SavePollList", "候选名单未保存,是否保存?"), "ARS2011", MessageBoxButtons.YesNo,
// MessageBoxIcon.Question) == DialogResult.Yes)
// {
// if (dgvVoterList.Rows.Count == 1) { TagSet.SetValue(TagKey.Poll_CandidatesCount, 0); }
// if (!CheckPollList())
// e.Cancel = true;
// }
// else
// SaveOK = true;
//}
}
private void btnExport_Click(object sender, EventArgs e)
{
//openFileDialog.InitialDirectory = "C:\\";
//openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
//openFileDialog.RestoreDirectory = true;
//openFileDialog.FilterIndex = 1;
//if (openFileDialog.ShowDialog() == DialogResult.OK)
//{
// string fName = openFileDialog.FileName;
//}
//if (PublicFunction.ExportToExcel(dgvVoterList, GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "ExcelTitle", "候选人名单")))
// MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name,"ExportSucceed", "导出成功"), "SunVote ARS PPT 2012");
// // lblExportSuccess.Visible = true;
this.Cursor = Cursors.WaitCursor;
string[,] aryTable = ControlOper.AryFromDataGrid(dgvVoterList);
ExcelOper.ExportArrayToExcel(aryTable, GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "ExcelTitle", "候选人名单"));
this.Cursor = Cursors.Default;
}
}
}