Multilayered Columns in Windows Applications.


I am very new to Windows Application I had given a task to create a Multi layered Column Grid . So how to achieve the task in windows application . I will discuss with Code Snippets and real time images.

Description
I am going to accomplish a task in Windows Application i.e. Multi layered Column Grid . In windows Application there is no property to set Multi layered Columns for that we need to write the Code and logic as per our requirements. In my Grid the Columns have panned like this .

Here is the Below Image i had a grid like this.

bhushan

I want to show the Grid Like this

sriram

What we need to write Code and Logic for achieving the task I will write the Code and Description.
This is not an easy task to achieve there is a lot of code and logic to write .

Our first and foremost step is to Create Event Delegates: "what are those I will write here".


this.dgvtotals.ColumnHeadersHeightSizeMode =

DataGridViewColumnHeadersHeightSizeMode.AutoSize;

this.dgvtotals.ColumnHeadersHeight =

this.dgvtotals.ColumnHeadersHeight * 2;

this.dgvtotals.ColumnHeadersDefaultCellStyle.Alignment =

DataGridViewContentAlignment.BottomCenter;

this.dgvtotals.CellPainting += new

DataGridViewCellPaintingEventHandler(dgvtotals_CellPainting);

this.dgvtotals.Paint += new PaintEventHandler(dgvtotals_Paint);

Set the Column Headers to Auto Size. The important step is the the Cell Paint even delegate enables to achieve the task by creating a Rectangle header in the Grid Column.

void dgvtotals_Paint(object sender, PaintEventArgs e)
{
string[] monthes = { "30 Days" };
for (int j = 4; j < 6; )
{

//get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

r1.X += 1;

r1.Y += 1;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Near;

e.Graphics.DrawString(monthes[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}


for (int j = 7; j < 9; )
{

//get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

string[] monthess = { "90 Days" };

r1.X += 2;

r1.Y += 2;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(monthess[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}

for (int j = 10; j < 13; )
{

// get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

string[] monthess = { "180 Days" };

r1.X += 2;

r1.Y += 2;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(monthess[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}

for (int j = 13; j < 16; )
{

//get the column header cell
// BY SETTING THE ATTRIBUTE FALSE IT WILL NOT OVERFLOW THE COLUMN HEADER...
Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

string[] monthess = { "360 Days" };

r1.X += 2;

r1.Y += 2;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(monthess[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}
}


As per the above Code, we need to Create a rectangle and fill with BackGround Color. As my columns are not divided exactly to create a top most header. So I choose my Column Positions to Create a rectangle on the above header. For that i need to start from 4th Column to 6th Column on the above I can place 30 days and from so on...

For set the rectangle Horizontal X and Vertical Y position and e.graphicsdrawstring Method will write the Column header above i loop the three Columns at a time for creating a rectangle.


void dgvprdgrp_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{

if (e.RowIndex == -1 && e.ColumnIndex > -1)
{

e.PaintBackground(e.CellBounds, false);

Rectangle r2 = e.CellBounds;

r2.Y += e.CellBounds.Height / 2;

r2.Height = e.CellBounds.Height / 2;

e.PaintContent(r2);

e.Handled = true;

}

}



Following is the complete code:


this.dgvtotals.ColumnHeadersHeightSizeMode =

DataGridViewColumnHeadersHeightSizeMode.AutoSize;

this.dgvtotals.ColumnHeadersHeight =

this.dgvtotals.ColumnHeadersHeight * 2;

this.dgvtotals.ColumnHeadersDefaultCellStyle.Alignment =

DataGridViewContentAlignment.BottomCenter;

this.dgvtotals.CellPainting += new

DataGridViewCellPaintingEventHandler(dgvtotals_CellPainting);

this.dgvtotals.Paint += new PaintEventHandler(dgvtotals_Paint);

void dgvtotals_Paint(object sender, PaintEventArgs e)
{

string[] monthes = { "30 Days" };

for (int j = 4; j < 6; )
{

//get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

r1.X += 1;

r1.Y += 1;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Near;

e.Graphics.DrawString(monthes[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}


for (int j = 7; j < 9; )
{

//get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

string[] monthess = { "90 Days" };

r1.X += 2;

r1.Y += 2;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(monthess[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}

for (int j = 10; j < 13; )
{

// get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

string[] monthess = { "180 Days" };

r1.X += 2;

r1.Y += 2;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(monthess[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}

for (int j = 13; j < 16; )
{

//get the column header cell

Rectangle r1 = this.dgvtotals.GetCellDisplayRectangle(j, -1, FALSE);

string[] monthess = { "360 Days" };

r1.X += 2;

r1.Y += 2;

r1.Width = r1.Width * 3 - 2;

r1.Height = r1.Height / 2 - 2;

e.Graphics.FillRectangle(new

SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString(monthess[0],

this.dgvtotals.ColumnHeadersDefaultCellStyle.Font,

new SolidBrush(this.dgvtotals.ColumnHeadersDefaultCellStyle.ForeColor),

r1,

format);

j += 3;

}
}

void dgvprdgrp_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{

if (e.RowIndex == -1 && e.ColumnIndex > -1)
{

e.PaintBackground(e.CellBounds, false);

Rectangle r2 = e.CellBounds;

r2.Y += e.CellBounds.Height / 2;

r2.Height = e.CellBounds.Height / 2;

e.PaintContent(r2);

e.Handled = true;

}

}


Attachments

Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: