Click here to Skip to main content
13,050,429 members (86,768 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 22 Mar 2007

Reusable ListView in C# with textbox and combobox

, 22 Mar 2007
Rate this:
Please Sign up or sign in to vote.
A Reusable ListView in C# with textbox and combobox
Screenshot - image001.jpg


Please don't ask me why I am only posting articles related to List View Controls! Even I wonder about this. In my first article, I had posted some code to create an MFC List Control with Edit Boxes and Combo Boxes. Here I am appearing again, with the same old wine but in C#.NET bottle!

In VC++, the problem was quite easy to solve as there was an API GetSubItemRect. Unfortunately, in C#.NET, there is no such method to get the sub item bounds. But as the Win32 API is still there in the operating system, we can access it through our C# code, and get the sub item coordinates. Once you get it correctly, you can create and display any controls within your ListView.

So, as usual, we have a class derived from ListView, and I gave a sweet, professional name too: ListViewEx.


  1. Editable cells: We can add text boxes in individual cells, individual rows or individual columns.
  2. Combo box in cells: We can add combo boxes in individual cells, individual rows or individual columns.
  3. If subitems are not added for an item, it will be added automatically, while clicking on the item.



About the Code

  • This code contains a reusable class derived from ListView.
  • This class is developed using Microsoft .NET framework 1.1. But it will execute in .NET 2.0 too.
  • Inside the class, a single hash table is used to store the information of the custom cell. By custom cell, I mean a cell that contains a textbox or combo box. The key of the HashTable is the row-column information of the cell. The value is a string collection if the cell is for combo box or null if the cell is for textbox. After getting the clicked item and sub item indices, we will check this hash table for deciding what to display.
  • This code will load user32.dll and invoke the method SendMessage, in order to get the sub item rectangle.

Pre Conditions

  1. ListView's View property should be Detail
  2. ListView's FullRowSelect property should be true

Using the Code

Placing a textbox

To place a text box, use the method AddEditableCell.

void AddEditableCell(int row, int col) 

We have to pass the 0 based index of the item and sub item to this method. For making individual row/column editable, pass -1 as index.


  1. This will place a text box in the cell : 2 X 3
    this.listViewMain.AddEditableCell(1, 2); 
  2. This will place a text box in all the cells of 4th row
    this.listViewMain.AddEditableCell(3, -1); 
  3. This will place a text box in all the cells of 4th column
    this.listViewMain.AddEditableCell(-1, 3); 
  4. This will place a text box in all the cells of the ListView
    this.listViewMain.AddEditableCell(-1, -1); 

Placing a combobox

To place a combo box, use the method AddComboBoxCell.

void AddComboBoxCell( int row, int col, StringCollection data )
void AddComboBoxCell( int row, int col, string[] data )

We have to pass the 0 based indices as well as the data to be displayed inside the combo box.


  1. Create data for combobox
    StringCollection grades = new StringCollection();
    grades.AddRange(new string[]{ "A", "B", "C", "D", "E" });

    This will place a combo box in the cell: 2 X 3
    this.listViewMain.AddComboBoxCell(1, 2, grades); 

  2. This will place a combo box in all the cells of 4th row
    this.listViewMain.AddComboBoxCell (3, -1, grades); 
  3. This will place a combo box in all the cells of 4th column
    this.listViewMain.AddComboBoxCell (-1, 3, grades); 
  4. This will place a combo box in all the cells of the ListView
    this.listViewMain.AddComboBoxCell (-1, -1, grades); 

Configuring ListView to add subitems automatically

If an item is not having many subitems as per the number of columns, this class will add subitems automatically, upon clicking on the item. We can configure this using the property AddSubItem.


If subitem is not added, add it automatically on clicking an item.

this.listViewMain.AddSubItem = true; 

By default, this class will not add subitems automatically. So please set the value to true to make this feature enabled.


That's it! Have a look at the code, use it, and customize as per your wish. I will come again with List View Controls. Let me study some new programming languages.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Shine Kumar
Web Developer
India India
Hi,this is Shine from India, working as a software engineer. I am working mainly with C++ and C#. I like to create reusable stuffs.

You may also be interested in...

Comments and Discussions

QuestionFails to hide the textbox, dropdown box when scrolled Pin
Bhushan A3-Jul-17 5:34
memberBhushan A3-Jul-17 5:34 
General很好用库!非常感谢! Pin
jatq20102-Aug-14 5:09
memberjatq20102-Aug-14 5:09 
QuestionWhat is the event for changed or modified items ? Pin
Member 874505219-Nov-13 0:50
memberMember 874505219-Nov-13 0:50 
QuestionVery nice but... Pin
pierumberto29-Sep-12 4:58
memberpierumberto29-Sep-12 4:58 
QuestionHow to set the state of a column/cell after change the value to not editable textbox ? Pin
Member 874505227-Aug-12 23:31
memberMember 874505227-Aug-12 23:31 
GeneralIt's really nice! Pin
qimingxingwen16-May-12 17:15
memberqimingxingwen16-May-12 17:15 
GeneralReading text from combo or textbox Pin
nordsluttning26-Oct-10 12:06
membernordsluttning26-Oct-10 12:06 
GeneralComboBox moving Pin
Omar.Pessoa17-Feb-10 5:59
memberOmar.Pessoa17-Feb-10 5:59 
QuestionTagging Images in Windows Application C# Pin
nace2k217-Jun-09 4:24
membernace2k217-Jun-09 4:24 
GeneralCropping Images Pin
nace2k216-Jun-09 21:42
membernace2k216-Jun-09 21:42 
Generaldelete combobox Pin
kumi_aberer@hotmail.com17-Mar-09 21:33
memberkumi_aberer@hotmail.com17-Mar-09 21:33 
GeneralRe: delete combobox Pin
Member 188752623-Feb-10 19:13
memberMember 188752623-Feb-10 19:13 
GeneralMy vote of 2 Pin
me_pollack2-Feb-09 17:46
memberme_pollack2-Feb-09 17:46 
QuestionHow to add textbox Pin
Ghelai ALonzo17-Nov-08 22:43
memberGhelai ALonzo17-Nov-08 22:43 
AnswerRe: How to add textbox Pin
Ghelai ALonzo19-Nov-08 16:43
memberGhelai ALonzo19-Nov-08 16:43 
GeneralVery good article Pin
Golllli8-Nov-08 22:01
memberGolllli8-Nov-08 22:01 
Generalhi Pin
sujeeba26-Oct-08 10:13
membersujeeba26-Oct-08 10:13 
QuestionHow can I clear the Hashtable Pin
mykey08153-Jul-08 22:30
membermykey08153-Jul-08 22:30 
Generali want to know the detail of GetSubItemRect, GetKey Pin
fatema7-May-08 21:23
memberfatema7-May-08 21:23 
GeneralExit from cell - little update from me Pin
Josip8410-Apr-08 3:59
memberJosip8410-Apr-08 3:59 
GeneralAfterLabelEdit Pin
Dekkie8-Apr-08 22:10
memberDekkie8-Apr-08 22:10 
GeneralRe: AfterLabelEdit Pin
niczobxor7-Feb-12 2:27
memberniczobxor7-Feb-12 2:27 
QuestionHow to improve this control ? Pin
comebaker20-Aug-07 14:54
membercomebaker20-Aug-07 14:54 
AnswerRe: How to improve this control ? Pin
comebaker23-Aug-07 19:24
membercomebaker23-Aug-07 19:24 
GeneralRe: How to improve this control ? Pin
Shine Kumar27-Aug-07 13:39
memberShine Kumar27-Aug-07 13:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Mar 2007
Article Copyright 2007 by Shine Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid