我想显示的list控件如下:
第一列     第二列
1          1
2          2
3          3 
4          4
5          5
6          6
7          7
8          8
但是一下的代码却显示这样的列表:
第一列     第二列

7          
6           
5
4
3
2
1          8代码:在文件CSortList.cpp中
         ....
m_cList.InsertColumn(0, "第一列", LVCFMT_LEFT, 100);
m_cList.InsertColumn(1, "第二列", LVCFMT_LEFT, 100);
         ....
CString s;
for (int i=0; i<8; i++)
{
s.Format("%d", i+1);
m_cList.InsertItem(0, s);
m_cList.SetItemText(i, 1, s);
}
如上所示,在第二列的1至7行本来应该显示数字的,却是空的,第一列的数字怎么是从大到小呢?这是怎么回事呢?有没有兄弟遇到过这样的情况?

解决方案 »

  1.   

    第一列从达到小:是因为你的列表控件设置了 Sort (排序)
    第二列是空的,是因为你的语句不对,需要再加一个循环才可以
    CString s;
    for (int i=0; i<8; i++)
    {
        s.Format("%d", i+1);
        m_cList.InsertItem(0, s);
        for(int j=1;j<2;j++)
       { 
           m_cList.SetItemText(i, j, s);
        }
    }
      

  2.   

    多谢starytx(某某人)!但是还是不对呀!原来我的ListCtrl并没有设置Sort,只是默认的Sort为None。换成了你的代码还是原来的样子!我觉得嵌套的那个for循环并没有起到作用。
      

  3.   

    for (int i=0; i<8; i++)
    {
    s.Format("%d", i+1);
    m_cList.InsertItem(0, s);
    m_cList.SetItemText(i, 1, s);//这个地方的i应该是0
    }
    因为你插入一个新的第一行,所以只要写到第一行就行了。
      

  4.   

    问题解决了!我是这样解决的:
    CString s;
    for (int i=0; i<8; i++)
    {
    s.Format("%d", i+1);
    m_cList.InsertItem(0, s);
    m_cList.SetItemText(i, 1, s);
    }
    这段代码,修改为
    CString s;
    int a;
    for (int i=0; i<8; i++)
    {
    s.Format("%d", i+1);
    a = m_cList.InsertItem(0, s);
    m_cList.SetItemText(a, 1, s);// 注意这里不再使用i,
                                            //而是使用InsertItemf返回值a!!
    }对于这一点,我在书上看到过。把InertItem的返回值给SetItemText。我以为直接把行号传递给SetItemText就可以了,但是好像这样作才正确!至于为什么我也不知道。还请高手指点呀^_^