.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...

Who is Participating?
cosieConnect With a Mentor Author 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)
                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) { }
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.