Editable ListView Replacement for C#

Edited by

Microsoft ListView control isn't fully editable!

If you ever used the C# ListView control in report mode to emulate data grids, you already know that ListView does not allow you to do many things your users need. The most annoying of them is that at some time 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.

Our 10Tec grid called 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 in C#:

iGrid as an editable ListView replacement in C#.

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:

listView1.LabelEdit = true;

But you cannot enable editing of ListView subitems with another similar property setting. You need to you use extra code snippets to implement subitems editing and think how you will dupplicate this code for every ListView that needs to be edited.

There are plenty of examples in the Internet demonstrating the same basic technique of editing ListView subitems in C#. What authors of those solutions do is that they place an additional textbox control over the clicked ListView subitem, copy the subitem's text to the textbox and activate it; then they 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 in C# does not provide you with a scrolling event you could use for this purpose. To solve this problem, you need to process the 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 - but not with the items/subitems ideology like in ListView. The iGrid syntax of accessing cell values is easier and 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 numeric or date values, or values of any data type available in C# - 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. We have two versions of iGrid - one is an ActiveX grid and the other is a WinForms grid for .NET Framework. This means you can choose the required version of 10Tec grid control to be a powerful VB6/VBA ListView replacement for the classic VB (Visual Basic 6) or VBA, or use it in WinForms with C# or VB.NET or any other .NET-compatible language. All modern versions of the Windows OS, including Windows 7 and Windows 10, are fully supported too. All this makes iGrid a first-class editable ListView replacement for C#/VB.NET or many other popular programming languages.

Detailed info about WinForms grid iGrid.NET »

Detailed info about ActiveX grid iGrid »