C# ListView控件的间隔色和自动适应宽度

       自动适应宽度只须设置列宽的值等于-1即可,另外,如果要调整每列的宽度为标题的宽度的话设置列宽的值等于-2即可,要注意一定要在ListView加载了数据之后再设置,要不然就没效果了,而且每次刷新数据后必须设置。至于间隔色就很简单了,在每行要加载数据的时候设置每行的背景色就搞定了,下面我写一个很很简单的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ReadWriteFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ListViewItem lstItem = null;
            string stState = string.Empty;

            //行和列是否显示网格线
            listView1.GridLines = true;
            //单选时,选择整行
            listView1.FullRowSelect = true;
            //显示方式
            listView1.View = View.Details;
            //没有足够的空间显示时,是否添加滚动条
            listView1.Scrollable = true;
            //是否可以选择多行
            listView1.MultiSelect = false;

            listView1.Columns.Add("lstFileName", "文件名");
            listView1.Columns.Add("lstNoteBook", "工作薄");
            listView1.Columns.Add("lstArea", "区域");
            listView1.Columns.Add("lstState", "导入状态");

            for (int i = 0; i < 12; i++)
            {
                lstItem = new ListViewItem();
                if (i % 2 == 0)
                {
                    lstItem.BackColor = Color.Green;
                    stState = "成功";
                }
                else
                {
                    lstItem.BackColor = Color.Red;
                    stState = "失败";
                }
                lstItem.SubItems[0].Text = "《现代熟女故事》 ";
                lstItem.SubItems.Add("最初的炙热过后,感情渐渐变地像鸡肋。");
                lstItem.SubItems.Add("是守着属于自己的鸡肋还是冒险开始一段新感情?");
                lstItem.SubItems.Add(stState);

                listView1.Items.Add(lstItem);
            }

            //自动适应宽度,-1根据内容设置宽度,-2根据标题设置宽度.
            listView1.Columns["lstFileName"].Width = -1;
            listView1.Columns["lstNoteBook"].Width = -1;
            listView1.Columns["lstArea"].Width = -1;
            listView1.Columns["lstState"].Width = -1;
            listView1.Columns["lstFileName"].Width = -2;
            listView1.Columns["lstNoteBook"].Width = -2;
            listView1.Columns["lstArea"].Width = -2;
            listView1.Columns["lstState"].Width = -2;
        }
    }
}


来自:http://www.cnblogs.com/stephen-vv/archive/2011/12/08/2281318.html

posted @ 2012-01-19 14:30  java消费保护  阅读(190)  评论(0编辑  收藏  举报