[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

fill listctrl from database

Hi,all!
Now I try to make my question as clear as possible:

I have a database from ACCESS,my.mdb

the mdb only contains one table whose record has this format.

ID | Name | Data | City1 | City2


and I allready fill a few records by hand in ACCESS.

O.K ,now I construct a MFC-WIZARD database application(linking to this .mdb) and I add a ListCtrl(in report mode) into the dialogue-based-view.

I'v added 4 column-head (i.e:name,city1,city2,date) into the ListCtrl except the ID in the database table.

Now my question is:

How can I fill each corresponding row by the data from the database?(one row in listctrl is a record in .mdb)

FYI:
In my application,the auto-generated crecordset has 5 viarables refering to each column in the .mdb,i think you know this.
But How I use it to fill the ListCtrl?
I dont know how to specify each item in the listctrl and fill it.

Dont be afraid to answer or comment.If I can get a useful idea from you ,I will award your certain points even another people answer it:)
Thanx
Thanx
Thanx
HuangYun.












0
huangyun
Asked:
huangyun
  • 4
  • 2
  • 2
  • +3
1 Solution
 
MDarlingCommented:
hi, try this...

for (int i=0;i<rows_in_db;i++)
{
   // some code here to fill
   // strings strID,strName, etc
   // from db record

   m_ListCtrl.InsertItem(i,strID);
   m_ListCtrl.SetItemText(i,1,strName);
   m_ListCtrl.SetItemText(i,2,strData);
   m_ListCtrl.SetItemText(i,3,strCity1);
   m_ListCtrl.SetItemText(i,3,strCity2);
}

hope this helps,
regards
Mike.
0
 
MDarlingCommented:
addendum - make sure the listctrl is in report mode LVS_REPORT style

sorry misread the bit about no ID

just start with InsertItem(i,strName)

and follow on with SetItemText(i,1,strData)

etc...

regards,
Mike.
0
 
huangyunAuthor Commented:
for (int i=0;i<rows_in_db;i++)
{
   // some code here to fill
   // strings strID,strName, etc
   // from db record

   m_ListCtrl.InsertItem(i,strID);
   m_ListCtrl.SetItemText(i,1,strName);
   m_ListCtrl.SetItemText(i,2,strData);
   m_ListCtrl.SetItemText(i,3,strCity1);
   m_ListCtrl.SetItemText(i,3,strCity2);
}

I dont know how to move strID,strName,strData point to the next record and third and so on.

I reject for more info .After I overcome this problem,if your help helpful,I will establish a empty question and you simply tick it.
Thank you.


0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MDarlingCommented:
Have you mapped a class to the table in your database?

using DAO or OleDB or ODBC?

regards,
Mike.

0
 
MDarlingCommented:
you say you have a generated record set

you should have something like

CMyRecordSet mySet;

mySet.MoveFirst();
while(mySet.IsEOF()==FALSE)
{
  strName=mySet.GetFieldValue("Name");
  strCity1=mySet.GetFieldValue("City1");

  mySet.GetNext();
}

etc......

regards,
Mike.

0
 
WynCommented:
huangyun,the comment seems to be right.
You should try it
Regards
Wyn
0
 
simongodCommented:
i had the same problem here is a snip it of code from my program.  This is the way that I did it.

first off you have to set a few variables

CDaoDatabase db;
COleVariant covVal;
CDaoRecordset rec(&db);

the next thing that you need to opening the database with the following code.

