Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help!!!!!!!!!!!!!!!!!!!!!!!!!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Can someone please Help with dummy code or Guide me on how i can write Three Column Data from database into List Control Box.

Thanks in advance...........
 
You will want to look into

InsertColumn and insert columns 0,1 & 2

And by using SetItemData, InsertItem and/or SetItemText you can add what you need. You can also resize the colums.

Matt
 
Thanks for replying Mat,

I tried following code(test demo before playing with database), i can get column heading but can nat get the items underneath the heading, is there any chancetelling me where i am going wrong please.
code...

#define NUM_COLUMNS 3
#define NUM_ITEMS 2

static _TCHAR *_gszColumnLabel[NUM_COLUMNS] =
{
"PartNo","PartsName","PartsStock"
};

static int _gnColumnFmt[NUM_COLUMNS] =
{
LVCFMT_CENTER,LVCFMT_CENTER,LVCFMT_CENTER
};

static int _gnColumnWidth[NUM_COLUMNS] =
{
150,150,150
};


static _TCHAR *_gszItem[NUM_ITEMS][NUM_COLUMNS] =
{
"P078", "Phone", "2","LM386","Audio Chip","92"
};

void COrderList::OnUpdate()
{
int i, j;
LV_COLUMN lvc;

lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

for(i = 0; i<NUM_COLUMNS; i++)

{
lvc.iSubItem = i;
lvc.pszText = _gszColumnLabel;
lvc.cx = _gnColumnWidth;
lvc.fmt = _gnColumnFmt;
m_showList.InsertColumn(i, &lvc);
}

LV_ITEM lvi;

for(i = 0; i < NUM_ITEMS; i++)
{
lvi.mask = LVIF_TEXT ;
lvi.iItem = i;
lvi.pszText = _gszItem[0];
m_showList.InsertItem(&lvi);
}


}

void COrderList::OnShowlist()
{
OnUpdate();
}
 
You could try something like this


yourList.InsertItem(item_index,_gszItem[0]);
yourList.SetItem(item,1,LVIF_TEXT,_gszItem[1],0,0,0,0);
yourList.SetItem(item,2,LVIF_TEXT,_gszItem[2],0,0,0,0);

This should work for you. the 1 & 2 are sub items representing columns in the SetItem 2nd argument =)

Matt
 
or you can try this:

for( i = 0; i < NUM_ITEMS; i++)
{
lvi.mask = LVIF_TEXT ;
lvi.iItem = i;
lvi.iSubItem = 0;
lvi.pszText = _gszItem[0];
int nItem = m_list.InsertItem(&lvi);
m_list.SetItemText(nItem,1,_gszItem[1]);
m_list.SetItemText(nItem,2,_gszItem[2]);
}
 
Thanks Mats,

BUT DID NOT WORK!,

First i got Debug Assertion Failed with lvi.iSubItem = 0;
I commented this line still i only got column headings as before, and nothing else, any more sugestions, please help...,.

thanks
 
It seems no reason why it did not work. you can add ASSERT(nItem != -1); before you setitemtext to see if you insertitem successfully or not.
or you can try not to use LV_ITEM. Instead you can simply replace it with the statement
int nItem = m_list.InsertItem(i,_gszItem[0]);

Hope you can get it.
 
Sorry mat still did not work!

Tried

for( i = 0; i < NUM_ITEMS; i++)
{
lvi.mask = LVIF_TEXT ;
lvi.iItem = i;
lvi.pszText = _gszItem[0];
int nItem = m_list.InsertItem(&lvi);
ASSERT(nItem!=-1);
m_list.SetItemText(nItem,1,_gszItem[1]);
m_list.SetItemText(nItem,2,_gszItem[2]);
}

got Debug Assertion Failed did not even draw columns

Tried:

for( i = 0; i < NUM_ITEMS; i++)
{
//lvi.mask = LVIF_TEXT ;
//lvi.iItem = i;
//lvi.pszText = _gszItem[0];
int nItem = m_list.InsertItem(i,_gszItem[0]);
m_list.SetItemText(nItem,1,_gszItem[1]);
m_list.SetItemText(nItem,2,_gszItem[2]);
}

commenting lvi or not produced Debug Assertion Failed.

Mat, is there another way of doing this or this it, beacuse once i get this to work i want to try and read a table from database and display two column of that table, i dont know how i am going to get it to work if i can figure this out....

thanks for help..
 
check when you call the onUpdate. maybe at the time you call it the list ctrl has not been properly initialized.

I put the code in the CTestDlg:OnInitDialog(). The Dialog holds the list control.

by the way, what assertion error did you get?
 
Mat,
I had the code into CTestDlg:OnInitDialog()
i had removed the OnClick function, so that i can view the list when the dialog box open.( changed this morning)

Got error

DEBUG ASSERTION FAILED
Winctrl2.cpp
Line 473.

Thanks
 
GOT IT WORKING, THANKS MAT........

it was my proplem, sorry, when drawing the list contol on my dialog box, under properties i ticked box saying ONWER DRAW FIXED, this didn't allow the items to be displayed.....

