Click here to Skip to main content
15,914,895 members
Articles / Programming Languages / C#
Article

C# Grid

Rate me:
Please Sign up or sign in to vote.
3.60/5 (36 votes)
11 Dec 2003 281.2K   3.6K   69   39
An implementation of a grid in C#

Image 1

Introduction

Before we begin, this grid is in it's infancy and I hope with collaboration from other CodeProject members we can make this grid as functional as the one you would find in Microsoft Excel, in respect of that I've kept the article content to a minimum, expect it grow in the coming months.

Expect bugs and lack of functionality these items will be addressed in future releases.

What's missing:

  • Image alignment in cells
  • Limited events
  • Specialized Cells
  • A whole raft of other stuff

If you would like to contribute to this project, email me using the discussion thread below. I'm looking really for 2 other people who would have good GUI and design skills.

Design

The grid is primary to be used as a control and could be optionally used a straight class. Lets take a look at a simplistic class design.

Image 2

I've excluded properties and methods here, these will be documented at a later stage.

Example of use

C#
virtual public void Demo()
{

    gridCtrl.LockUpdates = true;

    for (int r=0; r < 30; r++)
    gridCtrl.AddRow();

    for (int c=0; c < 30; c++)
        gridCtrl.AddColumn();

    int ii=0;

    int i=255;
    for (int r=1;r < gridCtrl.rowList.Count;r++)
    {
        i = 255;
        for (int c=1;c < gridCtrl.colList.Count;c++)
        {
            gridCtrl.GetCell(r,c).Value = ii++;
            gridCtrl.GetCell(r,c).BackColor = Color.FromArgb(i,i,255);;
            i-=5;
        }
    }

    gridCtrl.SetFixedRowCount(1);
    gridCtrl.SetFixedColumnCount(1);


    gridCtrl.GetRow(2).Size = 40;
    gridCtrl.GetColumn(1).Size = 180; 
    gridCtrl.GetRow(8).Visible = false;

    gridCtrl.GetRow(17).Size = 40;

    gridCtrl.GetCell(1,4).BackColor = Color.Cornsilk;
    gridCtrl.GetCell(2,4).BackColor = Color.CornflowerBlue;
    gridCtrl.GetCell(3,4).BackColor = Color.Coral;
    gridCtrl.GetCell(4,4).BackColor = Color.CadetBlue;

    gridCtrl.GetCell(1,1).HorizontalAlignment = 
      Cell.HorizontalAlignmentType.Left;
    gridCtrl.GetCell(1,1).Value = "Left";
    gridCtrl.GetCell(1,1).TextColor = Color.Blue;
    gridCtrl.GetCell(1,1).FontName = "System";

    gridCtrl.GetCell(2,1).HorizontalAlignment = 
      Cell.HorizontalAlignmentType.Center;
    gridCtrl.GetCell(2,1).Value = "Center";
    gridCtrl.GetCell(3,1).HorizontalAlignment =
      Cell.HorizontalAlignmentType.Right;
    gridCtrl.GetCell(3,1).Value = "Right";

    gridCtrl.GetCell(4,1).VerticalAlignment = 
      Cell.VerticalAlignmentType.Top;
    gridCtrl.GetCell(4,1).Value = "Top";
    gridCtrl.GetCell(5,1).VerticalAlignment = 
      Cell.VerticalAlignmentType.Center;
    gridCtrl.GetCell(5,1).Value = "Center";
    gridCtrl.GetCell(6,1).VerticalAlignment = 
      Cell.VerticalAlignmentType.Bottom;
    gridCtrl.GetCell(6,1).Value = "Bottom";

    gridCtrl.GetCell(2,2).FontStyle = FontStyle.Bold;
    gridCtrl.GetCell(2,3).FontStyle = FontStyle.Italic;
    gridCtrl.GetCell(2,4).FontStyle = FontStyle.Underline;
    
    gridCtrl.GetCell(12,12).TipText = "Cell";


    Cell cell = gridCtrl.GetCell(7,1);
    cell.TextColor = Color.Yellow;
    cell.BackColor = Color.Black;
    cell.Value = "Funny";



    gridCtrl.GetRow(4).CanResize = false;
    gridCtrl.GetColumn(4).CanResize = false;


    gridCtrl.GetCell(9,1).FontName = "Wingdings";
    
    gridCtrl.GetCell(17,1).Image = new Bitmap(GetType(), "image1.bmp");

    gridCtrl.LockUpdates = false;
    gridCtrl.AdjustScrollbars();
    Invalidate();
}

