I am trying to display SQL result on a list control using MFC

253 views Asked by At

Please, I have list control on, I want to display my query result on a list control. The program runs without error, but it does not display the SQL result on the list control.

BOOL CClassDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // TODO:  Add extra initialization here
    CString DSN;
    DSN = _T("DRIVER=SQL Server;SERVER=DESKTOP-
DICUCDS\\SQL2K14;Trusted_Connection=Yes;APP=Microsoft\x00ae Visual Studio\x00ae 2013;WSID=DESKTOP-
DICUCDS;DATABASE=School");

    CDatabase aDB; 
    try {
        aDB.OpenEx(DSN);  

        CRecordset aRS(&aDB); 
        aRS.Open(CRecordset::forwardOnly, (L"SELECT DISTINCT Myclass FROM MyFacts"));  

        // populate Grids
        ListView_SetExtendedListViewStyle(m_classlist, LVS_EX_GRIDLINES);

        // Column width and heading
        m_classlist.InsertColumn(1, L"Class", LVCFMT_LEFT, -1, 1);

        m_classlist.InsertColumn(2, L"Age", LVCFMT_LEFT, -1, 1);
        m_classlist.SetColumnWidth(0, 120);
        m_classlist.SetColumnWidth(1, 200);
        m_classlist.SetColumnWidth(2, 200);

        while(!aRS.IsEOF())   
        {
            CString strValue;
            aRS.GetFieldValue(L"Myclass",  strValue);
            m_classlist.SetItemText(-1, 1, strValue);
            //strValue.AddString(strValue);

            aRS.MoveNext();
        }
        aRS.Close();
        aDB.Close();
    }
    catch (CDBException * ex)
    {
        TCHAR buf[255];

        ex->GetErrorMessage(buf, 255);

        CString strPrompt(buf);
        AfxMessageBox(strPrompt);
    }

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

void CClassDialog::ResetListControl()
{
    m_classlist.DeleteAllItems();
    int iNbrOfColumns;

    CHeaderCtrl* pHeader = (CHeaderCtrl*)m_classlist.GetDlgItem(0);

    if (pHeader) {
        iNbrOfColumns = pHeader->GetItemCount();
    }

    for (int i = iNbrOfColumns; i >= 0; i--) {
        m_classlist.DeleteColumn(i);
    }
}
1

There are 1 answers

3
Andrew Truckle On

Are you sure this is right? m_classlist.SetItemText(-1, 1, strValue);

If you research the official documentation for the CListCtrl (for example, here) you will see:

CString strText;

int nColumnCount = m_myListCtrl.GetHeaderCtrl()->GetItemCount();

// Insert 10 items in the list view control.
for (int i = 0; i < 10; i++)
{
    strText.Format(TEXT("item %d"), i);

    // Insert the item, select every other item.
    m_myListCtrl.InsertItem(LVIF_TEXT | LVIF_STATE, i, strText,
        (i % 2) == 0 ? LVIS_SELECTED : 0, LVIS_SELECTED, 0, 0);

    // Initialize the text of the subitems.
    for (int j = 1; j < nColumnCount; j++)
    {
        strText.Format(TEXT("sub-item %d %d"), i, j);
        m_myListCtrl.SetItemText(i, j, strText);
    }
}

You are making references to SetItemText but you have not actually added any elements into the list. The code before shows an example:

// Insert the item, select every other item.
m_myListCtrl.InsertItem(LVIF_TEXT | LVIF_STATE, i, strText,
    (i % 2) == 0 ? LVIS_SELECTED : 0, LVIS_SELECTED, 0, 0);

I am not saying that you have to use that specific set of paramaters. But the point is that you must insert an item into the list before you can update it's properties. Or even set the properties at the moment you add it into the list.