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

📄 stokerdatamanage.h

📁 Visual C++.net数据库开发经典案例
💻 H
字号:
#pragma once
#include "LinkDataBase.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> 
	/// StokerDataManage 摘要
	///
	/// 警告: 如果您更改该类的名称,则将需要更改 
	///          与该类所依赖的所有 .resx 文件关联的托管资源编译器工具的 
	///          “资源文件名”属性。  否则,
	///          设计器将不能与此窗体关联的
	///          本地化资源正确交互。
	/// </summary>
	public __gc class StokerDataManage : public System::Windows::Forms::Form
	{
	//--------------------成员声明------------------------
	public:
		String* SendStockerID;
		String* SendStockerName;
	private:
		LinkDataBase* MyDataBase;	//数据库连接类
		DataSet* ds;
		bool blSellTableShow;
		String* strTableName;
	private: System::Windows::Forms::GroupBox *  groupBox1;
	private: System::Windows::Forms::TextBox *  tboxStokerWord;
	private: System::Windows::Forms::Label *  label1;
	private: System::Windows::Forms::Button *  btnSearch;
			 String* strSQL;

	//-------------------成员声明结束-----------------------
	public: 
		StokerDataManage(void)
		{
			InitializeComponent();
		}
        
	protected: 
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}

	private: System::Windows::Forms::ToolBarButton *  tbarDelete;
	private: System::Windows::Forms::ImageList *  imageList1;


	private: System::Windows::Forms::ToolBar *  toolBar1;
	private: System::Windows::Forms::ToolBarButton *  tbarSave;
	private: System::Windows::Forms::DataGrid *  dgrdStokerData;

	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::StokerDataManage));
			this->tbarDelete = new System::Windows::Forms::ToolBarButton();
			this->imageList1 = new System::Windows::Forms::ImageList(this->components);
			this->toolBar1 = new System::Windows::Forms::ToolBar();
			this->tbarSave = new System::Windows::Forms::ToolBarButton();
			this->dgrdStokerData = new System::Windows::Forms::DataGrid();
			this->groupBox1 = new System::Windows::Forms::GroupBox();
			this->tboxStokerWord = 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->dgrdStokerData))->BeginInit();
			this->groupBox1->SuspendLayout();
			this->SuspendLayout();
			// 
			// 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;
			// 
			// 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(752, 41);
			this->toolBar1->TabIndex = 6;
			this->toolBar1->ButtonClick += new System::Windows::Forms::ToolBarButtonClickEventHandler(this, toolBar1_ButtonClick);
			// 
			// tbarSave
			// 
			this->tbarSave->ImageIndex = 0;
			this->tbarSave->Text = S"保存";
			this->tbarSave->ToolTipText = S"保存修改";
			// 
			// dgrdStokerData
			// 
			this->dgrdStokerData->DataMember = S"";
			this->dgrdStokerData->Dock = System::Windows::Forms::DockStyle::Fill;
			this->dgrdStokerData->HeaderForeColor = System::Drawing::SystemColors::ControlText;
			this->dgrdStokerData->Location = System::Drawing::Point(0, 112);
			this->dgrdStokerData->Name = S"dgrdStokerData";
			this->dgrdStokerData->Size = System::Drawing::Size(752, 309);
			this->dgrdStokerData->TabIndex = 5;
			this->dgrdStokerData->DoubleClick += new System::EventHandler(this, dgrdStokerData_DoubleClick);
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->tboxStokerWord);
			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(752, 71);
			this->groupBox1->TabIndex = 10;
			this->groupBox1->TabStop = false;
			// 
			// tboxStokerWord
			// 
			this->tboxStokerWord->Location = System::Drawing::Point(260, 23);
			this->tboxStokerWord->Name = S"tboxStokerWord";
			this->tboxStokerWord->Size = System::Drawing::Size(216, 21);
			this->tboxStokerWord->TabIndex = 12;
			this->tboxStokerWord->Text = S"";
			// 
			// label1
			// 
			this->label1->Location = System::Drawing::Point(156, 28);
			this->label1->Name = S"label1";
			this->label1->Size = System::Drawing::Size(96, 16);
			this->label1->TabIndex = 11;
			this->label1->Text = S"供货商名称拼音";
			// 
			// btnSearch
			// 
			this->btnSearch->Location = System::Drawing::Point(508, 23);
			this->btnSearch->Name = S"btnSearch";
			this->btnSearch->Size = System::Drawing::Size(88, 24);
			this->btnSearch->TabIndex = 10;
			this->btnSearch->Text = S"查询";
			this->btnSearch->Click += new System::EventHandler(this, btnSearch_Click);
			// 
			// StokerDataManage
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
			this->ClientSize = System::Drawing::Size(752, 421);
			this->Controls->Add(this->dgrdStokerData);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->toolBar1);
			this->Location = System::Drawing::Point(264, 64);
			this->Name = S"StokerDataManage";
			this->Text = S"StokerDataManage";
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dgrdStokerData))->EndInit();
			this->groupBox1->ResumeLayout(false);
			this->ResumeLayout(false);

		}		
		//--------------------成员方法------------------------
	public: 
		StokerDataManage(bool blShowToolBar)
		{
			InitializeComponent();
			this->MyDataBase = new LinkDataBase();
			this->strTableName = new String("供货商清单");
			this->strSQL = new String("Select * from 供货商清单;");
			this->ds = this->MyDataBase->SelectDataBase(this->strSQL,this->strTableName);
			//如果ToolBar可见,则只显示商品资料维护表,DataGrid可编辑;如果不可见,则同时显示销售表,DataGrid不可编辑
			this->DataGridStateControl(!blShowToolBar);
			this->dgrdStokerData->DataSource = ds->Tables->Item[0];
			this->toolBar1->Enabled = blShowToolBar;
		}
	
		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可以编辑,则可以选中任意单元
						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->dgrdStokerData->TableStyles->Add(ts);
		}
		//-----------------------------设置表格为只读--------------------------------------
		void SetDataGridReadOnly(bool blInSell)
		{
			this->dgrdStokerData->ReadOnly = true;
			if(blInSell == true)
				blSellTableShow = true;
			else
				blSellTableShow = false;
		}

		//------------------------根据商品拼音编码查询商品---------------------------------
	private: System::Void btnSearch_Click(System::Object *  sender, System::EventArgs *  e)
		{
			String* strSpellWord = this->tboxStokerWord->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->dgrdStokerData->CurrentCell.RowNumber;
					 int col = this->dgrdStokerData->CurrentCell.ColumnNumber;
					 this->dgrdStokerData->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->dgrdStokerData->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 dgrdStokerData_DoubleClick(System::Object *  sender, System::EventArgs *  e)
		 {
			  if(this->dgrdStokerData->ReadOnly == true)
			 {
				 int intCurrentRowNumber = this->dgrdStokerData->CurrentCell.RowNumber;
				 SendStockerID = this->dgrdStokerData->get_Item(intCurrentRowNumber,0)->ToString();
				 SendStockerName = this->dgrdStokerData->get_Item(intCurrentRowNumber,3)->ToString();
				 this->DialogResult = DialogResult::OK;
			 }
		 }






};
}

⌨️ 快捷键说明

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