How to remove glyph sorting in datagridview without deleting Sort object

How can I remove a sort glyph from column headers in a DataGridView without removing its sort function.

I am working on a Windows Form application in C #, I want to generate a report from datagridview, where the column width of the datagridview will be assigned in the report column, where glyph sorting is enabled in the DataGridView column, which is extra space in my case, I want to exclude it from ColumnHeader.

+3
source share
3 answers

This is actually quite simple to do using custom coloring elements.

All you have to do is handle the DataGridView CellPainting event:

 dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting); 

And in the handler do something like this:

 void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1) { e.Paint(e.CellBounds, DataGridViewPaintParts.All &~DataGridViewPaintParts.ContentBackground); e.Handled = true; } } 

The above code is very simple - just check if the current cell is in the title bar (has an index of -1), and then I paint everything except the ContentBackground .

I only tested this on my Windows 7 machine and it looks good, it seems that the background of the content is used only for the sorting glyph - you will want to check it in the target environment to make sure that you do not need to do even more complex regular painting, so that save ContentBackground without glyph.


The width of the header cell will still contain space for the glyph. I would generally agree that since changing this becomes a bit messy, but if you have to have a width that matches the text, then something like the following works.

First set the width in the DataBindingComplete DataGridView event:

 void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { if (dataGridView1.AutoSizeColumnsMode == DataGridViewAutoSizeColumnsMode.AllCells) { // Loop over all the columns foreach (DataGridViewColumn c in dataGridView1.Columns) { // Work out the size of the header text Size s = TextRenderer.MeasureText(c.HeaderText, dataGridView1.Font); // Change the autosize mode to allow us to see if the header cell has the // longest text c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; if (s.Width + 10 > c.Width) { // If the header cell is longest we set the column width c.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; c.Width = s.Width + 10; } else { // If the header cell is not longest, reset the autosize mode c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } } } } 

After you have done this, you still need to allow the autoload of the column if the cell text is longer than the header.

For this, I used the CellValueChanged event:

 void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { DataGridViewColumn c = dataGridView1.Columns[e.ColumnIndex]; if (c.AutoSizeMode == DataGridViewAutoSizeColumnMode.None) { Size s = TextRenderer.MeasureText(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), dataGridView1.Font); if (s.Width > c.Width) { c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; } } } 
+3
source

You cannot do this in WinForms unless you create a custom view of the data grid to meet your needs, which I would call redundant for your requirement.

UPDATE

I created custom progress bars for buttons, for canvas controls, but didn't create a custom view of the data grid. However, here is my idea to do this ...

You essentially create a new UserControl and add a regular DataGridView to it and remove the headers. Now add a panel above the DataGridControl that "acts" in the column headers. You will have to handle all the events, for example, clicking on the headers to sort, resizing them to resize the columns and calling methods in the DataGridView to do the same.

If you want the effect of hiding column headers when the user scrolls down, you will also have to do it manually.

Sorry, I canโ€™t give you a solid starting point than this. If you are not familiar with creating custom controls, try creating a custom button (for example: the image on the left) or a progress bar that also shows the percentage of completion in the middle. You will get an idea of โ€‹โ€‹what you can do.

If you use WPF, I think that such things can be easily accomplished.

+1
source

I am facing the same problem when trying to get the text aligned to the right according to the contents of the cell (it looks silly in the string of numbers that the title text is also not completely aligned to the right). I noticed in another dgv that it was possible to squeeze a glyph.

In any case, the real simple solution is to fill the header text with leading spaces and set the bypass mode of the header cell to false. Example:

  dgv.Columns["Width"].HeaderText = " Width"; dgv["Width"].HeaderCell.Style.WrapMode = DataGridViewTriState.False; 

may play a little to get the right amount of leading spaces, but the result is nice. (I donโ€™t know if it works with finite spaces, too late at night to try.)

0
source

All Articles