rec.Open(dbOpenDynaset, SELECT * FROM Parameter WHERE Parameter!Name '*'", 0);

In the line of code above the two occurances of Parameter is the name of the table you want to open.  The Name occurance is the first field you look at.
      m_database.DeleteAllItems();

this next line is to delete anything that may already be in the listctrl.

m_database is of type CListCtrl.  Which is your listctrl

            x=0;  //counter
            while(!rec.IsEOF())
            {
                  //Name Field
                  rec.GetFieldValue(_T("Name"), covVal);
                  strcpy(Name.GetBuffer(56), (char*)covVal.bstrVal);
                  m_database.InsertItem(x, Name);
                  
                  //X Cord Field
                  rec.GetFieldValue(_T("X Cord"), covVal);
                  XCord.Format("%.3f", covVal.fltVal);
                  m_database.SetItem(x, 1, LVIF_TEXT, XCord, 0, 0, 0, 0);
                  
                  //Y Cord Field
                  rec.GetFieldValue(_T("Y Cord"), covVal);
                  YCord.Format("%.3f", covVal.fltVal);
                  m_database.SetItem(x, 2, LVIF_TEXT, YCord, 0, 0, 0, 0);
                              
                  //Blocks Field                                                
                  rec.GetFieldValue(_T("BlockName"), covVal);
                  if(covVal.vt == VT_NULL)
                        strcpy(BlockName.GetBuffer(56), "?");
                  else
                        strcpy(BlockName.GetBuffer(56), (char*)covVal.bstrVal);
                  m_database.SetItem(x, 3, LVIF_TEXT, BlockName, 0, 0, 0, 0);

                  //Direction field
                  rec.GetFieldValue(_T("Direction"), covVal);
                  if(covVal.vt == VT_NULL)
                        strcpy(Direction.GetBuffer(12), "?");
                  else
                        strcpy(Direction.GetBuffer(12), (char*)covVal.bstrVal);
                  m_database.SetItem(x, 4, LVIF_TEXT, Direction, 0, 0, 0, 0);

                  x++;
                  rec.MoveNext();      
            }
rec.Close();

The rest is straight forward and I will try and help you with the code that I have listed if you need any additional help.  If you can't understand what I have please send me your code and I will fix it for you OR I can send you a better example of the code that I have listed above.

asulwer@quadplusinc.com
0
 
huangyunAuthor Commented:
thank,simongod .
Would you please mail a workable sample containing code above ?

ilovehuangyun@163.net

Thanx again!!!
0
 
V_BapatCommented:
simongod has given the solution which uses dynamic binding. I would suggest you to use RFX method, since the data is just to be populated into the listctrl. See TN053 in MSDN help for details.
If you are using Access database, it is preferable to use DAO classes. You need not even create a DB application. Just create a SDI or a dialog based app. If you create a dialog based app, insert a listctrl in your dialog.
Now, derive a class from CDaoRecordset. You will be required to specify the mdb file and the table. On doing this, you will get member variables for each field of the database. Now, you are ready to fill the records in the listctrl.
Lets us say this is your db structure:
Field Name        Type       Member
ID                   dbText       m_ID
Name             dbText       m_Name
Data               dbText       m_Data
City1              dbText        m_City1
City2              dbText        m_City2

In OnInitDialog(for dialog based app) or OnUpdate(for SDI app) of the view, you have to populate the listctrl.

Since you have member variables for all the fields, you can use them directly instead of calling SetFieldValue and GetFieldValue.
Just try this:

CDaoDatabase db;
db.Open(/*file name*/);
CMyRecordset rs(&db);  // CMyRecordset is derived from CDaoRecordset
rs.Open();
int nItem = 0;

while(!rs.IsEOF())
{
m_ListCtrl.InsertItem(nItem,m_ID);
m_ListCtrl.SetItemText(nItem,1,m_Name);
m_ListCtrl.SetItemText(nItem,2,m_Data);
m_ListCtrl.SetItemText(nItem,3,m_City1);
m_ListCtrl.SetItemText(nItem,4,m_City2);

nItem++;
rs.MoveNext();
}

rs.Close();
db.Close();

That is it. Hope this helps.

Vicky
0
 
carldeanCommented:
Admittedly I was connecting to an Informix db using ODBC but I managed to write a CListCtrl derived class that you gave a piece of SQL to and it did the rest - it even generated and added column headings.

If you are interested in this then let me know and I can mail you the source code.
0
 
simongodCommented:
no i tryed this and it was confusing and harder to understand.  I am a beginner but my method worked and its easier to understand.  If he is a beginner then that would be the best way to do it for now.  Please be advised that v Bapat method is the best way to do it but you should read a book or it should be explain step by step for a beginner.  When I used the method above I had to pick up a book to read more about it to make it work.  My method was given to me by one of the top experts here in this catagory.  VinExpert gave it to me.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now