C#之常用控件集合(英文版)

The ListView control is an ItemsControl that is derived from ListBox.

C#之常用控件集合(英文版)_第1张图片

Add Columns in ListView

You can add columns in Listview by using Columns.Add() method. This method takes two arguments, first one is the Column heading and second one the column width.

listView1.Columns.Add("ProductName", 100);

In the above code, "ProductName" is column heading and 100 is column width.

Add Item in Listview

You can add items in listbox using ListViewItem which represents an item in a ListView control.

    string[] arr = new string[4];    ListViewItem itm;//add items to ListView    arr[0] = "product_1";    arr[1] = "100";    arr[2] = "10";    itm = new ListViewItem(arr);    listView1.Items.Add(itm);

Get selected item from ListView

productName = listView1.SelectedItems[0].SubItems[0].Text;

Above code will return the itme from first column of first row.

Sorting Listview Items

If the Sorted property of Listview is set to true, then the ListView items are sorted. The following code sorts the ListView items:

ListView1.Sorted = true;

Add Checkbox in Listview

You can add checkbox in Listview columns.

myListView.CheckBoxes = true;myListView.Columns.Add(text, width, alignment);

C#之常用控件集合(英文版)_第2张图片

ListView provides a large number of properties that provide flexibility in appearance and behavior. The View property allows you to change the way in which items are displayed. The SelectionMode property for a ListView determines how many items a user can select at one time.

The following C# program first set its view property as Details and GridLines property as true and FullRowSelect as true.

listView1.View = View.Details;listView1.GridLines = true;listView1.FullRowSelect = true;

Finally at the button click event, it will display the selected row values in a message box.



Download Source Code
Print Source Code

using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.FullRowSelect = true;

            //Add column header
            listView1.Columns.Add("ProductName", 100);
            listView1.Columns.Add("Price", 70);
            listView1.Columns.Add("Quantity", 70);

            //Add items in the listview
            string[] arr = new string[4];
            ListViewItem itm ;

            //Add first item
            arr[0] = "product_1";
            arr[1] = "100";
            arr[2] = "10";
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);

            //Add second item
            arr[0] = "product_2";
            arr[1] = "200";
            arr[2] = "20";
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string productName = null;
            string price = null;
            string quantity = null;

            productName = listView1.SelectedItems[0].SubItems[0].Text;
            price = listView1.SelectedItems[0].SubItems[1].Text;
            quantity = listView1.SelectedItems[0].SubItems[2].Text;

            MessageBox.Show (productName + " , " + price + " , " + quantity);
        }
    }
}

  • C# Visual Studio IDE
  • How to Create a C# Windows Forms Application
  • C# Label Control
  • C# Button Control
  • C# TextBox Control
  • C# ComboBox
  • C# ListBox Control
  • C# Checked ListBox Control
  • C# RadioButton Control
  • C# CheckBox Control
  • C# PictureBox Control
  • C# ProgressBar Control
  • C# ScrollBars Control
  • C# DateTimePicker Control
  • C# Treeview Control
  • C# Menu Control
  • C# MDI Form
  • C# Color Dialog Box
  • C# Font Dialog Box
  • C# OpenFile Dialog Box
  • C# Print Dialog Box
  • keyPress event in C# , KeyDown event in C# , KeyUp event in C#
  • How to create Dynamic Controls in C# ?
  • Keep Form on Top of All Other Windows
  • C# Timer Control



  • An overview of Microsoft C#
  • C# Language Tutorial
  • C# Statements Tutorial
  • C# Graphical User Interface Tutorial


  • C# Collection Tutorial
  • C# String Tutorial
  • C# File Operations Tutorial
  • C# Excel Tutorial
  • C# Crystal Reports Tutorial
  • CSharp Communication Tutorial
  • C# Ado.Net Tutorial and Source Code , c# database programming tutorial
  • C# ADO.NET data Providers Tutorial
  • C# Dataset Tutorial
  • C# DataAdapater Tutorial
  • Csharp DataView Tutorial
  • Csharp Remoting Tutorial
  • C# XML Tutorial
  • C# DataGridView Tutorial


你可能感兴趣的:(ListView,C#,C#常用控件使用方法集合)