DataGridView之编码列重绘

实现方式如下:

private void dgvRelation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                SolidBrush b = new SolidBrush(this.dgvRelation.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture),
                    this.dgvRelation.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }
            catch { return; }
        }

效果如下:

注意:使用该方式处理编号列的显示,需要处理cell_click事件,如: if (e.RowIndex == -1||e.ColumnIndex==-1) return;

 

转载于:https://www.cnblogs.com/YYkun/p/7111782.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于winform中的DataGridView控件,重绘滚动条的方式如下: 1. 创建一个新的滚动条,使用CreateWindow函数,指定ScrollBar类和style为WS_CHILD | WS_VISIBLE | SBS_VERT | SBS_RIGHTALIGN。 2. 让新创建的滚动条关联到DataGridView的滚动条事件,使用SetScrollInfo函数设置滚动条信息,包括滚动条范围,当前位置和页大小等等。 3. 在DataGridView的Paint事件中重绘控件,包括滚动条和数据区域。 代码示例: ``` private const uint WM_HSCROLL = 0x0114; private const uint WM_VSCROLL = 0x0115; private const uint SB_THUMBTRACK = 5; [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); IntPtr hScrollBar; private void dataGridView1_Resize(object sender, EventArgs e) { int borderWidth = SystemInformation.Border3DSize.Width; int x = dataGridView1.Width - SystemInformation.VerticalScrollBarWidth - borderWidth; int y = dataGridView1.Top + borderWidth; int width = SystemInformation.VerticalScrollBarWidth; int height = dataGridView1.Height - 2 * borderWidth; if (hScrollBar == IntPtr.Zero) { hScrollBar = CreateWindow("SCROLLBAR", null, WS_CHILD | WS_VISIBLE | SBS_VERT | SBS_RIGHTALIGN, x, y, width, height, dataGridView1.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); SetScrollInfo(hScrollBar, SIF_ALL, 0, 100, 10, 0); } else { MoveWindow(hScrollBar, x, y, width, height, true); } } private void dataGridView1_Scroll(object sender, ScrollEventArgs e) { IntPtr wParam; if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll) { wParam = (IntPtr)(SB_THUMBTRACK << 16) | (IntPtr)(e.NewValue & 0xFFFF); SendMessage(dataGridView1.Handle, WM_HSCROLL, wParam, IntPtr.Zero); } else if (e.ScrollOrientation == ScrollOrientation.VerticalScroll) { wParam = (IntPtr)(SB_THUMBTRACK << 16) | (IntPtr)(e.NewValue & 0xFFFF); SendMessage(hScrollBar, WM_VSCROLL, wParam, IntPtr.Zero); } dataGridView1.Invalidate(); } private void dataGridView1_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(Brushes.White, dataGridView1.ClientRectangle); IntPtr hDC = e.Graphics.GetHdc(); SendMessage(dataGridView1.Handle, WM_PRINTCLIENT, hDC, PRF_CLIENT); e.Graphics.ReleaseHdc(hDC); if (hScrollBar != IntPtr.Zero) { hDC = e.Graphics.GetHdc(); SendMessage(hScrollBar, WM_PRINTCLIENT, hDC, PRF_CLIENT); e.Graphics.ReleaseHdc(hDC); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值