Hello Anbaesivam,
Thanks for using Microsoft Newsgroup Support Service, my name is Colbert
Zhou [MSFT] and I will be working on this issue with you.
Firstly, please let me make sure I understand your issue correctly. Now, we
are trying to create two rows of header in the DataGridView, right? Please
correct me if I have misunderstood your issue.
Based on my knowledge, the Windows Form DataGridView does not support
multiple rows of headers by default. The DataGridView Program Manager Mark
has confirmed this in the following forum discussion:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/49
e62b5a-51a2-4daf-b77b-8491d0d19b24/
If we really want this, we have to draw the header by ourselves. The
followings are codes work fine in my side. Would you mind giving it a try
and let me know if this resolves your issue?
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.Columns.Add("Column1", "My Column 1");
this.dataGridView1.Columns.Add("Column2", "My Column 2");
for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
{
this.dataGridView1.Columns[j].Width = 120;
}
this.dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.dataGridView1.ColumnHeadersHeight =
this.dataGridView1.ColumnHeadersHeight * 2;
this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopCenter;
this.dataGridView1.Paint += new
PaintEventHandler(dataGridView1_Paint);
}
void dataGridView1_Paint(object sender, PaintEventArgs e)
{
string[] defaultValues = { "Default Value 1", "Default Value 2"
};
for (int j = 0; j < 2; )
{
Rectangle r1 =
this.dataGridView1.GetCellDisplayRectangle(j, -1, true); //get the column
header cell
r1.X += 1;
r1.Y = r1.Height / 2 + 1;
r1.Width -= 2;
r1.Height = r1.Height / 2 - 2;
e.Graphics.FillRectangle(new
SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(defaultValues[j],
this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
new
SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
r1,
format);
j++;
}
}
Best regards,
Colbert Zhou (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.