Now i can start with database....another HEAD AK....

Thanks again.....
 
Same problem!!

can someone please help, i have similar problem as person above, i want to read four coulms of my databse into ctrllist,
reading data is OK!
Prob. passing data to static *_Item[][] = &quot; &quot;
data type TEXT,Long,Long,Text from table.

can someone please put me in right direction or please tell me how i can implement this.

Thank you
 
basically you can set a loop to write items into list control while you are reading data from table.
And you have to use ltoa() to convert long type to a string before you add it into list control.

post your code and say where your problem is. you will get more help.
 
Some please Help haveing proplem with code below....

zen, i have been on same kind of stuff, i am not sure if this can help you
but it sort of does the job for me, it print all table record into CTRLIST
There are few thing that i am not sure off... and i cannot convert one
of my table field, which is a text field. Not sure how they do that..

Can soneone please help, i am trying to pass a Text field to my static
data for list. get error

&quot;cannot convert from 'class CString' to 'char *' &quot;

and this program shows all the records in my table, where am i going wrong
it looks like my if statement's not functioning....


TableSet recSet(pDb);

int recCount=0; // get record count test purpose
char pInSk[10]; //did not work without 10
char pOtSk[10]; // id not work without 10
int nNum = 1; // for number greater than

for( recSet.Open(); !recSet.IsEOF(); recSet.MoveNext() )
{
// only show records that has number more than 1

if (recSet.m_POn >= nNum)
recCount ++;

// Data conversion Lon to Char not sure why there is 10 at the end
// ***** Cannot convert CString to Char **********

_itoa( recSet.m_PInSok, pInSk, 10 );
_itoa( recSet.m_POnOd, pOtSk, 10 );

static _TCHAR *_Item[NUM_ITEMS][NUM_COLUMNS] =
{
&quot;test&quot;,pInSk,pOtSk // get table data
};

// write to list

LV_ITEM lvi;
for(i = 0; i < NUM_ITEMS; i++)

{ lvi.mask = LVIF_TEXT;
lvi.iItem = i;
lvi.iSubItem = 0;
lvi.pszText = _Item[0];
int nItem = m_showList.InsertItem(i,_Item[0]);
ASSERT(nItem!=-1);
m_showList.SetItemText(nItem,1,_Item[1]);
m_showList.SetItemText(nItem,2,_Item[2]);

}
 
I just had a lok at C++ help file and have now got my CString converted using code below.

CString theString(recSet.m_PartNo);
LPTSTR pNo = new TCHAR[theString.GetLength()+1];
_tcscpy(pNo, theString);

but now i have a different problem can someone please help..

when i put list in to the list box all my rows with pNo (converted CString) displays same values for all the rows...
does field needs terminating before the next row is displyed in the listbox..

can someone please help....
 
Can someone please help, same vales in my first column, i had to convert my field from CString to char before writting to column. (other two columns are fine have different value as required.)

conversion code..

CString theString(recSet.m_PartNo);
LPTSTR pNo = new TCHAR[theString.GetLength()+1];
_tcscpy(pNo, theString);


Box 5 2
Box 3 1
Box 6 5

please help what could i be doing wrong...
 
It seems there is no problem with the code you posted. Pls check if you have refreshed recSet.m_PartNo before you write the next row.
 
No i haven't refresh recSet.m_PartNo how can i do this??

Please Help...
 
By refresh, I mean you should get a new data for recSet.m_PartNo and other items from the database every time before you write the data into a new row in the list ctrl.
 
am sorry but i still didn't get the point, because i have got the writing into list code into a loop, where it will loop through the records into table and if any record who's m_PNo is >=1 then write the record details into the list,

only one column has the same value through out while the other two have different values as required...the column that has the same value is the one that i has to convert from CString to Char to add the field into the list..for(

my code loos like...

recSet.Open(); !recSet.IsEOF(); recSet.MoveNext() )
{
// only show records that has >= 1

if (recSet.m_POn >= nNum)
recCount ++;

// Data conversion Lon to Char not sure why there is 10 at the end

_itoa( recSet.m_PInSok, pInSk, 10 );
_itoa( recSet.m_POnOd, pOtSk, 10 );

CString theString(recSet.m_PartNo);
LPTSTR pNo = new TCHAR[theString.GetLength()];
_tcscpy(pNo, theString);


static _TCHAR *_Item[NUM_ITEMS][NUM_COLUMNS] =
{
pNo,pInSk,pOtSk // get table data
};

//write the data

LV_ITEM lvi;
for(i = 0; i < NUM_ITEMS; i++)

{ lvi.mask = LVIF_TEXT;
lvi.iItem = i;
lvi.iSubItem = 0;
lvi.pszText = _Item[0];
int nItem = m_showList.InsertItem(i,_Item[0]);
ASSERT(nItem!=-1);
m_showList.SetItemText(nItem,1,_Item[1]);
m_showList.SetItemText(nItem,2,_Item[2]);

}

//loop back until end of table
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top