如何在不删除排序工具的情况下删除datagridview中的排序标志符号

编程入门 行业动态 更新时间:2024-10-28 04:19:15
本文介绍了如何在不删除排序工具的情况下删除datagridview中的排序标志符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在不删除其排序功能的情况下从DataGridView的列标题中删除排序标志符号。

How can i remove sorting glyph from column headers in DataGridView without removing its sorting functionality.

我正在使用C#开发Windows窗体应用程序,我想生成报告从datagridview中,datagridview列的宽度将在报表列中分配,由于DataGridView列包括排序标志符号的with,在我的情况下这是不必要的空间,我想将其从ColumnHeader中排除。

I am working on windows form application in C#, i want to generate report from a datagridview, where datagridview column width will assign in report column, where as the DataGridView column include the with of sorting glyph, that is unnecessary space in my case, i want to exclude it from ColumnHeader.

推荐答案

使用自定义单元格绘画实际上很容易做到。

This is actually quite easy to do using custom cell painting.

您需要做的就是处理 DataGridView CellPainting 事件:

All you need 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; } }

上面的代码非常简单-只需检查当前单元格位于标题行(索引为-1)中,然后绘制除 ContentBackground 以外的所有内容。

The code above is very simple - just check if the current cell is in a header row (has a -1 index) then paint everything except the ContentBackground.

I仅在我的Windows 7计算机上进行了检查,看起来还不错,看来内容背景仅用于排序标志符号-您将要在目标环境中对其进行测试,以确保不需要执行任何操作

I've only checked this on my Windows 7 machine and it looks fine, it appears that the content background is only used for the sort glyph - you will want to test it on the target environment to make sure you don't need to do any more involved custom painting to keep the ContentBackground without the glyph.

标头单元格的宽度仍将包含用于字形。我通常会接受,因为更改此选项会变得有些混乱,但是如果您必须使宽度适合文本,则可以执行以下操作。

The width of the header cell will still include the space for the glyph. I would generally accept that since changing this becomes a bit messy, but if you must have the width fit the text then something like the following will work.

首先设置宽度在 DataGridView 的 DataBindingComplete 事件中:

First set the width in the DataBindingComplete event of the DataGridView:

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; } } } }

一次您已经完成了操作,那么当单元格文本比标题长时,您仍然需要允许列自动调整大小。

Once you have done that you then need to still allow the column to autosize when the cell text is longer than the header.

为此,我使用了 CellValueChanged 事件:

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; } } }

更多推荐

如何在不删除排序工具的情况下删除datagridview中的排序标志符号

本文发布于:2023-05-31 12:03:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   情况下   标志   工具   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!