conversion of long to LPCTSTR

void CODBCView::OnDraw(CDC* pDC)
{
      // TODO: Add your specialized code here and/or call the base class
      CRect oRect;
      CString m_strPartID;
      long iRowCount = 0;

      GetClientRect(oRect);
      m_DBGrid.MoveWindow(oRect, TRUE);
      m_DBGrid.RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);

      m_DBGrid.SetCols(m_pSet->m_nFields);
      m_DBGrid.SetRows(m_pSet->GetRecordCount() + 1);

      m_DBGrid.SetColWidth(-1, 1440);

      m_DBGrid.SetRow(iRowCount);
      m_DBGrid.SetCol(0);
      m_DBGrid.SetText("Part ID");
      m_DBGrid.SetCol(1);
      m_DBGrid.SetText("Part Name");
      m_DBGrid.SetCol(2);
      m_DBGrid.SetText("Part Description");

      m_pSet->MoveFirst();

      iRowCount += 1;

      while (!m_pSet->IsEOF()) {
            m_DBGrid.SetRow(iRowCount);
            m_DBGrid.SetCol(0);      
            m_DBGrid.SetText((LPTSTR)m_pSet->m_PartID);
            m_DBGrid.SetCol(1);
            m_DBGrid.SetText(m_pSet->m_PartName);
            m_DBGrid.SetCol(2);
            m_DBGrid.SetText(m_pSet->m_PartDescription);
            m_pSet->MoveNext();
            iRowCount += 1;
      }
      
      m_pSet->MoveFirst();

      m_DBGrid.SetRow(1);
      m_DBGrid.SetCol(1);
}

I have an Access97 database. One of my fields is PartID (of autonumber type). How do I convert PartID to LPCTSTR so that it can be displayed using the built-in "SetText()" function in Visual C++.

I have another question. Is there any way to do currency formatting in Visual C++. For example,

m_DBGrid.SetText(m_pSet->m_UnitPrice);

where UnitPrice is of type currency. I wish to format it to display up to 2 decimal places.

ervinlohAsked:
Who is Participating?
 
abdijConnect With a Mentor Commented:
Hi,

 Here is probably what you need.
 
For normal strings:

 char *_ltoa( long value, char *string, int radix );

For Wide Character Strings:
wchar_t *_ltow( long value, wchar_t *string, int radix );

Routine Required Header Compatibility
_ltoa <stdlib.h> Win 95, Win NT
_ltow <stdlib.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns a pointer to string. There is no error return.

Parameters

value

Number to be converted

string

String result

radix

Base of value

Remarks

The _ltoa function converts the digits of value to a null-terminated character string and stores the result (up to 33 bytes) in string. The radix argument specifies the base of value, which must be in the range 2 – 36. If radix equals 10 and value is negative, the first character of the stored string is the minus sign (–). _ltow is a wide-character version of _ltoa; the second argument and return value of _ltow are wide-character strings. Each of these functions is Microsoft-specific.

Generic-Text Routine Mappings

Bye.
Abdij
Feel free to ask
0
 
kishk91Commented:
Hi.. can you please tell me what type is the m_DBGrid.SetText("Part ID");
i mean.. is it a UNICODE string like LPWSTR or something else???
Regards
kishk91
0
 
abdijCommented:
Hi,
 Probably here is what you need to do,

 // you have the long PartId.
// have this in the header
// init to null in constructor
 LPSTR  m_lpszPartId ;

// and then allcate memory
m_lpszPartId = new char[1000];

// convert long to string
m_lpszPartId = _ltoa(PartId, m_lpszPartId, 10);

// display
m_DBGrid.SetText(m_lpszPartId);

**************************
Regarding your second question there should be no problem displaying the $ but other currencies display i do not know. Will try and find.

Bye
Abdij
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
gvgCommented:
This is way to simple.

First use

m_strPartID.Format( "%ld", m_pSet->m_PartID );

then you call SetText( m_strPartID );

You can use the format function to format currency

m_strPrice( "$%.2f", m_pSet->m_UnitPrice );

This is way easyer than what you have received before.

If this is the best answer you can accept the comment as answer.

Hope this helps

0
 
gvgCommented:
A typo in my erlier comment.
Should be.

m_strPrice.Format( "$%.2f", m_pSet->m_UnitPrice );

If you want I can explain how CString.Format works or you can check the help.

0
 
ervinlohAuthor Commented:
Thanks qvq and abdij.

To qvq:
I have checked through the MSDN Visual C++ help for the explanation. Thanks anyway.
0
All Courses

From novice to tech pro — start learning today.