Exemple #1
0
/**
	@brief	callback function

	@author	HumKyung

	@date	2011.01.05

	@param

	@return
*/
void CBusToBusView::OnBnClickedDelete()
{
	if(m_wndGridCtrl.GetSafeHwnd())
	{
		if(IDYES == AfxMessageBox(_T("Check한 아이템을 삭제할까요?") , MB_YESNO|MB_ICONWARNING))
		{
			while(1)
			{
				bool bDeleted = false;
				const int iRowCount = m_wndGridCtrl.GetRowCount();
				for(int i = iRowCount - 1;i > 0;--i)
				{
					CGridCellCheck* pCellCheck = (CGridCellCheck*)(m_wndGridCtrl.GetCell(i , 0));
					if(pCellCheck)
					{
						if(pCellCheck->GetCheck())
						{
							m_wndGridCtrl.DeleteRow(i);
							bDeleted = true;
							break;
						}
					}
				}

				if(false == bDeleted) break;
			}

			m_wndGridCtrl.RedrawWindow();

			GetDocument()->SetModifiedFlag();	/// 2014.04.04 added by humkyung
		}
	}
}
void CItemCommerceSearchView::OnExcelOut(CString filepath, CString filename)
{
	CSpreadSheet sOutFile(filepath, "ListSheet");

	CStringArray readArray;
	CGridCellCheck *pCell;
	BOOL bCheck;

	sOutFile.BeginTransaction();
  // 加入标题
	int iCol = m_cDisplay.GetColumnCount();
	int iRow = m_cDisplay.GetRowCount();
    readArray.RemoveAll();
	int i,j;
	CString str;
    for(i =0; i< iCol;i++)
	{
        str = m_cDisplay.GetItemText(0,i);
		if(str =="")
			str= "0";
	     readArray.Add(str);
	}
	sOutFile.AddHeaders(readArray);

	// 加入数据

	for(i = 1; i < iRow; i++)
	{
		readArray.RemoveAll();
        for(j=0;j<iCol;j++) 
		{
			if(j>=1 && m_iName[j-1].tType == 4)
			{
				 pCell = (CGridCellCheck*)m_cDisplay.GetCell(i,j);
                bCheck = pCell->GetCheck();
				if(bCheck)
					str = "TRUE";
				else
					str = "FALSE";
			}
			else
			{
			   str = m_cDisplay.GetItemText(i,j);
			   if(str=="")
				   str = "/";
			}

			readArray.Add(str);
		}
		sOutFile.AddRow(readArray,i+1);
	}
  
	sOutFile.Commit();	

	AfxMessageBox("查询结果已经保存为文件:\n "+filename);
    
}
void CDataBunchGridCtrl::InsertDataBunch(int rowIndex, const DataBunch& dataBunch, const tstring& baseDirectory){
	DataConverter converter;

	GV_ITEM item;
	item.mask = GVIF_TEXT;
	item.row = rowIndex;

	int colIndex = 0;

	item.col = colIndex;
	item.strText = converter.ConvertIntToString(rowIndex).c_str();
	this->SetItem(&item);
	this->SetItemState(rowIndex, colIndex, GVIS_READONLY);
	colIndex++;

	item.col = colIndex;
	item.strText = dataBunch.GetName().c_str();
	this->SetItem(&item);
	this->SetItemState(rowIndex, colIndex, GVIS_READONLY);
	colIndex++;

	CGridCellCheck* pCheckBoxCell = NULL;
	this->SetCellType(rowIndex, colIndex, RUNTIME_CLASS(CGridCellCheck));
	pCheckBoxCell = (CGridCellCheck*)this->GetCell(rowIndex, colIndex);
	if (dataBunch.GetSaveYN() == TRUE){
		pCheckBoxCell->SetCheck(TRUE);
	}
	else{
		pCheckBoxCell->SetCheck(FALSE);
	}
	colIndex++;

	tstring fullDirectoryWithFileName = baseDirectory + TEXT("\\") + dataBunch.GetName() + TEXT("\\");
	item.col = colIndex;
	item.strText = fullDirectoryWithFileName.c_str();
	this->SetItem(&item);
	this->SetItemState(rowIndex, colIndex, GVIS_READONLY);
}
void CItemCommerceSearchView::RefreshGrid(_RecordsetPtr& pSet,int row)
{
	int iCol = m_cDisplay.GetColumnCount()-1;
    GV_ITEM Item;
	COleDateTime dTemp,dCompare;
    CGridCellCheck* pCell;
	BOOL bCheck;
	CString str;

	Item.mask = GVIF_TEXT|GVIF_FORMAT|GVL_BOTH|GVL_VERT ;
	Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
	m_cDisplay.SetListMode(TRUE);
	m_cDisplay.SetSingleRowSelection(FALSE);
	Item.row = row;
	Item.col = 0;
	Item.strText.Format("%d",row);
	m_cDisplay.SetItem(&Item);
	for(int i = 0; i<iCol; i++)
	{

		Item.col = i+1;

		Holder = pSet->GetCollect(m_iName[i].sItem.AllocSysString());
		switch(m_iName[i].tType) {
		case 1:
			Item.strText = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			break;
		case 2:
			dTemp = Holder.date;
			str = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			if(str.IsEmpty())
			{
				Item.strText = _T("- -");
			}
			else
			    Item.strText = dTemp.Format("%Y-%m-%d");
			break;
		case 3:
		//	dbNum = (double)Holder;
		//	Item.strText.Format("%f",dbNum);
			Item.strText = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			break;
		case  4:
			m_cDisplay.SetCellType(row,i+1, RUNTIME_CLASS(CGridCellCheck));
			pCell = (CGridCellCheck*)m_cDisplay.GetCell(row,i+1);
			bCheck = (bool)Holder;
			if(bCheck)
				pCell->SetCheck(TRUE);
			else
				pCell->SetCheck(FALSE);
			Item.strText = "";
			break;
		default:
			break;
		}

		m_cDisplay.SetItem(&Item);
	} 
	m_cDisplay.Invalidate();
    m_cDisplay.AutoSizeColumns();
}
void CItemCommerceSearchView::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
    

	int iCase = m_cSearchCase.GetRowCount()-1;
    int iDispay = m_cDisplay.GetColumnCount()-1;

	m_cDisplay.DeleteNonFixedRows();
    CString str,strCase1,strCase2,str1,str2;

	CGridCellCheck* pCell; 
	BOOL bCheck;

	if(iCase == 0)
	{
		AfxMessageBox("查询条目为空,请先定制查询条件!",MB_ICONEXCLAMATION);
		return;
	}
	if(iDispay==0)
	{
		AfxMessageBox("显示条目为空,请先显示设置!",MB_ICONEXCLAMATION);
		return;
	}
	str = "select DISTINCT";
    for(int i= 0;i<iDispay;i++)
	{
        str = str + " " +m_iName[i].sTable+"."+m_iName[i].sItem + " ";
//		str = str + " " +m_iName[i].sItem + " ";
		if(i<iDispay-1)
			str = str+", ";

	}

	switch(m_iLink)
	{
	case 0:
//	   str  = str+"from (PBInformation INNER JOIN (Configuration INNER JOIN  OrderOfProject ON Configuration.OrderID = OrderOfProject.OrderID) ON PBInformation.ID = OrderOfProject.ID) INNER JOIN Delivery ON (Configuration.OrderID = Delivery.OrderID) AND (OrderOfProject.OrderID = Delivery.OrderID)  where (";
	   str  = str+"from (Configuration LEFT JOIN Controller ON Configuration.OrderID = Controller.OrderID) INNER JOIN (PBInformation INNER JOIN (OrderOfProject INNER JOIN Delivery ON OrderOfProject.OrderID = Delivery.OrderID) ON PBInformation.ID = OrderOfProject.ID) ON Configuration.OrderID = OrderOfProject.OrderID  where (";
       break;
	case 1:
		str= str + " from (Configuration LEFT JOIN Controller ON Configuration.OrderID = Controller.OrderID) RIGHT JOIN (PBInformation INNER JOIN (OrderOfProject LEFT JOIN Delivery ON OrderOfProject.OrderID = Delivery.OrderID) ON PBInformation.ID = OrderOfProject.ID) ON Configuration.OrderID = OrderOfProject.OrderID where (";
//	   str= str + " from (PBInformation OUTER JOIN (Configuration OUTER JOIN OrderOfProject ON Configuration.OrderID = OrderOfProject.OrderID) ON PBInformation.ID = OrderOfProject.ID) OUTER JOIN Delivery ON OrderOfProject.OrderID = Delivery.OrderID where (";
		break;
	default:
		break;
	}

