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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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,

 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.