List of Exercises

Make Border in Row of DataGridView in C#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Border_Row_DataGridView
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      DataTable dt = new DataTable();
      dt.Columns.Add();
      dt.Columns.Add();
      dataGridView1.DataSource = dt;
    }

    private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
      if (e.RowIndex == 2)
      {
        // Calculate the bounds of the row
        int rowHeaderWidth = dataGridView1.RowHeadersVisible ?
                  dataGridView1.RowHeadersWidth : 0;
        Rectangle rowBounds = new Rectangle(
          rowHeaderWidth,
          e.RowBounds.Top,
          dataGridView1.Columns.GetColumnsWidth(
              DataGridViewElementStates.Visible) -
              dataGridView1.HorizontalScrollingOffset + 1,
         e.RowBounds.Height);
        // Paint the border
        ControlPaint.DrawBorder(e.Graphics, rowBounds,
                Color.Red, ButtonBorderStyle.Solid);

        // Paint the background color
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor =
                          Color.LightBlue;
      }
    }
  }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Make Border in Row of DataGridView in WinForms C#


Video Thumbnail