Datagridview隐藏行标题记录选择器

本文提供了三种方法来隐藏Datagridview组件中的行标题记录选择器,包括简单地减小行头宽度、手动重绘行标题以及使用智能方法请求datagridview自动处理。这些解决方案旨在帮助开发者优化应用的视觉效果。
摘要由CSDN通过智能技术生成

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions.

为了在行标题中隐藏“丑陋的”记录选择器(三角形),以下是一些建议。

Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column.

Microsoft没有直接的方法/属性来执行此操作。 您只能隐藏行标题列。

First solution, the easy way

第一个解决方案,简单的方法

The first solution is to make the column slim enough, so that the record selectors are not drawn: the are on the left side of the column. Set the property RowHeaderWidth to a value less than 20, but this value depends on the style you used...

第一种解决方案是使列足够细长,以免绘制记录选择器:列的左侧。 将属性RowHeaderWidth设置为小于20的值,但是此值取决于您使用的样式...

Second solution, the long way

第二个解决方案,很长的路要走

OK, you can always redraw your items completely, by doing the work "by hand"...

好的,您始终可以通过“手动”完成工作来完全重绘项目...

This is what I found by Googling; it is in C# language, but I think it's easy to understand.

这是我通过Google搜索发现的; 它是用C#语言编写的,但我认为它很容易理解。

 void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs  e)

        {

            if (e.ColumnIndex == -1 && e.RowIndex > -1)

            {

                e.PaintBackground(e.CellBounds,true);

                using (SolidBrush br = new SolidBrush(Color.Black))

                {

                    StringFormat sf = new StringFormat();

                    sf.Alignment = StringAlignment.Center;

                    sf.LineAlignment = StringAlignment.Center;

                    e.Graphics.DrawString(e.RowIndex.ToString(),

                        e.CellStyle.Font, br, e.CellBounds, sf);

                }

                e.Handled = true;

            }

        }

Third solution, the smart way

第三种解决方案,聪明的方法

I prefer this solution. I don't know if anyone (and it is probable) have already posted something similar, in this case I apologize :-)

我更喜欢这种解决方案。 我不知道是否有人(可能)已经发布过类似的内容,在这种情况下,我很抱歉:-)

This solution is the middle-way of the others. It is simplier and doesn't need to draw anything: just ask the datagridview to do the job for you.

该解决方案是其他解决方案的中间。 它更简单,不需要绘制任何东西:只需要求datagridview为您完成这项工作即可。

Private Sub GRD_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles GRD.CellPainting
        If (e.ColumnIndex < 0) AndAlso (e.RowIndex >= 0) Then
            ' This will paint the background
            e.PaintBackground(e.CellBounds, False)
            ' And this will paint your text without the record selectors
            e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground)
            ' This avoids grid's own paint handler to execute
            e.Handled = True
        End If
    End Sub

In this way, you don't have to set the text fonts/format/colors and the backgrounds.

这样,您不必设置文本字体/格式/颜色和背景。

The content of the first column is painted correctly (if you need, for example a long description for the row).

第一列的内容正确绘制(如果需要,例如,该行的详细说明)。

I hope this would help developers when they spend time in "tuning" applications.

我希望这对开发人员花时间在“调整”应用程序上有帮助。

翻译自: https://www.experts-exchange.com/articles/3108/Datagridview-hiding-rowheader-record-selectors.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值