How to add borders to rows in winform?

To add a border to a row in WinForm, you can follow these steps:

  1. Create a custom control that inherits from a container control like Panel or TableLayoutPanel.
  2. Set the border style to a fixed single line border, as well as specifying the color and width in the constructor of the custom control.
  3. Add child controls to the custom control to display the contents of a row.
  4. Use this custom control instead of the typical Panel or TableLayoutPanel containers where additional rows are needed.

Here is an example code that demonstrates how to create a custom control to add borders to rows.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomControls
{
    public class CustomRow : Panel
    {
        public CustomRow()
        {
            this.BorderStyle = BorderStyle.FixedSingle;
            this.BackColor = Color.White;
            this.ForeColor = Color.Black;
            this.Font = new Font("Arial", 10);
            this.Padding = new Padding(5);
        }
    }
}

In the area where this custom control is used, rows can be added by following these steps:

CustomRow row = new CustomRow();
row.Dock = DockStyle.Top;
this.Controls.Add(row);

Label label = new Label();
label.Text = "Row content";
row.Controls.Add(label);

In this way, we can achieve the effect of adding borders to rows in WinForm.

bannerAds