Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have student structure and I wanted to store it in list control how can we do this?

see in coding lstStudent is the list object

What I have tried:

struct Student 
{
	char sname[25];
	char id[5];
	char sclass[10];
};
void CStudent::SaveData()
{
	Student s1;
	CString sname,id,sclass,m;
	m_txtStudentName.GetWindowTextW(sname);
	m_txtStudentName.GetWindowTextW(id);
	m_txtClass.GetWindowTextW(sclass);

	sprintf(s1.sname,"%S",sname);
	sprintf(s1.id,"%S",id);
	sprintf(s1.sclass,"%S",sclass);

        m_lstStudent.add(s1);
	}
}
Posted
Updated 13-Feb-17 2:06am

You probably mean CListView Class[^], although you have to add items manually from the structure for each column. Or you could switch to C# which provides data binding.
 
Share this answer
 
Comments
Premnath Mali 16-Feb-17 0:30am    
That was a good suggestion but I wanted to stick with c++
You have to add corresponding columns to your list control which must be in report mode (LVS_REPORT style, usually defined in the resource file). Do this within the initialisation function of the parent window (e.g. OnInitDialog with dialog windows):
C++
m_lstStudent.InsertColumn(0, _T("Name"));
m_lstStudent.InsertColumn(1, _T("ID"));
m_lstStudent.InsertColumn(2, _T("Class"));

Then set the column data after adding a new item:
C++
// Insert new item and set text for first column
int pos = m_lstStudent.InsertItem(0, sname);
// Set text for other columns
m_lstStudent.SetItemText(pos, 1, id);
m_lstStudent.SetItemText(pos, 2, sclass);
 
Share this answer
 
Comments
Premnath Mali 16-Feb-17 0:28am    
Thank you This is working fine but check once SetItemData() if we can use it for the same then comment me again...!
Jochen Arndt 16-Feb-17 3:03am    
SetItemData() sets a user data for an item (a DWORD value). With database applications it can be used to store the numeric ID.

You probably want to use other Set functions like SetItem(). They can be off course used. But then you have to create and initialise an LVITEM structure.
Premnath Mali 17-Feb-17 23:24pm    
How do we do that? Do you have any snippet of that?
I mean you already helped me to do my stuff but what if that student structure has more that 20 variables at that time setting each item manually is not a good way, isn't it?
Jochen Arndt 18-Feb-17 3:00am    
A snippet without knowing how your dara are organised would not really help.

For each field of your structure there will be a column in the list. And you have to set each column (subitem). The only thing to do when setting many sub columns or adding multiple items is disable redrawing while updating the list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900