//	str  = str+"from OrderOfProject INNER JOIN Delivery (INNER JOIN Configuration ON Delivery.OrderID = Configuration.OrderID)   where ";

	for(int j= 0;j<iCase;j++)
	{
		
		  if(j!=0)
			str = str + " and ";
          switch(m_iType[j].tType) {
          case 1:
            strCase1 = m_cSearchCase.GetItemText(j+1,4);
			strCase2= strCase1;
			if(m_iType[j].sItem == _T("OrderID"))
			{
				strCase2.Remove(' ');
				if(strCase2.GetLength()==8)
				{
				  str1= strCase2.Left(2);
				  str2 = strCase2.Mid(2,2);
				  strCase2 = strCase2.Right(4);
				  strCase2 = str1 + " " + str2 + " " +strCase2;
				  strCase1 = strCase2 ;
				}
			}
			str = str + " " + m_iType[j].sTable +"."+m_iType[j].sItem +" like  '%"+strCase1+"%'";
          	break;
          case 2:
            strCase1 =  m_cSearchCase.GetItemText(j+1, 2);
			strCase2 = m_cSearchCase.GetItemText(j+1,3);
			if(strCase1.IsEmpty()&&strCase2.IsEmpty())
				str= str +" " + m_iType[j].sTable+"."+m_iType[j].sItem+" Is Null";
			else{
			if(strCase1.IsEmpty())
				strCase1 = "1900-01-01";
			if(strCase2.IsEmpty())
				strCase2 = "9999-12-31";
			str = str+" " +  m_iType[j].sTable +"."+m_iType[j].sItem+ " >= #"+strCase1+"# and "+ m_iType[j].sTable +"."+m_iType[j].sItem+" <= #"+strCase2+"#";
			}
          	break;
		  case 3:
            strCase1 = m_cSearchCase.GetItemText(j+1, 2);
			strCase2 = m_cSearchCase.GetItemText(j+1, 3);
            if(strCase1.IsEmpty())
                 strCase1 = "0";
			if(strCase2.IsEmpty())
				strCase2 = "999999999";
			str = str + " " +  m_iType[j].sTable +"."+ m_iType[j].sItem+">= "+strCase1 + " and " +  m_iType[j].sTable +"."+ m_iType[j].sItem+" <= "+strCase2;
			break;
		  case 4:
			 pCell = (CGridCellCheck*)m_cSearchCase.GetCell(j+1, 5);
			 bCheck = pCell->GetCheck();
             if(bCheck)
				 strCase1 = "TRUE";
			 else 
				 strCase1 = "FALSE";
			 str = str +" " +  m_iType[j].sTable +"."+ m_iType[j].sItem + " = " + strCase1;
			break;
          default:
		    break;
          }
  
	}

	str= str +")";
	strSQL = str.AllocSysString();

	try{
		theApp.ADOExecute(m_pResult, theApp.m_pConnect, strSQL);
		int iCount  = theApp.GetRecordCountEx(m_pResult);
		m_iTotalCount = iCount;
	
		if(iCount==0)
		{
			AfxMessageBox(_T("没有所要查询的内容!"),MB_ICONEXCLAMATION);
			m_cDisplay.DeleteNonFixedRows();
			m_iTotalCount = 0;
			m_bSearch = FALSE;
			return;
		}
		else
		{
			m_bSearch = TRUE;
			m_pResult->MoveFirst();
            for(int i=1;i<=iCount;i++)
			{
				str.Format("%d",i);

				m_cDisplay.InsertRow(str, -1);
				RefreshGrid(m_pResult, i);
				m_pResult->MoveNext();
			}
		}

	}
	catch(_com_error &e)///捕捉异常
	{
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);///显示错误信
		return;
	} 
	catch(...)
	{
		AfxMessageBox("未知错误...");
		return;
	}
	UpdateData(FALSE);

}
void CItemCommerceSearchView::LoadData(void)
{
	CGridCellCheck* pCellCheck;
	CGridCellDateTime* pCellDate;
	GV_ITEM Item;

	m_cSearchCase.DeleteNonFixedRows();



	try{

		int iCount ;

		strSQL = " select * from SearchCase  where Check = TRUE order by ID";
		theApp.ADOExecute(m_pTempSet, theApp.m_pSearchConn,  strSQL);
        iCount  = theApp.GetRecordCountEx(m_pTempSet);
		
	//	if(m_iType)
	//		delete [] m_iType;
	//	m_iType = new int[iCount];
		CString str;
		if(iCount !=0)
			m_pTempSet->MoveFirst();
		int i = 0;
		if(m_iType)
			delete [] m_iType;
		m_iType = new ItemRow[iCount];
		while(!m_pTempSet->EndOfFile)
		{

			str.Format("%d",i+1);

			m_cSearchCase.InsertRow(str, -1);
			
			Holder = m_pTempSet->GetCollect(_T("Name"));
			m_iType[i].sName  = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			
			Holder = m_pTempSet->GetCollect(_T("Item"));
			m_iType[i].sItem  = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;

			Holder = m_pTempSet->GetCollect(_T("Type"));
			m_iType[i].tType  = (int)Holder;
           
			Holder = m_pTempSet->GetCollect(_T("Table"));
			m_iType[i].sTable = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;

			Item.row = i+1;
			for(int j = 0;j<6; j++)
			{
				Item.mask = GVIF_TEXT|GVIF_FORMAT|GVL_BOTH|GVL_VERT ;
				Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;

				Item.col = j;

				switch(j) {
			case 0:
				Item.strText.Format("%d",i+1);
				break;
			case 1:				
				Item.strText = m_iType[i].sName;
				break;
			case 2:
				if(m_iType[i].tType==2)
				{
					m_cSearchCase.SetCellType(i+1,j, RUNTIME_CLASS(CGridCellDateTime));

					pCellDate = (CGridCellDateTime*) m_cSearchCase.GetCell(i+1,j);
					pCellDate->SetTime(CTime::GetCurrentTime());
				//	Item.strText = _T("1900-1-1");
				}
			//	else
					Item.strText = _T("");
				break;
			case 3:
				if(m_iType[i].tType == 2)
				{
					m_cSearchCase.SetCellType(i+1,j, RUNTIME_CLASS(CGridCellDateTime));

					pCellDate = (CGridCellDateTime*) m_cSearchCase.GetCell(i+1,j);
					pCellDate->SetTime(CTime::GetCurrentTime());
				//	Item.strText = m_dCurrent.Format("%Y-%m-%d");
				}
			//	else
					Item.strText = _T("");
				
				break;
			case 4:
				Item.strText = _T("");
				break;
			case 5:
                if(m_iType[i].tType == 4)
				{
					m_cSearchCase.SetCellType(i+1,j, RUNTIME_CLASS(CGridCellCheck));
					pCellCheck = (CGridCellCheck*)m_cSearchCase.GetCell(i+1,j);
					pCellCheck->SetCheck(FALSE);
				}
				Item.strText = _T("");
				break;
			default:
				break;
				}
				m_cSearchCase.SetItem(&Item);
				
			}

			m_cSearchCase.Invalidate();
			m_pTempSet->MoveNext();
			i++;
		}
		m_cSearchCase.AutoSizeRows();
        
		m_cSearchCase.SetColumnWidth(2,100);
		m_cSearchCase.SetColumnWidth(3,100);
		m_cSearchCase.SetColumnWidth(4,100);
		for(int m= 0; m<iCount;m++)
		{
			switch(m_iType[m].tType) {
				//RGB(0xFF, 0xFF, 0xE0)
		case 1:
			m_cSearchCase.SetItemBkColour(m+1,2,RGB(0,0,0));
			m_cSearchCase.SetItemBkColour(m+1,3,RGB(0,0,0));
			m_cSearchCase.SetItemBkColour(m+1,5,RGB(0,0,0));
            m_cSearchCase.SetItemBkColour(m+1,4,RGB(0xFF, 0xFF, 0xE0));
			m_cSearchCase.SetItemState(m+1,4,m_cSearchCase.GetItemState(m+1,4) & ~GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,2,m_cSearchCase.GetItemState(m+1,2) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,3,m_cSearchCase.GetItemState(m+1,3) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,5,m_cSearchCase.GetItemState(m+1,5) | GVIS_READONLY);
			break;
		case 2:
			m_cSearchCase.SetItemBkColour(m+1,2,RGB(0xFF, 0xFF, 0xE0));
			m_cSearchCase.SetItemBkColour(m+1,3,RGB(0xFF, 0xFF, 0xE0));
			m_cSearchCase.SetItemBkColour(m+1,5,RGB(0,0,0));
			m_cSearchCase.SetItemBkColour(m+1,4,RGB(0, 0, 0));
			m_cSearchCase.SetItemState(m+1,4,m_cSearchCase.GetItemState(m+1,4) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,2,m_cSearchCase.GetItemState(m+1,2) & ~GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,3,m_cSearchCase.GetItemState(m+1,3) & ~GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,5,m_cSearchCase.GetItemState(m+1,5) | GVIS_READONLY);
			break;
		case 3:
			m_cSearchCase.SetItemBkColour(m+1,2,RGB(0xFF, 0xFF, 0xE0));
			m_cSearchCase.SetItemBkColour(m+1,3,RGB(0xFF, 0xFF, 0xE0));
			m_cSearchCase.SetItemBkColour(m+1,5,RGB(0,0,0));
			m_cSearchCase.SetItemBkColour(m+1,4,RGB(0, 0, 0));
			m_cSearchCase.SetItemState(m+1,4, m_cSearchCase.GetItemState(m+1,4) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,2,m_cSearchCase.GetItemState(m+1,2) & ~GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,3,m_cSearchCase.GetItemState(m+1,3) & ~GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,5,m_cSearchCase.GetItemState(m+1,5) | GVIS_READONLY);
			break;
		case 4:
			m_cSearchCase.SetItemBkColour(m+1,2,RGB(0, 0, 0));
			m_cSearchCase.SetItemBkColour(m+1,3,RGB(0, 0, 0));
			m_cSearchCase.SetItemBkColour(m+1,5,RGB(0xFF, 0xFF, 0xE0));
			m_cSearchCase.SetItemBkColour(m+1,4,RGB(0, 0, 0));
			m_cSearchCase.SetItemState(m+1,4, m_cSearchCase.GetItemState(m+1,4) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,2, m_cSearchCase.GetItemState(m+1,2) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,3, m_cSearchCase.GetItemState(m+1,3) | GVIS_READONLY);
			m_cSearchCase.SetItemState(m+1,5, m_cSearchCase.GetItemState(m+1,5) & ~GVIS_READONLY);
			break;
		default:
			break;
			}
			m_cSearchCase.SetItemState(m+1,1,m_cSearchCase.GetItemState(m+1,1) | GVIS_READONLY);
		}

		m_pTempSet->Close();
      
	}
	catch(_com_error &e)///捕捉异常
	{
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);///显示错误信
		return;
	} 
	catch(...)
	{
		AfxMessageBox("未知错误...");
		return;
	}
}