Windows Develop Bookmark and Share   
 index > Windows Forms Designer > setting negative padding for header text in datagridview control.
 

setting negative padding for header text in datagridview control.

I have gridview with number of columns. the width of column is very small say 20. I want to store large text into it.
I reduced font size.
Padding =0 all.
However, it shows small space from the gridline of header.
Is there any way to display header text of each column exactly near the gridline of column?
nilesh k
nilesh Kalyankar  Wednesday, September 23, 2009 8:50 AM

Hi,

You can draw the text yourself.

void dataGridView1_Paint(object sender, PaintEventArgs e)

{

//get the column header cell

Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);

e.Graphics.FillRectangle(new

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

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Near;

format.LineAlignment = StringAlignment.Center;

//r1.X -= 2;

e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText,

this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,

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

r1,

format);

}


More information drawing columnHeader:

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq6

http://www.codeproject.com/KB/grid/DataGridView_winforms.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, September 29, 2009 8:35 AM

Hi,

You can draw the text yourself.

void dataGridView1_Paint(object sender, PaintEventArgs e)

{

//get the column header cell

Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);

e.Graphics.FillRectangle(new

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

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Near;

format.LineAlignment = StringAlignment.Center;

//r1.X -= 2;

e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText,

this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,

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

r1,

format);

}


More information drawing columnHeader:

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq6

http://www.codeproject.com/KB/grid/DataGridView_winforms.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, September 29, 2009 8:35 AM

You can use google to search for other answers

Custom Search

More Threads

• Adding References in the Design Time automatically
• Changing font of ErrorProvider and DataGridViewRow.ErrorText
• Which Components and Controls can be used as MDIParent??
• Multiple forms and a DataSet
• The VisibleClipBounds is not modified by SetClip function
• Positioning the child controls
• Force UI of control to refresh on certain control property changed
• Controls displaying improperly.
• WebBrowser Control
• Can't drag and drop ActiveX Controls onto Component from designer