⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridform.cs

📁 《C#和.NET第一步》中的财务系统 利用三层结构做的
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using System.IO;
namespace HomeMoney
{
	public partial class GridForm : Form
	{
		private DataSet dsCurrent = new DataSet();
		private string gridFormType = string.Empty;

		public string GridFormType
		{
			get { return gridFormType; }
			set { gridFormType = value; }
		}

		public DataSet DsCurrent
		{
			get { return dsCurrent; }
			set { dsCurrent = value; }
		}
		public GridForm()
		{
			InitializeComponent();
		}
		public void GridFindItems()
		{
			lblFindItem_LinkClicked(this.lblFindItem, null);
		}
		public void ExportData()
		{
			SaveFileDialog saveFile = new SaveFileDialog();
			saveFile.Filter = "Excel files (*.csv)|*.csv|All files (*.*)|*.*";
			if (saveFile.ShowDialog() != DialogResult.OK) return;
			//建立文件写入器,注意后面设定了读取器的字符集
			StreamWriter sw = new StreamWriter(saveFile.FileName, true, System.Text.Encoding.Default);
			DataView dvCur = this.dataGridView.DataSource as DataView;
			try
			{
				for (int i = 0; i < dvCur.Count; i++)
				{
					string line = string.Empty;
					foreach (DataColumn col in this.dsCurrent.Tables[0].Columns)
					{
						line += dvCur[i][col.ColumnName] + ",";
					}
					sw.WriteLine(line);
				}
			}
			catch (Exception e)
			{
				Logger.Log(e.Message);
			}
			finally
			{
				sw.Close();
			}
		}
	public void InportData()
	{
		OpenFileDialog openFile = new OpenFileDialog();
		openFile.Filter = "Excel files (*.csv)|*.csv|All files (*.*)|*.*";
		if (openFile.ShowDialog() != DialogResult.OK) return;
		StreamReader sr = new StreamReader(openFile.FileName, System.Text.Encoding.Default);
		DataView dvCur = this.dataGridView.DataSource as DataView;
		try
		{
			string line = string.Empty;
			while ((line = sr.ReadLine()) != null)
			{
				string[] cols = line.Split(',');
				DataRowView r = dvCur.AddNew();
				for (int i = 0; i < this.dsCurrent.Tables[0].Columns.Count; i++)
				{
					r[i] = cols[i];
				}
			}
		}
		catch (Exception e)
		{
			Logger.Log(e.Message);
		}
		finally
		{
			sr.Close();
		}
	}

		private void GridForm_Load(object sender, EventArgs e)
		{
			this.dataGridView.DataSource = this.dsCurrent.Tables[0].DefaultView;
		}
		private void lklAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (this.GridFormType == BillType.Daily)
			{
				BillDailyForm billDailyForm = new BillDailyForm();
				billDailyForm.MaximizeBox = false;
				billDailyForm.MinimizeBox = false;
				billDailyForm.FormClosed += new FormClosedEventHandler(billDailyForm_FormClosed);
				billDailyForm.Show();
			}
			if (this.GridFormType == BillType.Invest)
			{
				BillInvestForm billInvestForm = new BillInvestForm();
				billInvestForm.MaximizeBox = false;
				billInvestForm.MinimizeBox = false;
				billInvestForm.FormClosed += new FormClosedEventHandler(billInvestForm_FormClosed);
				billInvestForm.Show();
			}

		}
		private void billInvestForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			BillInvestForm bf = sender as BillInvestForm;
			if (bf.FormEnable)
			{
				DataTable dtCur = this.dsCurrent.Tables[0];
				BillInvest bill = bf.BillCurrent;
				dtCur.Rows.Add(new object[] { 
				bill.BIDate, bill.BICode, bill.BIName,bill.BICopys, bill.BIPrice,bill.BIType,
				bill.BICopys*bill.BIPrice,bill.BRemarks });
				DataRow row = this.Tag as DataRow;
				bill.BIAID = Convert.ToInt32(row["AID"]);
				new Business.BillInvestManager().Insert(bill);
			}
		}
		private void billDailyForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			BillDailyForm bf = sender as BillDailyForm;
			BillDaily bill = bf.BillCurrent;
			Business.BillDailyManager billManager = new Business.BillDailyManager();
			if (bf.FormEnable)
			{
				DataRow rNew = billManager.CreateDataRow(bill, this.dsCurrent.Tables[0]);
				this.dsCurrent.Tables[0].Rows.Add(rNew);
				//INTO DB
				DataRow row = this.Tag as DataRow;
				bill.BAID = Convert.ToInt32(row["AID"]);
				bill.BDBalance = Convert.ToSingle(rNew["余额"]);
				billManager.Insert(bill);
			}
		}
		private void lklDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			DataView dvCur = this.dataGridView.DataSource as DataView;
			dvCur.Delete(this.dataGridView.CurrentCell.RowIndex);
		}
		private void lblFindItem_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			GridFindForm gridFindForm = new GridFindForm();
			gridFindForm.MaximizeBox = false;
			gridFindForm.MinimizeBox = false;
			gridFindForm.FormClosed += new FormClosedEventHandler(gridFindForm_FormClosed);
			gridFindForm.Show();
		}
		private void gridFindForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			GridFindForm f = sender as GridFindForm;
			if (f.FindValue != string.Empty)
			{
				DataView dvCur = this.dataGridView.DataSource as DataView;
				try
				{
					if (f.Type == GridFindForm.FindType.FDateTime)
						dvCur.RowFilter = string.Format("交易日期 >= '{0}' AND 交易日期 <= '{1}'", f.FindValue, DateTime.Parse(f.FindValue).AddDays(1));
					if (f.Type == GridFindForm.FindType.FRemark)
						dvCur.RowFilter = string.Format("说明 LIKE '*{0}*'", f.FindValue);
					if (f.Type == GridFindForm.FindType.FName)
					{
						if (this.GridFormType == BillType.Daily)
							dvCur.RowFilter = string.Format("收支项目 LIKE '*{0}*'", f.FindValue);
						else
							dvCur.RowFilter = string.Format("名称 LIKE '*{0}*'", f.FindValue);
					}
				}
				catch (Exception em)
				{
					MessageBox.Show("查找格式无效," + em.Message);
				}

			}
		}
		private void ckUpdate_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckUpdate.Checked)
			{
				this.lblUpdate.Visible = true;
				this.dataGridView.ReadOnly = false;
			}
			else
			{
				this.lblUpdate.Visible = false;
				this.dataGridView.ReadOnly = true;
			}
		}

		private void lblUpdate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (this.GridFormType == BillType.Daily)
			{
				DataRow row = this.Tag as DataRow;
				Business.BillDailyManager m = new Business.BillDailyManager();
				m.Update(this.dsCurrent, row["AID"].ToString());
				m.Delete(this.dsCurrent, row["AID"].ToString());
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -