I see that the date time control is using the default date format as specified in the system settings for storing the cell value. How can I make sure that the date information is correctly retrieved on system with different date format (e.g. Germany)?
You can override StoreStyleRowCol and convert the date value to a number as follows
BOOL CMyGrid::StoreStyleRowCol(ROWCOL nRow, ROWCOL nCol, const CGXStyle* pStyle,...)
{
if (nCol == yourSpecifcCol && pStyle->GetIncludeValue())
{
CGXStyle style = *pStyle;
COleDateTime dt;
dt.ParseDateTime(style.GetValueRef());
style.SetValue((DATE) dt);
return CGXGridCore::StoreStyleRowCOl(nRow, nCol, &style,..);
}
return CGXGridCore::StoreStyleRowCOl(nRow, nCOl, pStyle, ...);
}
or subclass CGXDateTimeControl and override the following two methods:
BOOL CGXDateTimeCtrl::GetValue(CString& strResult)
{
// Reads out the window text and converts it into
// the plain value which should be stored in the style object.
if (!IsInit())
return FALSE;
if (GetModify())
{
strResult.Format(_T("%g"), GetDateTime().Format());
return TRUE;
}
else
return CGXControl::GetValue(strResult);
}
BOOL CGXDateTimeCtrl::SetControlText(ROWCOL nRow, ROWCOL nCol, const CString& strDisplay, UINT nFlags, const CGXStyle* pOldStyle)
{
CGXStyle* pStyle = NULL;
BOOL bSuccess = FALSE;
if (pOldStyle == NULL)
{
pStyle = Grid()->CreateStyle();
Grid()->ComposeStyleRowCol(nRow, nCol, pStyle);
pOldStyle = pStyle;
}
// check for read-only
if (Grid()->IsReadOnly() || pOldStyle->GetIncludeReadOnly() && pOldStyle->GetReadOnly())
bSuccess = FALSE;
// allow only valid input
else
{
// First do this
COleDateTime dt;
if (strDisplay.IsEmpty())
{
if (Grid()->IsCurrentCell(nRow, nCol))
Reset();
bSuccess = Grid()->SetValueRange(CGXRange(nRow, nCol), strDisplay, gxOverride, 0, nFlags);
}
else if (dt.ParseDateTime(strDisplay) && (DATE) dt != 0)
{
SetDateTime(dt);
bSuccess = Grid()->SetValueRange(CGXRange(nRow, nCol), (DATE) dt, gxOverride, 0, nFlags);
}
else
{
// parse the string using the current format
CStringArray strArray;
if (!ParseTextWithCurrentFormat(strDisplay, pOldStyle, strArray))
return FALSE;
SetFormat(m_TextCtrlWnd, *pOldStyle);
int nArrIndex = 0;
for(int i=0; i<m_TextCtrlWnd.m_gadgets.GetSize(); i++)
{
int val = m_TextCtrlWnd.m_gadgets[i]->GetValue();
// s.Empty();
if(m_TextCtrlWnd.m_gadgets[i]->IsKindOf(RUNTIME_CLASS(CGXBDTNumericGadget)))
{
// TRACE(_T("The value %s\n"), strArray[nArrIndex]);
((CGXDTNumericGadget*)m_TextCtrlWnd.m_gadgets[i])->m_nNewValue = _ttoi(strArray[nArrIndex]);
nArrIndex++;
if (nArrIndex>strArray.GetUpperBound())
break;
}
else if(m_TextCtrlWnd.m_gadgets[i]->IsKindOf(RUNTIME_CLASS(CGXBDTListGadget)) && val!=-1)
{
int nIndex = ((CGXDTListGadget*)m_TextCtrlWnd.m_gadgets[i])->FindMatch(strArray[nArrIndex], ((CGXDTListGadget*)m_TextCtrlWnd.m_gadgets[i])->GetValue()+1);
if (nIndex!=-1)
{
// TRACE(_T("The value %s\n"), strArray[nArrIndex]);
((CGXDTListGadget*)m_TextCtrlWnd.m_gadgets[i])->SetValue(nIndex);
nArrIndex++;
if (nArrIndex>strArray.GetUpperBound())
break;
}
}
bSuccess = Grid()->SetValueRange(CGXRange(nRow, nCol), (DATE) m_TextCtrlWnd.GetDateTime(), gxOverride, 0, nFlags);
}
}
}
if (pStyle)
Grid()->RecycleStyle(pStyle);
return bSuccess;
}
We will add an attribute to CGXDateTimeControl in future versions which lets you switch between storing the language independent date (a number) and default date. Then the two overrides will become obsolete.
NOTE: Be sure that you have OG 5.01 or OG 6.0 if you override these methods.
Note: Only the full version of Objective Grid does provide a date-time control.