History

  • 1.0 - 12 September 2003 - Initial Release.

License

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


Written By
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom




Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.


We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net


Visit Software Kinetics

Comments and Discussions

 
GeneralRightToLeft Pin
Anonymous15-Dec-03 20:30
Anonymous15-Dec-03 20:30 
GeneralWinForms DataGrid Sort Indicator Pin
kkaps15-Dec-03 11:10
kkaps15-Dec-03 11:10 
Generalsuggestion: complex headers Pin
bluemiracoli15-Dec-03 3:47
bluemiracoli15-Dec-03 3:47 
Generalsuggestion: complex headers Pin
bluemiracoli15-Dec-03 3:37
bluemiracoli15-Dec-03 3:37 
GeneralSmall bug Pin
Thierry Parent14-Dec-03 22:11
Thierry Parent14-Dec-03 22:11 
GeneralFeature plan Pin
Bill Seddon12-Dec-03 8:25
Bill Seddon12-Dec-03 8:25 
GeneralRe: Feature plan Pin
NormDroid12-Dec-03 10:43
professionalNormDroid12-Dec-03 10:43 
GeneralSuggestion Pin
Jörgen Sigvardsson12-Dec-03 5:44
Jörgen Sigvardsson12-Dec-03 5:44 
I think you need to break things apart. I think the MVC paradigm should apply here.

How about something like this: (interfaces)
interface IGridView {
    public IGridModel Model { set; get; }
    public void ModelChanged();
}
  
interface IGridModel {
    public int Width { get; }
    public int Height { get; }
 
    public IGridCell this [int x, int y] { get; set; }
    IEnumerator Views { get; }
}
  
interface IGridController {
    public void Scroll(int xstep, int ystep);
    public CellRange Select(int x, int y, int width, int height);
    public IGridView View { get; set; }
    public IGridModel Model { get; set; }
    ...
}
 
interface IGridCell {
    public IGridCellEditor Editor { get; }
    public IGridCellPainter Painter { get; }
};
 
class BoundGridModel : IGridModel {
    // Cells are stored inside model
};
 
class UnboundGridModel : IGridModel {
    // Cells are not necessarily stored in model 
    // they may be located on disk, in database, etc.
};


etc. I think you get the idea. The model, view and controller are very loosely coupled which allows for interesting combinations and virtually unlimited extendability. A grid like that is almost worth dying for.. Smile | :)

--
20 eyes in my head, they're all the same![^]
GeneralRe: Suggestion Pin
NormDroid12-Dec-03 6:23
professionalNormDroid12-Dec-03 6:23 
GeneralVirtual Grid Pin
unitrunker12-Dec-03 3:37
unitrunker12-Dec-03 3:37 
GeneralRe: Virtual Grid Pin
NormDroid12-Dec-03 3:59
professionalNormDroid12-Dec-03 3:59 
GeneralRe: Virtual Grid Pin
Keith Farmer12-Dec-03 16:07
Keith Farmer12-Dec-03 16:07 
QuestionShould we contribute....? Pin
mogwai12-Dec-03 3:26
mogwai12-Dec-03 3:26 
AnswerRe: Should we contribute....? Pin
NormDroid12-Dec-03 3:57
professionalNormDroid12-Dec-03 3:57 
GeneralRe: Should we contribute....? Pin
mogwai12-Dec-03 4:41
mogwai12-Dec-03 4:41 
GeneralRe: Should we contribute....? Pin
NormDroid12-Dec-03 4:47
professionalNormDroid12-Dec-03 4:47 

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.