Click here to Skip to main content
13,197,105 members (60,452 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 21 Dec 2004

A sparse array input control

, 9 Feb 2005
Rate this:
Please Sign up or sign in to vote.
An article on a simple control to enter sparse values or ranges of values in an array.

Sample Image


You may have run into the need of entering sparse values for a two dimensional array. The most common case is that of entering time ranges for each day of the week when a person is available for work.

Searching through CP, I found some interesting work by ClearlyDotNet (see "A Time-Of-Day Picker control"), which provides such functionality, except that by returning TimeSpans, would not allow for a generic control that allows for the entry of non-time values.

What I needed was a control that generated generic output that could be saved to a file, and restored as needed, as well as the ability to dynamically change the row and column definitions while keeping any selected cell that applies in the new row/column set.

Having sometime on my hands, I decided to write my own. This article is the result of my labors, and it is placed here at CP as part of my repayment for some good ideas that I have gotten from other submissions. You can use the source as you wish, just remember that it is public domain, so keep it that way!

Using the code (programmer's view)

You can adapt the control to meet your requirements by changing the following properties:



BackColorThe BackColor property controls the color of the background of the control.
ColumnsThe Columns property controls the data point names, and are displayed across the page. The property accepts a string array. The values are used in the Text property.
CursorColorThe CursorColor property controls the color of the cursor.
FontThe Font property controls the font used for the column labels.
GridColorThe GridColor property controls the color of the grid.
GutterSizeThe GutterSize property controls the gutter size inside each cell of the grid. The value is treated as a percentage of the cell height/width. Note that the cursor uses double the gutter size to ease the telling of where the cursor is at.
RowsThe Rows property controls the data point names, and are displayed down the page. The property accepts a string array. The values are used in the Text property.
SelectedColorThe SelectedColor property controls the color of the selected cells in the grid.

The Text property is an XML string that has one entry per selected cell. The syntax is as follows:


If you set this property to an invalid value, no cells will be selected.

All of the properties are available at design time, and changes are shown immediately.

The control fires one event TextChanged whenever the user changes the selected cells. This event does not fire if the Text property is set programmatically.

Using the code (end user's view)

The end user selects cells by clicking on them. He/she can also select ranges by clicking on the starting cell and then dragging the cursor to the end cell. Note that selecting regions causes the reversal of cell state for those cells already selected.

Points of Interest

The code was developed under Visual Studio 2003, and Framework 1.1. My intention is to share the code so all can use and maybe learn a bit from it. Do not assume that the code is free from bugs, as it was done in a few hours, but feel free to make any changes and/or corrections that you feel appropriate.


Released to the world December 20, 2004.


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

Software Developer eCandidus
United States United States
A random human being, loose on a planet full of other organic beings. Got sidetracked into computer programming as the arpanet, which later became the internet was being tested. If you remember the IBM 1620 Model 1 (whole 4K of core baby!), then you can place me in time, as it was the computer I first played with.

Begun with assembly, moved on to Fortran (no number), and in alpha order: Algol, APLer (for life), Basic, C, C++(when it was a preprocess!), C#, COBOL, Java, Lisp, Pascal, PL/1, POP (Ada-like, not e-mail), Python and RPG. (Sorry, HTML is not a programming language, neither is SQL .. And I have done them both).

Begun with single user (1620), on to centralized (360), shared (VAX), standalone (PC), networked (LAN/Internet), client/server and on to distributed (SQL/IIS/C# Web Services/Ajax/Javascript/Thick Client)

My goal in life is to drive the Oscar Mayer Wienermobile.

You may also be interested in...


Comments and Discussions

GeneralI think you uploaded the wrong thing... Pin
ACorbs7-Feb-05 10:42
memberACorbs7-Feb-05 10:42 
GeneralRe: I think you uploaded the wrong thing... Pin
je_gonzalez10-Feb-05 18:49
memberje_gonzalez10-Feb-05 18:49 

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
Web03 | 2.8.171020.1 | Last Updated 10 Feb 2005
Article Copyright 2004 by je_gonzalez
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid