.Net DataGridView align cell content to top

I have a Winforms .Net2.0 C# app using DataGridView with noneditable text data in it.
I woul like 14 pixel high rows... I use a font with fontsize=8.
DataGridView1.RowTemplate.Height = 14;
And I set every row to the same height in a loop:
DataGridView1.Rows[i].Height = DataGridView1.RowTemplate.Height;

My only problem is that in every row there is a 2 pixel hight empty area betwwen the text and the top border, and the last 2 pixels of the text are cut by the bottom border of the cells.
I need 1 pixel high cell borders, but I need to "shift up" the cell content with 2 pixels so the

How can I remove the 2 pixel top padding of the cells?
Maybe custom CellPaint needed?
I tried DefaultCellStyle.padding=new Padding(0), but did not helped...
I tried also set align to TopLeft, but not helped...

Please help...





cosieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

strickddCommented:
you can apply a style to the "td" tag in a CSS or you can loop through all the rows after they are created and apply a style to text-align:right.
0
cosieAuthor Commented:
I found the solution myself.
You have to override the callpainting event and modify CellBounds.Top -= 1;

        void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            try
            {
                Rectangle CellBounds = e.CellBounds;
                CellBounds.Location = new Point(CellBounds.Left, CellBounds.Top - 1);

                if (e.ColumnIndex > -1 && e.RowIndex > -1 && e.RowIndex != moListCtrl.NewRowIndex)
                {
                    StringFormat sf = new StringFormat();
                    if (moListCtrl.Columns[e.ColumnIndex].DefaultCellStyle.Alignment == DataGridViewContentAlignment.TopRight)
                    {
                        sf.Alignment = StringAlignment.Far;
                    }

                    e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
                    if (this.moListCtrl[e.ColumnIndex, e.RowIndex].Selected)//selected
                    {
                        using (SolidBrush sb = new SolidBrush(e.CellStyle.SelectionForeColor))
                        {
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, sb , CellBounds, sf);
                        }
                    }
                    else //unselected
                    {
                        using (SolidBrush sb = new SolidBrush(e.CellStyle.ForeColor))
                        {
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, sb, CellBounds, sf);
                        }

                    }
                    e.Handled = true;
                }
            }
            catch (Exception) { }
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.