Sponsored Ad

Thursday, October 22, 2009

C# Data Grid

SourceGrid is a Windows Forms control written entirely in C#, my aim is to generate a simple but flexible grid to use in all of the cases in which it is necessary to visualize or to alter a series of data in a table format. There's a lot of controls of this type available, but often are pricey, difficult to be customize or not compatible with. NET. The Microsoft DataGrid for me is DataSet orientated and therefore results often complicated to use in the cases in which the source data is not a DataSet and often is not customizable.

The grid is paramount to be used as a control and could be used optionally, a kind of right. Let's look at a kind of simplistic design.

Example:

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();
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers