2012年10月1日 星期一

c# hide and show column in a listview

  1. Add a button to the form. Name the button as buttonHideColumn.
  2. Add a button to the form. Name the button as buttonShowColumn.
  3. Then add following code. Change the value of the variable freezeColumn and i to freeze the user specified column. Column index start from 1. These two variables freezeColumn and i must be the same.
  4. int freezeColumn = 2;
            int i = 2;
    
            private void buttonHideColumn_Click(object sender, EventArgs e)// hide
            {
                
                if (i < listView1.Columns.Count)
                {
                    if (i >= freezeColumn)
                    {
                        listView1.Columns[i].Width = 0;
                    }
                    i++;
                }
    
                Console.WriteLine("<<<" + i);
            }
    
            private void buttonShowColumn_Click(object sender, EventArgs e)// show
            {
                if (i > freezeColumn)
                {
                        i--;
                        listView1.Columns[i].Width = -2;
                    
                }
                Console.WriteLine(">>>" + i);
            }
    

沒有留言: