private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (dgvDetail.Rows.Count >= 1) { if (e.ColumnIndex == dgvDetail.Columns.Count - 1) { if (dgvDetail.Rows[e.RowIndex].Tag != null) { ExtFileList task = (ExtFileList)dgvDetail.Rows[e.RowIndex].Tag; if (MessageBox.Show("真的要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { //先保存当前的 if (dgvDetail.Rows.Count >= 2) { SaveOnly(); } try { File.Delete(Path.Combine(PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().filesDir, task.RealFileName)); } catch (Exception ex) { } ConnectionManager.Context.table("ExtFileList").where ("ID='" + task.ID + "'").delete(); RefreshView(); } } else { if (e.ColumnIndex == dgvDetail.Columns.Count - 1) { if (MessageBox.Show("真的要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { dgvDetail.Rows.RemoveAt(e.RowIndex); } catch (Exception ex) { RefreshView(); } } } } } else if (e.ColumnIndex == dgvDetail.Columns.Count - 2) { if (ofdUpload.ShowDialog() == DialogResult.OK) { dgvDetail.Rows[e.RowIndex].Cells[2].Tag = ofdUpload.FileName; dgvDetail.Rows[e.RowIndex].Cells[2].Value = Path.GetFileName(ofdUpload.FileName); } } else if (e.ColumnIndex == 2) { if (dgvDetail.Rows[e.RowIndex].Tag != null) { ExtFileList task = (ExtFileList)dgvDetail.Rows[e.RowIndex].Tag; if (string.IsNullOrEmpty(task.RealFileName)) { return; } else { try { Process.Start(Path.Combine(PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().filesDir, task.RealFileName)); } catch (Exception ex) { } } } } } }
public override void OnSaveEvent() { base.OnSaveEvent(); foreach (DataGridViewRow dgvRow in dgvDetail.Rows) { if (dgvRow.Cells[1].Value == null || string.IsNullOrEmpty(dgvRow.Cells[1].Value.ToString())) { MessageBox.Show("对不起,请输入单位名称!"); break;; } if (dgvRow.Tag != null) { //修改 ExtFileList efl = (ExtFileList)dgvRow.Tag; //检查是否修改过 if (dgvRow.Cells[2].Tag != null) { string sourceFile = dgvRow.Cells[2].Tag.ToString(); string realFileName = DateTime.Now.Ticks + "___" + Path.GetFileName(sourceFile); string destFile = Path.Combine(PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().filesDir, realFileName); if (File.Exists(sourceFile)) { //旧地址 string oldFile = Path.Combine(PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().filesDir, efl.RealFileName); try { File.Delete(oldFile); } catch (Exception ex) { } //复制新地址 File.Copy(sourceFile, destFile, true); //源文件 efl.SourceFileName = Path.GetFileName(sourceFile); //真实文件名称 efl.RealFileName = realFileName; //是否为军队单位 efl.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; //单位名称 efl.ExtName = dgvRow.Cells[1].Value.ToString(); //更新数据 efl.copyTo(ConnectionManager.Context.table("ExtFileList")).where ("ID='" + efl.ID + "'").update(); } else { //是否为军队单位 efl.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; //单位名称 efl.ExtName = dgvRow.Cells[1].Value.ToString(); //更新数据 efl.copyTo(ConnectionManager.Context.table("ExtFileList")).where ("ID='" + efl.ID + "'").update(); } } else { //是否为军队单位 efl.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; //单位名称 efl.ExtName = dgvRow.Cells[1].Value.ToString(); //源文件名 efl.SourceFileName = string.Empty; //真实文件名 efl.RealFileName = string.Empty; //更新数据 efl.copyTo(ConnectionManager.Context.table("ExtFileList")).where ("ID='" + efl.ID + "'").update(); } } else { //增加 if (dgvRow.Cells[2].Tag != null) { string sourceFile = dgvRow.Cells[2].Tag.ToString(); string realFileName = DateTime.Now.Ticks + "___" + Path.GetFileName(sourceFile); string destFile = Path.Combine(PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().filesDir, realFileName); if (File.Exists(sourceFile)) { //复制新地址 File.Copy(sourceFile, destFile, true); ExtFileList efll = new ExtFileList(); efll.ID = Guid.NewGuid().ToString(); efll.ProjectID = PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().projectObj.ID; efll.ExtName = dgvRow.Cells[1].Value.ToString(); efll.SourceFileName = Path.GetFileName(sourceFile); efll.RealFileName = realFileName; efll.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; efll.copyTo(ConnectionManager.Context.table("ExtFileList")).insert(); } } else { ExtFileList efll = new ExtFileList(); efll.ID = Guid.NewGuid().ToString(); efll.ProjectID = PublicReporterLib.PluginLoader.getLocalPluginRoot <ProjectReporterPlugin.PluginRoot>().projectObj.ID; efll.ExtName = dgvRow.Cells[1].Value.ToString(); efll.SourceFileName = string.Empty; efll.RealFileName = string.Empty; efll.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; efll.copyTo(ConnectionManager.Context.table("ExtFileList")).insert(); } } } //刷新数据 RefreshView(); }
private void SaveOnly() { foreach (DataGridViewRow dgvRow in dgvDetail.Rows) { if (dgvRow.Cells[1].Value == null) { dgvRow.Cells[1].Value = string.Empty; } if (dgvRow.Tag != null) { //修改 ExtFileList efl = (ExtFileList)dgvRow.Tag; //检查是否修改过 if (dgvRow.Cells[2].Tag != null) { string sourceFile = dgvRow.Cells[2].Tag.ToString(); string realFileName = DateTime.Now.Ticks + "___" + Path.GetFileName(sourceFile); string destFile = Path.Combine(MainForm.ProjectFilesDir, realFileName); if (File.Exists(sourceFile)) { //旧地址 string oldFile = Path.Combine(MainForm.ProjectFilesDir, efl.RealFileName); try { File.Delete(oldFile); } catch (Exception ex) { } //复制新地址 File.Copy(sourceFile, destFile, true); //源文件 efl.SourceFileName = Path.GetFileName(sourceFile); //真实文件名称 efl.RealFileName = realFileName; //是否为军队单位 efl.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; //单位名称 efl.ExtName = dgvRow.Cells[1].Value.ToString(); //更新数据 efl.copyTo(ConnectionManager.Context.table("ExtFileList")).where ("ID='" + efl.ID + "'").update(); } else { //是否为军队单位 efl.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; //单位名称 efl.ExtName = dgvRow.Cells[1].Value.ToString(); //更新数据 efl.copyTo(ConnectionManager.Context.table("ExtFileList")).where ("ID='" + efl.ID + "'").update(); } } else { //是否为军队单位 efl.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; //单位名称 efl.ExtName = dgvRow.Cells[1].Value.ToString(); //源文件名 efl.SourceFileName = string.Empty; //真实文件名 efl.RealFileName = string.Empty; //更新数据 efl.copyTo(ConnectionManager.Context.table("ExtFileList")).where ("ID='" + efl.ID + "'").update(); } } else { //增加 if (dgvRow.Cells[2].Tag != null) { string sourceFile = dgvRow.Cells[2].Tag.ToString(); string realFileName = DateTime.Now.Ticks + "___" + Path.GetFileName(sourceFile); string destFile = Path.Combine(MainForm.ProjectFilesDir, realFileName); if (File.Exists(sourceFile)) { //复制新地址 File.Copy(sourceFile, destFile, true); ExtFileList efll = new ExtFileList(); efll.ID = Guid.NewGuid().ToString(); efll.ProjectID = MainForm.Instance.ProjectObj.ID; efll.ExtName = dgvRow.Cells[1].Value.ToString(); efll.SourceFileName = Path.GetFileName(sourceFile); efll.RealFileName = realFileName; efll.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; efll.copyTo(ConnectionManager.Context.table("ExtFileList")).insert(); } } else { ExtFileList efll = new ExtFileList(); efll.ID = Guid.NewGuid().ToString(); efll.ProjectID = MainForm.Instance.ProjectObj.ID; efll.ExtName = dgvRow.Cells[1].Value.ToString(); efll.SourceFileName = string.Empty; efll.RealFileName = string.Empty; efll.IsIgnore = ((bool)dgvRow.Cells[3].Value) ? 1 : 0; efll.copyTo(ConnectionManager.Context.table("ExtFileList")).insert(); } } } }