/*-------------------------------------------------------------------------------------------
* 选举名单窗体
* 创建: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;
}
}
///
/// 记录原候选人ID
///
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();
}
///
/// 加载候选人列表
///
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
}
///
/// 保存候选人列表
///
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;
}
}
///
/// 检测候选人列表
/// 修改:杨斌 2012-05-16
///
///
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;
}
}
}