Editable ListView for C#/VB.NET

by Igor Katenov, the lead developer at 10Tec

Microsoft ListView control isn't fully editable!

If you ever used the Microsoft ListView control in report mode to emulate data grids, you already know that ListView has some annoying drawbacks. At some point you may need editable ListView with the ability to edit all strings displayed in it, but editing ListView subitems is not possible out-of-the-box.

The 10Tec grid control, iGrid, looks like ListView in report mode and allows you to edit its cells using several built-in editors. From this point of view, it can be used as an editable ListView replacement for C# and VB.NET:

iGrid as an editable ListView replacement for C#/VB.NET.

How to edit ListView subitems in C#

You can enable inline editing of ListView items in C# simply by setting ListView's LabelEdit property to True (all ideas from this article also work in VB.NET after corresponding syntax changes):

listView1.LabelEdit = true;

But you cannot enable editing of ListView subitems by setting a similar property. You need extra code snippets to implement subitems editing and think how to reuse this code for every editable ListView in your app.

There are plenty of examples in the Internet demonstrating the same basic technique of editing ListView subitems in C# or VB.NET. The idea of those solutions is simple: place an additional textbox control over the clicked ListView subitem, copy the subitem's text to the textbox and activate it. There should be also event handlers to process the Enter/Esc keys and mouse clicks outside of the textbox editor to commit or cancel editing of the ListView subitem.

The idea is right, but in the most cases the authors forget about possible scrolling in ListView: if the user is scrolling the control, the textbox editor must be hidden too. Unfortunately, the ListView control does not provide you with a scrolling event you could use for this purpose. To solve this problem, you should process specific Windows messages. Below is a link to a Stackoverflow post containing a full editable ListView solution for C#:

C#: How do you edit items and subitems in a listview?

This solution is robust enough. Note that it is based on another derived C# ListView control providing the Scroll event (you can find a link to this solution in Nemesis's answer). And you also need to have an additional hidden textbox control on your form or create it on the fly when the user is about to edit a ListView subitem.

ListView flicker effect and more problems

ListView flickering is another well-known problem of this standard Microsoft control. There are workarounds to eliminate the ListView Flicker effect in particular situations or development environments, but it seems there is no universal solution for all cases.

Some other problems of ListView are the followings:

  • You cannot sort or group ListView items.
  • Every ListView item uses the same color and font.
  • ListView is slow when you work with big amounts of data.

A few words about coding

In iGrid you deal with a cell matrix indexed by row and column indices - in contrast to the items/subitems ideology of ListView. The iGrid syntax of accessing cell values is more intuitive when you work with tabular data because you access every cell using the Cell[<row>, <column>] syntax like in a two-dimensional array. If we use the C# syntax, your ListView calls like

listView1.Items[1].SubItems[2] = "some text";

are transformed into something more understandable:

iGrid1.Cells[1, 3].Value = "some text";

In iGrid you can use string keys to access rows and columns, and the calls above can be even more understandable if you use this feature:

iGrid1.Cells["row_key", "description"].Value = "some text";

Or you can even change the background color of an individual cell easily (compare to a ListView subitem!):

iGrid1.Cells[1, 3].BackColor = Color.IndianRed;

Many everyday ListView tasks are implemented in code easily with iGrid. For instance, the following code was used to dynamically highlight rows with sales greater than or equal to 50,000 in the ListView at the top of the article above:

private void iGrid1_CellDynamicFormatting(object sender, iGCellDynamicFormattingEventArgs e)
   if ((int)iGrid1.Cells[e.RowIndex, "Total Sales"].Value >= 50000)
      e.BackColor = Color.FromArgb(224, 240, 240);

And if you want to code ListView editing of items and subitems in iGrid, you should do nothing (really, no code) as cell editing works out-of-the-box!

What you get if you replace ListView with iGrid

If you replace ListView with iGrid, you provide your users with the following important features:

  • iGrid's cells can store values of any data type available in C# or VB.NET (numbers, dates, etc.) - in contrast to ListView items/subitems which are only strings.
  • If a cell is truncated, the full cell text can be displayed in the built-in tooltip when the user hovers the mouse pointer over the truncated cell.
  • The main iGrid built-in cells editors are textbox editor, drop-down list (combo box) and check box.
  • ListView does not have frozen (fixed) columns or rows, but iGrid does.
  • iGrid provides you with built-in multicolumn sorting and grouping.
  • Any column of your grid can be a treeview column!

Supported platforms

To wrap up, some words about the supported OS versions and development platforms 10Tec iGrid is intended for.

10Tec Company provides two versions of iGrid. One is an ActiveX grid for the classic VB platform (Visual Basic 6, Microsoft Office VBA). The other is a WinForms grid for .NET Framework. The WinForms version of iGrid can be used with C#, VB.NET or any other .NET-compatible language.

All modern versions of the Windows OS, including Windows 7 and Windows 10, are fully supported. The iGrid control automatically gets the look-and-feel of the OS and is drawn with the OS visual styles if they are turned on. If required, this feature can be turned off to provide the same look (flat, 3D or a custom one) in all versions of Windows regardless of visual styles availability.

All this makes iGrid a first-class editable ListView replacement for C#/VB.NET and many other popular programming languages.

Detailed info about WinForms grid iGrid.NET »

Detailed info about ActiveX grid iGrid »