日期:2014-05-18  浏览次数:20890 次

listview 重画的问题
我想让listview循环从SQL数据表中提取数据,并重画文本显示的位置,但现在只能显示出第一个数据来,并且listview中项的值提取不出来,请高手帮忙解决下。

代码如下:
C# code

listView_qt.Items.Clear();
            SqlConnection conn = BaseClass.DBConn.CyCon();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from T_Ztzl", conn);
            sdr = cmd.ExecuteReader();
            listView_qt.LargeImageList = imageList_qt;
            imageList_qt.ImageSize = new Size(100, 100);

            while (sdr.Read())
            {
                string zt = sdr["Ztzt"].ToString().Trim();
                string s = sdr["Ztmc"].ToString().Trim();
                if (zt.Trim() == "空闲")
                {
                    Image img = new Bitmap(@"D:\Projects\CYAPP\CYAPP\Image\1.bmp");
                    Graphics g = Graphics.FromImage(img);
                    SizeF size = g.MeasureString(sdr["Ztmc"].ToString().Trim(), new Font("黑体", 20));
                    g.DrawString(zt, new Font("黑体", 20), new SolidBrush(Color.White), (img.Width - size.Width) / 2, (img.Height - size.Height) / 2);
                    imageList_qt.Images.Add(img);
                    listView_qt.Items.Add("");
                    s = null;
                }
                else
                {
                    Image img = new Bitmap(@"D:\Projects\CYAPP\CYAPP\Image\2.bmp");
                    Graphics g = Graphics.FromImage(img);
                    SizeF size = g.MeasureString(sdr["Ztmc"].ToString().Trim(), new Font("黑体", 20));
                    g.DrawString(zt, new Font("黑体", 20), new SolidBrush(Color.White), (img.Width - size.Width) / 2, (img.Height - size.Height) / 2);
                    imageList_qt.Images.Add(img);
                    listView_qt.Items.Add("");
                    s = null;
                }
            }
            listView_qt.Items[0].ImageIndex = 0;
            sdr.Close();
            conn.Close();



------解决方案--------------------
listView_qt.Items[0].ImageIndex = 0; 这句话错了 应该0,1,2,3这样的 你记一个index吧!
index=0;
listView_qt.Items[index++].ImageIndex = index++;