Remove bottom line of DataRepeater Control in Winforms

how can I remove the bottom line border of a data control in a control DataRepeater:

enter image description here

+4
source share
1 answer

The separator that you see in the control DataRepeaterbetween the elements is a drawing on the non-client area of ​​the control DataRepeaterItem.

You can find those DataRepeaterItemand process those messages WM_NCPAINTand draw a line with the same color as the element BackColoror any other color you want from (0, Height-1)to (Width-1, Height-1).

Implementation

, NativeWindow, , handle :

using Microsoft.VisualBasic.PowerPacks;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class DataRepeaterItemHelper : NativeWindow
{
    private DataRepeaterItem item;
    private const int WM_NCPAINT = 0x85;
    [DllImport("user32.dll")]
    static extern IntPtr GetWindowDC(IntPtr hWnd);
    [DllImport("user32.dll")]
    static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
    public DataRepeaterItemHelper(DataRepeaterItem repeaterItem)
    {
        item = repeaterItem;
        this.AssignHandle(item.Handle);
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_NCPAINT)
        {
            var hdc = GetWindowDC(m.HWnd);
            using (var g = Graphics.FromHdcInternal(hdc))
            using (var p = new Pen(item.BackColor, 1))
                g.DrawLine(p, 0, item.Height - 1, item.Width - 1, item.Height - 1);
            ReleaseDC(m.HWnd, hdc);
        }
    }
}

DrawItem DataRepeater , DataRepeaterItemHelper e.DataRepeaterItem . , . DataRepeater DataRepeaterItemHelper , DrawItem. , DataRepeaterItemHelper, List<DataRepeaterItem>:

new List<DataRepeaterItem> items = new List<DataRepeaterItem>();
void HandleItem(DataRepeaterItem item)
{
    if (items.Contains(item))
        return;
    var handler = new DataRepeaterItemHelper(item);
    items.Add(item);
}
private void Form1_Load(object sender, EventArgs e)
{
    //Load data and put data in dataRepeater1.DataSource
    var db = new TestDBEntities();
    this.dataRepeater1.DataSource = db.Category.ToList();
    this.dataRepeater1.Controls.OfType<DataRepeaterItem>().ToList()
        .ForEach(item => HandleItem(item));
    this.dataRepeater1.DrawItem += dataRepeater1_DrawItem;
}
void dataRepeater1_DrawItem(object sender, DataRepeaterItemEventArgs e)
{
    HandleItem(e.DataRepeaterItem);
}

:

enter image description here

:

  • Form1_Load Load. dataRepeater1_DrawItem DrawItem. Form1_Load .
  • DataRepeater.
+2

Source: https://habr.com/ru/post/1654581/


All Articles