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

📄 waredatamanage.h

📁 Visual C++.net数据库开发经典案例
💻 H
字号:
#pragma once
#include "LinkDataBase.h"
#include "DataGridDoubleClickColumn.h"
#include "DataGridComboBoxColumn.h"
#include "DataGridNoActiveCellColumn.h"

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace My
{
	/// <summary> 
	/// WareDataManage 摘要
	///
	/// 警告: 如果您更改该类的名称,则将需要更改 
	///          与该类所依赖的所有 .resx 文件关联的托管资源编译器工具的 
	///          “资源文件名”属性。  否则,
	///          设计器将不能与此窗体关联的
	///          本地化资源正确交互。
	/// </summary>
	public __gc class WareDataManage : public System::Windows::Forms::Form
	{
	//--------------------成员声明------------------------
	public:
		String* SendWareID;
		String* SendWareName;
		String* SendWareUnit;
	private:
		LinkDataBase* MyDataBase;	//数据库连接类
		DataSet* ds;
		bool blSellTableShow;
		String* strTableName;
	private: System::Windows::Forms::ToolBarButton *  tbarDelete;
	private: System::Windows::Forms::GroupBox *  groupBox1;
	private: System::Windows::Forms::TextBox *  tboxWareWord;
	private: System::Windows::Forms::Label *  label1;
	private: System::Windows::Forms::Button *  btnSearch;
			 String* strSQL;

	//-------------------成员声明结束-----------------------
	public: 
		WareDataManage(bool blShowToolBar)
		{
			InitializeComponent();
			this->MyDataBase = new LinkDataBase();
			this->strTableName = new String("商品清单");
			this->strSQL = new String("Select 货号,条码,拼音编码,品名,单位,进货价,销售价1,销售价2,规格,产地,类别,最低售价 from 商品清单;");
			this->ds = this->MyDataBase->SelectDataBase(this->strSQL,this->strTableName);
			//如果ToolBar可见,则只显示商品资料维护表,DataGrid可编辑;如果不可见,则同时显示销售表,DataGrid不可编辑
			this->DataGridStateControl(!blShowToolBar);
			this->dgrdWareData->DataSource = ds->Tables->Item[0];
			this->toolBar1->Enabled = blShowToolBar;
			this->toolBar1->Visible = blShowToolBar;
		}
        
	protected: 
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}

	private: System::Windows::Forms::DataGrid *  dgrdWareData;
	private: System::Windows::Forms::ToolBar *  toolBar1;



	private: System::Windows::Forms::ImageList *  imageList1;
	private: System::Windows::Forms::ToolBarButton *  tbarSave;
	private: System::ComponentModel::IContainer *  components;

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>


		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = new System::ComponentModel::Container();
			System::Resources::ResourceManager *  resources = new System::Resources::ResourceManager(__typeof(My::WareDataManage));
			this->dgrdWareData = new System::Windows::Forms::DataGrid();
			this->toolBar1 = new System::Windows::Forms::ToolBar();
			this->tbarSave = new System::Windows::Forms::ToolBarButton();
			this->tbarDelete = new System::Windows::Forms::ToolBarButton();
			this->imageList1 = new System::Windows::Forms::ImageList(this->components);
			this->groupBox1 = new System::Windows::Forms::GroupBox();
			this->tboxWareWord = new System::Windows::Forms::TextBox();
			this->label1 = new System::Windows::Forms::Label();
			this->btnSearch = new System::Windows::Forms::Button();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dgrdWareData))->BeginInit();
			this->groupBox1->SuspendLayout();
			this->SuspendLayout();
			// 
			// dgrdWareData
			// 
			this->dgrdWareData->DataMember = S"";
			this->dgrdWareData->Dock = System::Windows::Forms::DockStyle::Fill;
			this->dgrdWareData->HeaderForeColor = System::Drawing::SystemColors::ControlText;
			this->dgrdWareData->Location = System::Drawing::Point(0, 120);
			this->dgrdWareData->Name = S"dgrdWareData";
			this->dgrdWareData->Size = System::Drawing::Size(728, 237);
			this->dgrdWareData->TabIndex = 0;
			this->dgrdWareData->DoubleClick += new System::EventHandler(this, dgrdWareData_DoubleClick);
			// 
			// toolBar1
			// 
			System::Windows::Forms::ToolBarButton* __mcTemp__1[] = new System::Windows::Forms::ToolBarButton*[2];
			__mcTemp__1[0] = this->tbarSave;
			__mcTemp__1[1] = this->tbarDelete;
			this->toolBar1->Buttons->AddRange(__mcTemp__1);
			this->toolBar1->DropDownArrows = true;
			this->toolBar1->ImageList = this->imageList1;
			this->toolBar1->Location = System::Drawing::Point(0, 0);
			this->toolBar1->Name = S"toolBar1";
			this->toolBar1->ShowToolTips = true;
			this->toolBar1->Size = System::Drawing::Size(728, 41);
			this->toolBar1->TabIndex = 1;
			this->toolBar1->ButtonClick += new System::Windows::Forms::ToolBarButtonClickEventHandler(this, toolBar1_ButtonClick);
			// 
			// tbarSave
			// 
			this->tbarSave->ImageIndex = 0;
			this->tbarSave->Text = S"保存";
			this->tbarSave->ToolTipText = S"保存修改";
			// 
			// tbarDelete
			// 
			this->tbarDelete->ImageIndex = 1;
			this->tbarDelete->Text = S"删除";
			this->tbarDelete->ToolTipText = S"删除数据";
			// 
			// imageList1
			// 
			this->imageList1->ImageSize = System::Drawing::Size(16, 16);
			this->imageList1->ImageStream = (__try_cast<System::Windows::Forms::ImageListStreamer *  >(resources->GetObject(S"imageList1.ImageStream")));
			this->imageList1->TransparentColor = System::Drawing::Color::Transparent;
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->tboxWareWord);
			this->groupBox1->Controls->Add(this->label1);
			this->groupBox1->Controls->Add(this->btnSearch);
			this->groupBox1->Dock = System::Windows::Forms::DockStyle::Top;
			this->groupBox1->Location = System::Drawing::Point(0, 41);
			this->groupBox1->Name = S"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(728, 79);
			this->groupBox1->TabIndex = 5;
			this->groupBox1->TabStop = false;
			// 
			// tboxWareWord
			// 
			this->tboxWareWord->Location = System::Drawing::Point(256, 27);
			this->tboxWareWord->Name = S"tboxWareWord";
			this->tboxWareWord->Size = System::Drawing::Size(216, 21);
			this->tboxWareWord->TabIndex = 7;
			this->tboxWareWord->Text = S"";
			// 
			// label1
			// 
			this->label1->Location = System::Drawing::Point(144, 32);
			this->label1->Name = S"label1";
			this->label1->Size = System::Drawing::Size(96, 16);
			this->label1->TabIndex = 6;
			this->label1->Text = S"商品名称拼音";
			// 
			// btnSearch
			// 
			this->btnSearch->Location = System::Drawing::Point(496, 27);
			this->btnSearch->Name = S"btnSearch";
			this->btnSearch->Size = System::Drawing::Size(88, 24);
			this->btnSearch->TabIndex = 5;
			this->btnSearch->Text = S"查询";
			this->btnSearch->Click += new System::EventHandler(this, btnSearch_Click_1);
			// 
			// WareDataManage
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
			this->ClientSize = System::Drawing::Size(728, 357);
			this->Controls->Add(this->dgrdWareData);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->toolBar1);
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = S"WareDataManage";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = S"WareDataManage";
			this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
			this->Load += new System::EventHandler(this, WareDataManage_Load);
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dgrdWareData))->EndInit();
			this->groupBox1->ResumeLayout(false);
			this->ResumeLayout(false);

		}		
	private: System::Void WareDataManage_Load(System::Object *  sender, System::EventArgs *  e)
			 {
			 }
	//--------------------成员方法------------------------
	public:
		void DataGridStateControl(bool blReadOnly)
		{
			DataGridTableStyle* ts = new DataGridTableStyle();
			ts->AlternatingBackColor = Color::LightGray;
			//必须填写,否则样式表不起作用
			ts->MappingName = this->ds->Tables->Item[0]->TableName;
			ts->AllowSorting = false;

			int numCols = this->ds->Tables->Item[0]->Columns->Count;
			for(int i = 0;i < numCols;i++)
			{
				if(!blReadOnly)
				{
					//如果DataGrid可以编辑,则可以选中任意单元
					if(i == 4)
					{
						//为“单位”字段添加下拉列表框
						DataTable* tempTable = new DataTable();
						tempTable->Columns->Add();
						String* rowValues[] = {S"包",S"瓶",S"盒",S"带",S"箱"};
						for(int j = 0; j < 5;j++)
						{
							tempTable->Rows->Add(tempTable->NewRow());
							tempTable->Rows->Item[j]->Item[0] = String::Copy(rowValues[j]);
						}
						//构造新的ComboBoxColumn对象
						String* colName = this->ds->Tables->Item[0]->Columns->Item[i]->ColumnName;
                        
						DataGridComboBoxColumn* aComboBoxColumn = new DataGridComboBoxColumn(colName,tempTable,tempTable->Columns->Item[0]->ColumnName, tempTable->Columns->Item[0]->ColumnName,this->dgrdWareData);
						ts->GridColumnStyles->Add(aComboBoxColumn);
					}
					else
					{
						DataGridTextBoxColumn* aTextColumn = new DataGridTextBoxColumn();
						aTextColumn->MappingName = this->ds->Tables->Item[0]->Columns->Item[i]->ColumnName;
						aTextColumn->HeaderText = this->ds->Tables->Item[0]->Columns->Item[i]->ColumnName;
						aTextColumn->NullText = String::Empty;
						aTextColumn->Format = S"F";
						ts->GridColumnStyles->Add(aTextColumn);
					}
				}
				else
				{
					//如果DataGrid不可编辑,则单击选中整行
					DataGridNoActiveCellColumn* aNoActiveCellColumn = new DataGridNoActiveCellColumn();
                    
					aNoActiveCellColumn->get_TextBox()->Enabled = false;
					aNoActiveCellColumn->MappingName = this->ds->Tables->Item[0]->Columns->Item[i]->ColumnName;
					aNoActiveCellColumn->HeaderText = this->ds->Tables->Item[0]->Columns->Item[i]->ColumnName;
					aNoActiveCellColumn->NullText = String::Empty;
					aNoActiveCellColumn->Format = S"F";
					ts->GridColumnStyles->Add(aNoActiveCellColumn);
                    
				}			
			}
			this->dgrdWareData->TableStyles->Add(ts);
		}
		//-----------------------------设置表格为只读--------------------------------------
		void SetDataGridReadOnly(bool blInSell)
		{
			this->dgrdWareData->ReadOnly = true;
			if(blInSell == true)
				blSellTableShow = true;
			else
				blSellTableShow = false;
		}

		//------------------------根据商品拼音编码查询商品---------------------------------
	private: System::Void btnSearch_Click(System::Object *  sender, System::EventArgs *  e)
		{
			String* strSpellWord = this->tboxWareWord->Text;
			String* tempSQL = String::Format("Select * from 商品清单 where 拼音编码 like '{0}%';",strSpellWord);
			this->ds->Clear();
			this->MyDataBase->SelectDataBase(this->ds,tempSQL,strTableName);
		}

		//------------------------对数据的修改,增加和删除---------------------------------
	private: System::Void toolBar1_ButtonClick(System::Object *  sender, System::Windows::Forms::ToolBarButtonClickEventArgs *  e)
		 {
			 if(String::Compare(e->Button->ToolTipText,S"保存修改") == 0)
			 {
				 try
				 {
					 //改变当前单元的编辑状态
					 int row = this->dgrdWareData->CurrentCell.RowNumber;
					 int col = this->dgrdWareData->CurrentCell.ColumnNumber;
					 this->dgrdWareData->CurrentCell =  DataGridCell(row + 1,col);


					 if(this->ds->HasChanges())
					 {						 
						 this->MyDataBase->UpdateDataBase(this->ds,strTableName);
						 //更新ds中的数据
						 this->ds->AcceptChanges();
						 //this->ds->Clear();
						 //this->MyDataBase->SelectDataBase(this->ds,this->strSQL,strTableName);
					 }
					 else
					 {
						 MessageBox::Show(S"没有要修改的数据!",S"错误");
						 return;
					 }
				 }
				 catch(Exception* e)
				 {	
					 String* temp = String::Format("{0}: {1}",e->get_Source(),e->Message);
					  MessageBox::Show(temp,S"错误");
					  return;
				 }

				 MessageBox::Show(S"数据修改成功!",S"提示");
				
			 }
			 else if(String::Compare(e->Button->ToolTipText,S"删除数据") == 0)
			 {
				 if(MessageBox::Show(S"确实要删除这条记录吗?",S"询问",MessageBoxButtons::YesNo) == DialogResult::Yes)
				 {
					 int intRowNumber = this->dgrdWareData->CurrentCell.RowNumber;
					 try
					 {
						 this->ds->Tables->Item[0]->Rows->Item[intRowNumber]->Delete();
						 this->MyDataBase->UpdateDataBase(this->ds->GetChanges(),strTableName);	
						 //更新ds中的数据
						 this->ds->AcceptChanges();
					 }
					 catch(Exception* e)
					 {	
						 String* temp = String::Format("{0}: {1}",e->get_Source(),e->Message);
						 MessageBox::Show(temp,S"错误");
						 return;
					 }
					 
					 MessageBox::Show(S"删除数据成功!",S"提示");
				 }
			 }
		 }
		 //-------------------其他窗体调用商品信息窗体时,双击选择商品信息----------------------

	private: System::Void dgrdWareData_DoubleClick(System::Object *  sender, System::EventArgs *  e)
		 {
			  if(this->dgrdWareData->ReadOnly == true)
			 {
				 int intCurrentRowNumber = this->dgrdWareData->CurrentCell.RowNumber;
				 SendWareID = this->dgrdWareData->get_Item(intCurrentRowNumber,0)->ToString();
				 SendWareName = this->dgrdWareData->get_Item(intCurrentRowNumber,3)->ToString();
				 SendWareUnit = this->dgrdWareData->get_Item(intCurrentRowNumber,4)->ToString();
				 this->DialogResult = DialogResult::OK;
			 }
		 }

private: System::Void btnSearch_Click_1(System::Object *  sender, System::EventArgs *  e)
		 {
			 this->strSQL = String::Format(S"Select 货号,条码,拼音编码,品名,单位,进货价,销售价1,销售价2,规格,产地,类别,最低售价 from 商品清单 Where 拼音编码 Like '%{0}%'",this->tboxWareWord->Text);
			 this->ds->Clear();
			 this->MyDataBase->SelectDataBase(this->ds,this->strSQL,S"商品清单");
		 }

};
}

⌨️ 快捷键说明

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