DataGrid - Change column Headers height, row vertical alignment text.

SergXY

Member
Joined
Jul 26, 2024
Messages
7
Programming Experience
1-3
In my application I use System.Windows.Forms.DataGrid (not DataGridView!) Unfortunately, I cannot change ColumnHeadersHeight - there is no such property for this control. And secondly, if I increase the row height, text is aligned to the top row, but vertical alignment is needed. Is there any solution for this problem?
 

Attachments

  • DataGrid.jpg
    DataGrid.jpg
    84.3 KB · Views: 4
Not an answer to your question. The DataGrid only existed to ease the migration of the VB6 programmers to VB.NET. It is just a wrapper for the old control. New development should use DataGridView which was written from scratch based on everything learned from the old DataGrid. If you find any references on how to customize the old VB6 DataGrid, that still work.
 
In my application I use System.Windows.Forms.DataGrid (not DataGridView!)

Why? That control has effectively been deprecated since 2005 and, while WinForms has been migrated to .NET Core 3.0 and later, that control was not included in that migration.
 
The answer is very simple. DataGridView is much heavier and this is very noticeable. In my program, I need to color some rows with a different color, and some individual cells with a different color. If you do this coloring in DataGridView, the application slows down when scrolling and blinks like a Christmas tree. I implemented the same thing in the old DataGrid element - and everything is OK. Loads quickly and no flickering.
Here is a screenshot of my program. Can you do this using DataGreedView? At the same time, the application should not slow down and flicker?
 

Attachments

  • sample.jpg
    sample.jpg
    63.5 KB · Views: 6
How did you implement the row coloring for the DataGridView? When I used to write WinForms code, I had something that showed a couple thousand rows and 12 columns. The rows were color coded into 8 possible colors. No flickering at all without even have to turn on double-buffering for the form.
 
For the ROW I used

1 variant :
C#:
private void DGV_loads_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e){
if (e.RowIndex > -1 && e.RowIndex < DGV_loads.RowCount - 1)
{
         if (DGV_loads.Rows[e.RowIndex].Cells[0].Value.ToString() == "True") {
               ((DataGridView)sender).Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.PaleGoldenrod;
}
Or 2 variant :
C#:
for (int i = 0; i < gvItem.Rows.Count; i++) {
if (DGV_loads.Rows[e.RowIndex].Cells[0].Value.ToString() == "True"))
gvItem.Rows[I][I].DefaultCellStyle.BackColor = Color.PaleGoldenrod;[/I]
For a Single Cell I used :
C#:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e){
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0"){
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
[/I]
 
Last edited by a moderator:
Part of your slow down is constant pulling the data out of the data grid view, and then converting that to a string. I was accessing the data source to which the data grid view was bound to so that the data is already in a readily accessible and consumable form. Pulling data back out of the control is the old Win32 way of doing things. Access the backing data is the more modern MVC or MVP way of doing things.
 
If this is possible, can I see the part of the code where you implemented it? Or an example, or a link where to look?
 
Sorry the was in an old Mecurial repo that I had. At first I was not worried when Atlassian dropped support for Mercurial, but when I discovered my machine backups were not actually working, I've since lost the code.

Another thing that came to mind was this:
You can customize the appearance of any cell by handling the DataGridView control's CellPainting event. You can extract the DataGridView control's Graphics from the Graphics property of the DataGridViewCellPaintingEventArgs. With this Graphics, you can affect the appearance of the entire DataGridView control, but you will usually want to affect only the appearance of the cell that is currently being painted. The ClipBounds property of the DataGridViewCellPaintingEventArgs enables you to restrict your painting operations to the cell that is currently being painted.
from

and:
You can control the appearance of DataGridView rows by handling one or both of the DataGridView.RowPrePaint and DataGridView.RowPostPaint events. These events are designed so that you can paint only what you want to while letting the DataGridView control paint the rest. For example, if you want to paint a custom background, you can handle the DataGridView.RowPrePaint event and let the individual cells paint their own foreground content. Alternately, you can let the cells paint themselves and add custom foreground content in a handler for the DataGridView.RowPostPaint event. You can also disable cell painting and paint everything yourself in a DataGridView.RowPrePaint event handler.
from

(emphasis mine)

I noticed in your code above, in the PrePaint you are changing the style instead of doing the painting. That will cause a cascade of calls back into the data grid view. The appropriate time to change the styles are while you are loading the data, not while you are drawing the data.

Anyway, as for access the backing list here's some pseudo-code
C#:
var cars = new List<Car>();
dataGridView.DataSource = cars;

:
void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.PaintParts says that background should be painted)
    {
        // access the cars' color directly to create a brush for painting
        var brush = new SolidBrush(cars[e.RowIndex].Color);
        :
    }
}
 
Back
Top Bottom